DESCRIPTION
In this lab, we used a piezo buzzer to play a set melody and play notes using our keyboard. Then, we attached a photocell to our circuit to make a Theremin.
For homework, I designed music box which plays "London Bridge is Falling Down." The song will play when the top of the dial is touched. The song will keep repeating itself until the top of the dial is released. At that time, the song will finish playing then stop. The dial can be turned to control the volume of the song.
Turning the song on/off is controlled by an FSR attached to the top of the dial. The volume control consists of a pot pressed into the bottom of a dial/knob. All volume control is based on the resistance to the piezo buzzer from the pot. No code was written for this. Instead, only 2 of the 3 wires on the pot were used as a variable resistance control. The actual "music box" is a Yoplait yogurt cup with a hole cut in the side for the piezo buzzer wires and pot wires to come out. The piezo buzzer was placed at the bottom of the cup and the cup was filled with bubble wrap to raise the pot so the dial could sit above the cup. The piezo buzzer is underneath the bubble wrap to lower the volume of the buzzer because it was pretty loud in my apartment.
COMPONENTS USED
1- Arduino Uno (plus laptop and USB cord)
1- piezo buzzer
1- 10 kΩ Resistor
1- Breadboard
1- Pot
1- FSR
22 gauge wire
CODE
//The following code, which was provided in class, has been modified to play "London Bridge is Falling Down" when an FSR is touched. Once the FSR is released, the song finishes then stops playing until the FSR is touched again.
//The circuit (not the code) was modified so a pot controls the volume
/* Play Melody
* -----------
*
* Program to play melodies stored in an array, it requires to know
* about timing issues and about how to play tones.
*
* The calculation of the tones is made following the mathematical
* operation:
*
* timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note frequency period PW (timeHigh)
* c 261 Hz 3830 1915
* d 294 Hz 3400 1700
* e 329 Hz 3038 1519
* f 349 Hz 2864 1432
* g 392 Hz 2550 1275
* a 440 Hz 2272 1136
* b 493 Hz 2028 1014
* C 523 Hz 1912 956
*
* (cleft) 2005 D. Cuartielles for K3
*/
int sensorPin = A0; // select the input pin for the sensor
int val = 0; // variable to store the value coming from the sensor
int speakerOut = 7;
byte names[] = {'o', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2g2a2g2f2e2f4g2d2e4f2e2f4g2g2a2g2f2e2f4g4d4g4e4c";
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
void setup() {
pinMode(speakerOut, OUTPUT);
}
void loop() {
val = analogRead(sensorPin); // read the value from the sensor, 0-1023
if (val>150) {
digitalWrite(speakerOut, LOW);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
}
}
}
}
}
IMAGES
2 different views of my artifact integrated with my circuit are attached. The wires coming out of the music box are for the piezo buzzer (red/black thin wires) and pot (black/yellow thick wires).
- Login to post comments