Hardware Reference
In-Depth Information
{
Serial.print("Key: ");
Serial.print(key);
Serial.print(" Multiplier: ");
Serial.print(octave_multiplier);
Serial.print(" Frequency: ");
Serial.println(key*octave_multiplier);
delay(100);
}
You can easily find the music keys defined at the beginning with a search on
the Internet. They are the frequencies of the second octave of those notes. Note
that the key and octave_multiplier must be declared as volatile integers because
they are going to be changed within interrupt routines. changeKey() is called
every time the button interrupt is triggered. It changes the octave's base value
by moving from key to key. changePitch() calls tone() to set the frequency for
the speaker. It is triggered every .5 seconds by the timer interrupt. Each time
it is triggered, it doubles the frequency of the original note until it reaches 16
times its original frequency. It then loops back around and starts again at the
base frequency for the current note. Within loop() , the current key, multiplier,
and frequency are printed to the serial monitor every .1 seconds.
NOTE Towatchademovideoofthesoundmachine,checkout
www.exploringarduino.com/content/ch12 .Youcanalsofindthis
videoontheWileywebsiteshownatthebeginningofthischapter.
Summary
In this chapter you learned about the following:
There are tradeoffs between polling inputs and using interrupts.
Different Arduinos have different interrupt capabilities. The Due can
interrupt on any I/O pin, but other Arduinos have particular interrupt-
enabled pins.
Buttons can be debounced in hardware using an RC circuit and a Schmitt
trigger.
The Arduino can be made to respond to inputs asynchronously by attach-
ing interrupt functions.
You can install a third-party timer library to adder timer interrupt func-
tionality to the Arduino.
You can combine timer interrupts, hardware interrupts, and polling into
one program to enable pseudo-simultaneous code execution.
 
Search WWH ::




Custom Search