Speaker and 2x Momentary Switches 02
This program makes Arduino to read signals (on/off) from two momentary switches (it reads signals as fast as the processor can go). The program then instruct Arduino to select a larger or smaller frequency in the array (list of constants or variables) that contains frequencies that are 10 octaves of musical notes. Frequencies for each note is predefined in the array at the top of the program. The variable “note” keeps the current position of the frequency array.
playTone is a custom function to play a tone with the speaker. The first item in the bracket of the function is the pin that the speaker is connected to. The second item is the frequency of the pitch. The third item is the duration of the tone in microseconds.
Frequencies this program generates are technically not accurate, but they are good enough for generating sound effects. Arduino has its own function to play a frequency, which is much more accurate, but the problem is that it disables PWM output on pins 3 and 11. This is problematic if you want to combine RGB-LEDs with sound that relies on Arduino’s tone() function. If you are interested to learn more about Arduino’s Tone library, see “tone()” in Arduino’s language reference.