Step 4 - Watch the Video Walkthrough
Code Walkthrough Text
The first step of this program is to create a global variable called checkPress. It will hold the number of times you press a button and use that number to trigger sound effects.
There is also a function created for this program- checkPress(). The job of checkPress is to make it easy to increase pressCount. As you'll see by playing with the program, it is not simple to make a very reactive button to switch between sound effects.
In the setup() function, you set the pinModes of your components.
The first job of the loop is to run the checkPress() function. At first, the button will be unpressed and pressCount will equal 0. Because of that, all of the other statements in the code are false and nothing happens.
Pressing the button quickly will be caught by checkPress, which is running thousands of times a second. When checkPress sees the LOW signal from the button, pressCount becomes one.
For pressCount == 1, you’re ranging through the tones 0 to 1200 with a delay of 5 milliseconds between. The variable ‘i’ is used for both counting up through the ‘for’ loop and for assigning the tone to the speaker. The for will loop 1200 times, taking about 6000 milliseconds total, and will play each tone from 0 to 1200 for 5 milliseconds.
You can see that the checkPress() function is run every time the for loop runs. Then an if statement inside the for loop checks to see if pressCount does not equal 1 anymore. When that is true, the 'break' command ends the for loop immediately and the computer skips down to the next line of code.
pressCount == 2 is picking a random tone every 50 milliseconds and playing it to the speaker. You don’t need to use a variable here, since random() is a built-in function. Because of the way an if statement works, the checkPress() is still called at the top of the loop between each play of a random tone. Any time you press the button, the pressCount will be incremented before the next random tone is played.
pressCount == 3 is another for loop, this one moving in the opposite direction from pressCount ==1 . Starting at a tone value of 1300, the tone will fall by 1 every 5 milliseconds, then run the checkPress() function. When the value of i is 31, or when the value of pressCount is no longer 3, the for loop will exit and check the if statement again.
pressCount == 4 requires some variables to work correctly, so you can create those variables right above the ‘while’ statement. This can be a good idea in complicated code- create the variables close to where you will use them. While pressCount == 4, the variable ‘pitch’ is going to increase by ‘scale’ amount each time the loop is run. The if statement inside the while loop checks to see if the tone has reached a maximum of 1200 or a minimum of 0. If that’s true, then the scale becomes negative, reversing the tone’s direction.
pressCount == 5 plays two alternating tones back-to-back. One is rising and the other is falling by the amount in the variable ‘scale’. It uses variables to track these two tones and you can create them right above the ‘while’ loop. You want to play lowTone, play highTone, then increase lowTone by 10 and decrease highTone by 10. Within the ‘while’ is an ‘if’ statement that checks the value of lowTone and highTone every loop. When the max or minimum is hit, the lowTone and highTone are reset. It will only occur once each time that the variables are reset, but lowTone and highTone will be exactly equal at some point!
pressCount can continue on forever, but you stop it after 5. To reset the count to 0 and stop the tone, you can use a simple ‘if’ statement. Always be sure to check for all numbers greater than 5. Why? Imagine pressCount = 5 and you press and hold the button longer than you should. pressCount may now equal 7! Since you haven’t planned for 7 to be a possible pressCount, your code won’t know what to do.