Description
When I was little, I loved music boxes, because I thought they were a toy that offered some interaction. Twist the top, and then let the melody play. There was both a visual and an audio output. The ballerina would twist in pirouettes to the tune of the Nutcracker. In this lab, I decided to create my own using a Little Mermaid Theme.
The dolphin can bob back and forth a bit, but when pressed, the melody will speed up faster. If the melody is too distracting, someone can twist the potentiometer to turn down the sound. There is a mix of red and blue light in the bottom layer of the box that blinks to the melody.
Materials
Code
int potPin = A0;
int ledPin = 13;
int fsrPin = A1;
int ledPin2 = 12;
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[] = "2d1p2e1p2f1p5f5p2e1p2f1p2g1p5g5p1p2d1p2e1p2f1p5f8p2e1p2f1p2e1p2f1p2g1p2g1p5g5p2e1p2f1p5g1p2f1p2g1p5f2d1p2f1p2g1p2a1p2a1p4g8p";
// 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 =62;
int statePin = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(potPin, INPUT);
pinMode(ledPin2, OUTPUT);
pinMode(speakerOut, OUTPUT);
}
void loop() {
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(ledPin,HIGH);
digitalWrite(ledPin2, HIGH);
//delayMicroseconds(analogRead(potPin));
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
digitalWrite(ledPin,LOW);
digitalWrite(ledPin2, LOW);
//delayMicroseconds(analogRead(potPin));
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
Serial.println(analogRead(potPin));
delayMicroseconds(analogRead(fsrPin));
}
}
}
}
}