Description
For this assignment, I decided to create a musical coconut, inspired by music boxes. I had this really cool coconut shell holder that a friend gifted to me once and I decided to use it. I was hoping to figure out how to program a better song than Happy Birthday (the code has my attempt at Hey Jude commented out), but it was really hard to figure out how piano sheet music translates to notes, given that I have no training in reading music. The circuit was fairly simple since I hooked up the light sensor to the piezzo speaker such that the value of light on the sensor will determine the music that is played. There is an intro beat and that plays if the coconut is half opened. It switches to Happy Birthday when the coconut is fully open and the sensor is detecting more light. I taped the breadboard with the light sensor, and the piezzo speaker inside the coconut for the mechanical part.
My other idea was to create a similar setup and put into this chocolate tin that I own. In that case I'd have used alarm sounds because it would be an alarm indicating if someone opened my chocolate tin to steal my chocolates.
Components
1x Macbook Pro
1x Arduino
1x Breadboard
1x Piezzo Speaker
1x Light Sensor
5x Wires
1x 200 ohm resistor
Code
/* Happy Birthday Coconut
* -----------
*
*
* 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 potPin = 0; // select the input pin for the potentiometer
int potVal = 0; // variable to store the value coming from pot
int ledPin = 13;
int speakerOut = 7;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'B', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1073, 1014, 956};
byte intro[] = "8c8d8g8f8B8p8p8p";
byte birthday[] = "2c1p1c4d4c4f6e2p2c1p1c4d4c4g6f2p2c2c4C4a4f4e6d2p2B1p1B4a4f4g8f8p8p8p";
//byte judetest[]="4c4a2p2a4c4d4g4p4g4a4B4b4f1p4f4e4c4d4c4B4b4a";
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 40;
int statePin = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(speakerOut, OUTPUT);
Serial.begin(9600);
}
void play_melody(byte *melody){
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<9;count2++) {
if (names[count2] == melody[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
digitalWrite(speakerOut, 0);
delayMicroseconds(100);
}
}
}
}
}
void loop() {
digitalWrite(speakerOut, LOW);
potVal = analogRead(potPin); // read the value from pot, between 0 - 1024
Serial.println(potVal);
if (potVal > 256 && potVal < 512) {
play_melody(intro);
} else if (potVal >=
512) {
play_melody(birthday);
}
}
Video Link
https://www.facebook.com/photo.php?v=10153340503970065&comment_id=432991...
- Login to post comments