For this project, I followed along with the second half of Chapter 3 of Learning JavaScript Robotics by Kassandra Perch. In honor of my brothers birthday yesterday, and my birthday tomorrow, I decided to make the speaker play Happy Birthday!


The set up is pretty simple. Notice that the piezo uses a PWM pin. This is because the change in power is what causes the change in tone.

The Code

This code is very similar to the one from the previous project. One difference is that the speaker has more attributes: song, beats, and tempo.

The Song

I used the notes from this song for the “song” attribute, with a dash placed between each one in order to get a pause between the notes. I used an 1/8th beat, so in the cod,e one letter under the “song” attribute would play for 1/8th of a beat. Longer notes, like a quarter, are represented by two letters next to each other without a dash in between, and so on.