O'Reilly logo

Beginning Arduino by Michael McRoberts

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Project 12 – Piezo Sounder Melody Player

Rather than using the piezo to make annoying alarm sounds, why not use it to play a melody? You are going to get your Arduino to play the chorus of “Puff the Magic Dragon.” Leave the circuit exactly the same as in Project 11; you are just changing the code.

Enter the Code

Open up your Arduino IDE and type in the code from Listing 4-2.

Listing 4-2. Code for Project 12

// Project 12 - Piezo Sounder Melody Player #define NOTE_C3  131#define NOTE_CS3 139 #define NOTE_D3  147 #define NOTE_DS3 156 #define NOTE_E3  165 #define NOTE_F3  175 #define NOTE_FS3 185 #define NOTE_G3  196 #define NOTE_GS3 208 #define NOTE_A3  220 #define NOTE_AS3 233 #define NOTE_B3  247 #define NOTE_C4  262 #define NOTE_CS4 277 #define ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required