Devices & Components
Arduino Uno Rev3
Mini breadboard
Trim Potentiometer
Jumper wires (generic)
Resistor 10k ohm
Male/Female Jumper Wires
I used an old Tjena box but any box will do
LDR, 5 Mohm
Buzzer, Piezo
ESD Tape, Masking
Software & Tools
Arduino IDE
Project description
Code
LightSensingBuzzer
arduino
Firstly, take a look at the section of code from about lines 30 through to 59. This section determines what tune will be played and at what speed (via tempo value). The current tune has a total of 19 notes and rests. This is the value of songLength (line 31). You must edit this value to match the number of notes/rests in your tune. Edit the song length on line 29 to reflect the number of notes in your song. Include all the notes and rests in the count. Follow the commented instructions in the code when editing the notes and the beats array. You'll need some understanding of how musical compositions work to code your own tune. You can expect to spend some time getting the timing of the notes correct. Speed up or slow down the tune by changing the Tempo value on line 29.
1 2/****************************************************************** 3 * SparkFun Inventor"s Kit 4 * Example sketch 11 5 * 6 * BUZZER 7 * with lots of help from the Arduino community, 8 * and amended by J. Hadley to include an extra octave and semitones. 9 * Further amended August 2020 to allow the playing of a song to be 10 * interrupted by a sensor input. When the sensor value meets the 11 * condition, the song will cease playing. 12 * 13 * This sketch uses the buzzer to play songs. 14 * The Arduino"s tone() command will play notes of a given frequency. 15 * 16 * This sketch was written by SparkFun Electronics, 17 * (This sketch was originally developed by D. Cuartielles for K3) 18 * This code is completely free for any use. 19 * Visit http://learn.sparkfun.com/products/2 for SIK information. 20 * Visit http://www.arduino.cc to learn about the Arduino. 21 * 22 * Version 2.0 6/2012 MDG 23 * Version 2.1 9/2014 BCH 24 *****************************************************************/ 25 26 27const int SensorPin = A4; // switch to use a different analog sensor 28int sensorThreshold = 500; // what sensor value will stop the song 29const int buzzerPin = 3; // connect the buzzer to pin 3 (Thinkershield buzzer pin) 30int tempo = 270; // The tempo is how fast to play the song (beats per minute). 31const int songLength = 19; // sets the number of notes of the song 32 33// Notes is an array of text characters corresponding to the notes 34// in your song. A space represents a rest (no tone). Each note or rest must be separated by a comma 35// with no comma after the last note. 36 37// It"s important that you use single quotes for single letters like "b", "g" and the rest ' ', and 38// that you use double quotes for notes requiring multiple letters like "g2" (which is the g in the octave above) 39// and "f#" (which is the sharp - a semitone above "f") in the lower octave. Your code may compile if these are 40// incorrect but those notes will not play. It is a quirky part of the Arduino coding language in this IDE. 41// In Tinkercad, it works differently and all notes should be enclosed in double quotes. 42 43char* notes[songLength] = { 44 ' ', 45 "f#", "g#", 'b', 'b', ' ', 46 "d2#", "f2#", "g2#", "g2#", ' ', 47 "g2#", "f2#", "f2#", "e2", ' ', 48 "e2", "d2#", "d2#"}; 49 50// beats[] is an array of values for each note. A "1" represents a quarter-note, 51// "2" a half-note, and "4" a full-note. 52// Don"t forget that the rests (spaces) need a length as well. 53 54int beats[songLength] = { 55 2, 56 1, 1, 2, 3, 2, 57 1, 1, 2, 3, 2, 58 1.5, 0.5, 1, 3.5, 2, 59 1.5, 1, 3.5}; 60 61void setup() 62{ 63 pinMode(buzzerPin, OUTPUT); // sets the buzzer pin as an OUTPUT 64 pinMode(SensorPin, INPUT); 65 //Serial.begin(9600); 66} 67 68void loop() 69{ 70 int i, duration; 71 bool keepPlaying = true; 72 int value; 73 74 i=0; 75 value = analogRead(SensorPin); 76 //Serial.println(value); 77 keepPlaying = (value > sensorThreshold); 78 79 while ((i < songLength) && keepPlaying) 80 { 81 //Serial.println("playing"); 82 duration = beats[i] * tempo; // length of note/rest in ms 83 84 if (notes[i] == ' '){ // is this a rest? 85 delay(duration); // then pause for a moment 86 //Serial.println("pause"); 87 } 88 else // otherwise, play the note 89 { 90 tone(buzzerPin, frequency(notes[i]), duration); 91 delay(duration); // wait for tone to finish 92 //Serial.println("note is " + String(notes[i])); 93 } 94 95 delay(tempo/20); // brief pause between notes 96 value = analogRead(SensorPin); 97 keepPlaying = (value > sensorThreshold); 98 //Serial.println(value); 99 i = i+1; 100 } 101 102 //while(true){ 103 // We only want to play the song once, so we pause forever 104 //} 105 106 /* If you'd like your song to play once only, uncomment the 3 lines that make up the 107 while(true)statement above. When commented out, the song will play repeatedly. */ 108} 109 110//------------------------------------------------------------------------------------------------------------------------- 111int frequency(char* note) 112{ 113 int i; 114 const int numNotes = 25; // number of notes we"re storing - includes the semitones over 2 octaves starting at middle C 115 char* names[numNotes] = { 116 'c', "c#", 'd', "d#",'e', 'f', "f#", 'g', "g#", 'a', "a#", 'b', "c2", "c2#", "d2", "d2#", "e2", "f2", "f2#", "g2", "g2#", "a2", "a2#", "b2", "c3" }; 117 int frequencies[numNotes] = { 118 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 1047 }; 119 120 // Now we"ll search through the letters in the array, and if 121 // we find it, we"ll return the frequency for that note. 122 123 for (i = 0; i < numNotes; i++) // Step through the notes 124 { 125 if (names[i] == note) // Is this the one? 126 { 127 return(frequencies[i]); // Yes! Return the frequency and exit function. 128 } 129 } 130 return(0); // We looked through everything and didn't find it, 131 // but we still need to return a value, so return 0. 132} 133
LightSensingBuzzer
arduino
Firstly, take a look at the section of code from about lines 30 through to 59. This section determines what tune will be played and at what speed (via tempo value). The current tune has a total of 19 notes and rests. This is the value of songLength (line 31). You must edit this value to match the number of notes/rests in your tune. Edit the song length on line 29 to reflect the number of notes in your song. Include all the notes and rests in the count. Follow the commented instructions in the code when editing the notes and the beats array. You'll need some understanding of how musical compositions work to code your own tune. You can expect to spend some time getting the timing of the notes correct. Speed up or slow down the tune by changing the Tempo value on line 29.
1 2/****************************************************************** 3 4 * SparkFun Inventor"s Kit 5 * Example sketch 11 6 * 7 * BUZZER 8 * with 9 lots of help from the Arduino community, 10 * and amended by J. Hadley to include 11 an extra octave and semitones. 12 * Further amended August 2020 to allow the playing 13 of a song to be 14 * interrupted by a sensor input. When the sensor value meets 15 the 16 * condition, the song will cease playing. 17 * 18 * This sketch uses 19 the buzzer to play songs. 20 * The Arduino"s tone() command will play notes of 21 a given frequency. 22 * 23 * This sketch was written by SparkFun Electronics, 24 25 * (This sketch was originally developed by D. Cuartielles for K3) 26 * This code 27 is completely free for any use. 28 * Visit http://learn.sparkfun.com/products/2 29 for SIK information. 30 * Visit http://www.arduino.cc to learn about the Arduino. 31 32 * 33 * Version 2.0 6/2012 MDG 34 * Version 2.1 9/2014 BCH 35 *****************************************************************/ 36 37 38const 39 int SensorPin = A4; // switch to use a different analog sensor 40int sensorThreshold 41 = 500; // what sensor value will stop the song 42const int buzzerPin = 3; // 43 connect the buzzer to pin 3 (Thinkershield buzzer pin) 44int tempo = 270; // 45 The tempo is how fast to play the song (beats per minute). 46const int songLength 47 = 19; // sets the number of notes of the song 48 49// Notes is an array of text 50 characters corresponding to the notes 51// in your song. A space represents a rest 52 (no tone). Each note or rest must be separated by a comma 53// with no comma after 54 the last note. 55 56// It"s important that you use single quotes for single letters 57 like "b", "g" and the rest ' ', and 58// that you use double quotes for notes 59 requiring multiple letters like "g2" (which is the g in the octave above) 60// 61 and "f#" (which is the sharp - a semitone above "f") in the lower octave. Your 62 code may compile if these are 63// incorrect but those notes will not play. It 64 is a quirky part of the Arduino coding language in this IDE. 65// In Tinkercad, 66 it works differently and all notes should be enclosed in double quotes. 67 68char* 69 notes[songLength] = { 70 ' ', 71 "f#", "g#", 'b', 'b', ' ', 72 "d2#", 73 "f2#", "g2#", "g2#", ' ', 74 "g2#", "f2#", "f2#", "e2", ' ', 75 76 "e2", "d2#", "d2#"}; 77 78// beats[] is an array of values for each note. 79 A "1" represents a quarter-note, 80// "2" a half-note, and "4" a full-note. 81// 82 Don"t forget that the rests (spaces) need a length as well. 83 84int beats[songLength] 85 = { 86 2, 87 1, 1, 2, 3, 2, 88 1, 1, 2, 3, 2, 89 1.5, 0.5, 1, 3.5, 2, 90 91 1.5, 1, 3.5}; 92 93void setup() 94{ 95 pinMode(buzzerPin, OUTPUT); // 96 sets the buzzer pin as an OUTPUT 97 pinMode(SensorPin, INPUT); 98 //Serial.begin(9600); 99} 100 101void 102 loop() 103{ 104 int i, duration; 105 bool keepPlaying = true; 106 int value; 107 108 109 i=0; 110 value = analogRead(SensorPin); 111 //Serial.println(value); 112 113 keepPlaying = (value > sensorThreshold); 114 115 while ((i < songLength) && 116 keepPlaying) 117 { 118 //Serial.println("playing"); 119 duration = beats[i] 120 * tempo; // length of note/rest in ms 121 122 if (notes[i] == ' '){ // 123 is this a rest? 124 delay(duration); // then pause for a moment 125 126 //Serial.println("pause"); 127 } 128 else // 129 otherwise, play the note 130 { 131 tone(buzzerPin, frequency(notes[i]), 132 duration); 133 delay(duration); // wait for tone to finish 134 //Serial.println("note 135 is " + String(notes[i])); 136 } 137 138 delay(tempo/20); // 139 brief pause between notes 140 value = analogRead(SensorPin); 141 keepPlaying 142 = (value > sensorThreshold); 143 //Serial.println(value); 144 i = i+1; 145 146 } 147 148 //while(true){ 149 // We only want to play the song once, so we pause 150 forever 151 //} 152 153 /* If you'd like your song to play once only, uncomment 154 the 3 lines that make up the 155 while(true)statement above. When commented 156 out, the song will play repeatedly. */ 157} 158 159//------------------------------------------------------------------------------------------------------------------------- 160int 161 frequency(char* note) 162{ 163 int i; 164 const int numNotes = 25; // number 165 of notes we"re storing - includes the semitones over 2 octaves starting at middle 166 C 167 char* names[numNotes] = { 168 'c', "c#", 'd', "d#",'e', 'f', "f#", 169 'g', "g#", 'a', "a#", 'b', "c2", "c2#", "d2", "d2#", "e2", "f2", 170 "f2#", "g2", "g2#", "a2", "a2#", "b2", "c3" }; 171 int frequencies[numNotes] 172 = { 173 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 523, 554, 174 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 1047 }; 175 176 // Now we"ll 177 search through the letters in the array, and if 178 // we find it, we"ll return 179 the frequency for that note. 180 181 for (i = 0; i < numNotes; i++) // Step through 182 the notes 183 { 184 if (names[i] == note) // Is this the one? 185 { 186 187 return(frequencies[i]); // Yes! Return the frequency and exit function. 188 189 } 190 } 191 return(0); // We looked through everything and didn't find it, 192 193 // but we still need to return a value, so return 0. 194} 195
Downloadable files
Musical Treasure Box Circuit
This circuit has an LDR to sense the light level in the environment and a buzzer to play a tune. The potentiometer is included to encourage others to consider doing an extension of the project but is not used in this project. It can be left out if desired by removing the potentiometer and the three wires connected to it.
Musical Treasure Box Circuit
Comments
Only logged in users can leave comments