Assignment: Input / Output Coincidence Lab Assignment
Collaborators:
Description:
This project features a stuffed teddy-bear with potentiometers hooked up to its eyes and a piezeo speaker embedded in its head. The speaker plays "Twinkle, Twinkle, Little Star" on loop.
Twisting the bear's left eye causes the tempo of the song to increase or decrease.
Twisting the bear's right eye causes the pitch to increase or decrease.
Materials:
Potentiometer X 2
Piezo Speaker
Ill-Fated Teddy Bear
Arduino Board
Wires
Arduino Code:
/*
* Song Bear
* Plays "Twinkle Twinkle Little Star
* Twisting Song Bear's left eye makes the song play faster or slower
* Twisting Song Bear's right eye makes the pitch of the tune higher or lower
*/
int speedPin = 0; // Use pin 0 to control the speed of the song (impacts how long a note is held, and how long there is between notes)
int freqPin = 1; // use pin 1 to control the frequency of the piezo.
int speakerPin = 7; // use pin 7 to output the noise
int speedRead; //speedRead holds the value of the speedPin
int freqRead; //freqRead holds the value of the frequency pin
int k = 0;
float mod2;
float mod3;
float i = 0;
float mod;
void setup() {
pinMode(speakerPin, OUTPUT);
}
int melody[] = {1915, 1915, 1275, 1275, 1136, 1136, 1275, 1432, 1432, 1519, 1519, 1700, 1700, 1915, 1275, 1275, 1432, 1432, 1519, 1519, 1700, 1275, 1275, 1432, 1432, 1519, 1519, 1700, 1915, 1915, 1275, 1275, 1136, 1136, 1275, 1432, 1432, 1519, 1519, 1700, 1700, 1915};
float cycles = 150.0;
void loop () {
while (1) {
for(int j=0; j<42; j++) { //we have 42 notes to loop through
i = 0;
k++; //k keeps track of measures. Every seventh note gets doubled.
freqRead = analogRead(freqPin); //read in the values from the eyes
speedRead = analogRead(speedPin);
mod = freqRead / 100.0; //manipulate the data such that we get a range of roughly 1-10
mod++; //add one so we don't ever divide by zero
mod2 = speedRead / 100.0;
mod2++;
mod3 = melody[j]/1915.0; //mod3 adjusts the timing of the cycles so that higher frequency notes get played the same length as lower frequency notes
if (k == 7) //every seventh note of the song needs to get played twice as long
cycles = cycles*2;
}
for(i=i; i<cycles/(mod*mod2*mod3); i++) { //This for loop repreesnts one note getting played.
//higher frequency reading results in lower frequency and longer cycles
//requires reducing number of cycles
//reducing number cycles reduces processing time, which makes it run faster than it should
digitalWrite(speakerPin,HIGH);
delayMicroseconds(melody[j]*mod);
digitalWrite(speakerPin, LOW);
delayMicroseconds(melody[j]*mod);
}
digitalWrite(speakerPin,0);
for(i=0; i<150/mod2; i++) { //this is a pause between notes
delayMicroseconds(500000000);
}
if (k==7) { //once we've played our note double, we need to go back to playing them single
k=0;
cycles = cycles/2;
}
}
digitalWrite(speakerPin,0); //every run throug the song ends in a nice long delay before it starts back up
for(i=0; i<400/mod2; i++) {
delayMicroseconds(500000000);
}
}
}
Pictures: