Description
For this project, I cut a hole in the bottom of a box for playing cards, and placed the box over a photocensor acting as a potentiometer to tell a piezo speaker (also in the card box) to begin playing a pre-programmed melody when a certain amount of light hits the photocell (ie, when the card box is opened). I'm especially proud of the code, which I figured out on my own by looking just at the sample codes we've been given for our assignments. I was able to modify the "Play_Melody" sample code so that the melody, rather than looping to insanity, would only play when an input device produced a value greater than a certain number (in this case, I chose 240 as the photocell was too easily activated with much less). The melody loops as long as the value is that high, and stops looping when the value drops below that number. The full embodiment (if that's what this really is) comes from the idea that this box plays music when opened (totally original, I know). Modified, one could easily imagine a nasty sunrise-activated alarm clock coming into shape (and then being thrown at the wall like all the others).
Components Used
I used an empty box of cards, a photocell resistor, a piezo speaker, and two resistors (one to reduce the sound of the piezo speaker and the other for the photocell, noteably the orange-orange-brown-gold resistor (using the 10k resistor suggested in lab 4 rendered my photocells useless).
Code
/* 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 val = 0;
int potPin = 0;
int ledPin = 13;
int speakerOut = 7;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2a2p3c2p2a2p3c2p2d2p2g2p3c2p2g2p3c2p2d2p4f2p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// 10 20 30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(speakerOut, OUTPUT);
}
void loop() {
val = analogRead (potPin);
if(val > 240) {
digitalWrite(speakerOut, LOW);
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<8;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') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
}
}
Visual Documentation
Components: Piezo in the box, photocell exposed
Assembled: Box Closed
In Action: Opened and Sounding Off