## Circuit 2A: Buzzer

• Public
By Adam Kroeker June 23, 2022 - 12:43pm

I had no issues building the circuit. I decided to alter the example code so that when the "your name" part of the happy birthday song plays, it actually plays my name (Adam) in morse code. Here is the code:

`int speakerPin = 10; //the pin that buzzer is connected to`
`void setup(){ pinMode(speakerPin, OUTPUT); //set the output pin for the speaker}`
`void loop(){ play('g', 2); //ha play('g', 1); //ppy play('a', 4); //birth play('g', 4); //day play('C', 4); //to play('b', 4); //you`
`play(' ', 2); //pause for 2 beats`
`play('g', 2); //ha play('g', 1); //ppy play('a', 4); //birth play('g', 4); //day play('D', 4); //to play('C', 4); //you`
`play(' ', 2); //pause for 2 beats`
`play('g', 2); //ha play('g', 1); //ppy play('G', 4); //birth play('E', 4); //day play('C', 4); //dear playAdam(10); //Adam`
`play(' ', 2); //pause for 2 beats`
`play('F', 2); //ha play('F', 1); //ppy play('E', 4); //birth play('C', 4); //day play('D', 4); //to play('C', 6); //you`
`while (true) {} //get stuck in this loop forever so that the song only plays once}`
`void playAdam(float numberOfBeats){ float ditDuration = numberOfBeats/33; //"Adam" in morse code needs 33 dit durations, //so the duration of one dit should be 1/33 of //the total duration of the name.`
`//Letter A play('b', ditDuration); //dit play(' ', ditDuration); //space play('b', 3*ditDuration); //dah`
`//Space play(' ', 3*ditDuration); //longSpace`
`//Letter D play('b', 3*ditDuration); //dah play(' ', ditDuration); //space play('b', ditDuration); //dit play(' ', ditDuration); //space play('a', ditDuration); //dit`
`//Space play(' ', 3*ditDuration); //longSpace`
`//Letter A play('a', ditDuration); //dit play(' ', ditDuration); //space play('a', 3*ditDuration); //dah`
`//Space play(' ', 3*ditDuration); //longSpace`
`//Letter M play('a', 3*ditDuration); //dah play(' ', ditDuration); //space play('a', 3*ditDuration); //dah}`
`void play( char note, float beats){ int numNotes = 14; // number of notes in our note and frequency array (there are 15 values, but arrays start at 0)`
`//Note: these notes are C major (there are no sharps or flats)`
`//this array is used to look up the notes char notes[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B', ' '}; //this array matches frequencies with each letter (e.g. the 4th note is 'f', the 4th frequency is 175) int frequencies[] = {131, 147, 165, 175, 196, 220, 247, 262, 294, 330, 349, 392, 440, 494, 0};`
`int currentFrequency = 0; //the frequency that we find when we look up a frequency in the arrays int beatLength = 150; //the length of one beat (changing this will speed up or slow down the tempo of the song)`
`//look up the frequency that corresponds to the note for (int i = 0; i < numNotes; i++) // check each value in notes from 0 to 14 { if (notes[i] == note) // does the letter passed to the play function match the letter in the array? { currentFrequency = frequencies[i]; // Yes! Set the current frequency to match that note } }`
`//play the frequency that matched our letter for the number of beats passed to the play function tone(speakerPin, currentFrequency, beats * beatLength); delay(beats * beatLength); //wait for the length of the tone so that it has time to play delay(50); //a little delay between the notes makes the song sound more natural`
`}`

The code is mostly the same as the example program, the main difference is that my program includes the playAdam function. The playAdam function takes an integer representing the number of beats that the morse code for "Adam" should last in the happy birthday song. A higher value makes the morse code play for a longer time, and a smaller value makes it play more quickly.

Here is a video of the code in action:

I had a bit of trouble with writing my program. I had initially stored the numberOfBeats and ditDuration variables in the playAdam function as ints instead of floats. The problem was that integer division does not allow for any decimal places. I wanted the ditDuration to be proportional to the number of beats dedicated to the morse code portion of the song, so that if the entire name takes 10 beats, then one ditDuration should last for 10/33 beats, since there are 33 dit durations in the morse code encoding of "Adam". However, performing integer division on 10 and 33 resulted in 0, and 33/33 results in 1, so the ditDuration was always either extremely short or a whole beat long. Storing the numberOfBeats and ditDuration variables as floats solved this problem, so now one dit in the morse code section of the happy birthday song lasts for 10/33 or about 0.3 beats.

Another challenge was deciding how much of the morse code section should be played in the 'b' note and how much should be played in the 'a' note. In the end I decided that the first 13 dit durations should be in 'b' and the last 20 dit durations should be in 'a', since that is approximately a 60/40 split.