After completing two activities with the Piezo Buzzer, I was brainstorming for ideas of input output coincidence. I found a hand shape wooden carving in my shrine at home. I got the idea of turning this carving to a music box. Whenever I open the hand and expose the inside, it starts music.
I started to extending the wires connected to a photosensor installed inside the carving. I attached the Piezo Buzzer at the bottom of the carving. Then, I coded a program which play a music only when input value from the photosensor reaches a certain level.
The video can be seen in the following link:
http://www.youtube.com/watch?v=UqKMaiOJZQU
Components used:
1- Arduino Uno
1- Breadboard
1- 10Kohms resistor
1-Piezo Buzzer
1-Photosensor
9- Connecting wires
Arduino code:
/* initial code from http://www.arduino.cc/en/Tutorial/Melody
*/
int potPin = 0;
int speakerPin = 7;
int val = 0;
int length = 22; // the number of notes
char notes[] = "eedccddfedcggfeeedfedc "; // a space represents a rest
int beats[] = { 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 4};
int tempo = 300;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 23; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
Serial.println("ready");
}
void loop() {
val = analogRead(potPin);
Serial.println(val);
if (val>100){
play();
}
}
void play(){
for (int i = 0; i < length; i++) {
val = analogRead(potPin);
Serial.println(val);
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
}
else {
if (val>100){
playNote(notes[i], beats[i] * tempo);
}
}
// pause between notes
delay(tempo / 2);
}
}
- Login to post comments