Tone melody
A component which produces some kind of action when it receives a signal from the Arduino is called an actuator. An LED is an example of an actuator, but there are many other kinds including motors and speakers. In this tutorial we will use a simple speaker to play a simple melody when the sketch first starts.
The code for this example is shown in the box below. You can load it into
your Arduino IDE from the File menu: File → Examples → 02.Digital →
toneMelody
The code illustrates the use of a header file. The definitions of the notes are contained in the file pitches.h which is included in the sketch by the command at line 1. This file is included with the example in the Arduino IDE on a separate tab.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
Connecting the piezo buzzer is quite easy. Please use the diagram below to set up your circuit.
Challenge
At the moment, the melody only plays once when the sketch first starts.
Change the circuit and the sketch so that the melody plays whenever a button is pressed.