Assignment: Input / Output Coincidence Lab Assignment
Collaborators:
Description
I made a music jewelry box that plays music when it is opened. The tempo of the song also changes as more jewelry is put in the box. The more jewelry there is, the slower the song is.
Components
1 photocell
1 FSR
2 10K resistors
1 Piezo buzzer
1 jewelry box
Arduino Code
int sensorPin = 0; // select the input pin for the photocell
int speakerOut = 7; // select the output pin for speaker
int fsrPin = 3; //select input pin for FSR
int val = 0;// variable to store the value coming from the photocell
int fsr = 0; // variable to store the value coming from FSR
#define ARR_SIZE 86
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
//twinkle, twinkle little star
byte melody[] = "2c2c2g2g2a2a8g2f2f2e2e2d2d8c2g2g2f2f2e2e8d2g2g2f2f2e2e8d2c2c2g2g2a2a8g2f2f2e2e2d2d8c8p";
int count = 0;
int count2 = 0;
int count3 = 0;
void setup() {
Serial.begin(9600);
pinMode(speakerOut, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(fsrPin, INPUT);
digitalWrite(speakerOut, LOW);
}
void loop()
{
val = analogRead(sensorPin);
fsr = analogRead(fsrPin);
//make sure to check that jewelry box is still open
if(val > 100) {
for(count=0; count < ARR_SIZE/2; count++) {
for(count2=0; count2<8; count2++) {
val = analogRead(sensorPin);
//look for the corresponding note in the melody
if(names[count2] == melody[count*2+1] && val > 100)
{
Serial.println(names[count2]);
val = analogRead(sensorPin);
//make sure to check that jewelry box is still open
if (val > 100) {
fsr = analogRead(fsrPin);
//play the note
for(int i=0; i<int(melody[count*2])*(fsr/100);i++) {
digitalWrite(speakerOut, HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
}
}
//make sure to check that jewelry box is still open
val = analogRead(sensorPin);
//pause
if(melody[count*2+1] == 'p' && val > 100)
{
digitalWrite(speakerOut, LOW);
fsr = analogRead(fsrPin);
delay(int(melody[count*2])*(fsr/100));
}
}
}
}
}
Video