Assignment: DC Motor: Actuation Assignment 1
Collaborators: jin0305
Assignment: DC Motor: Actuation Assignment 1
Collaborators:
Description
Drawing inspiration from our trip to the Experience Music Project in Seattle over the weekend, Jin and I created a DIY Jukebox for the DC Actuation Assignment. The Jukebox plays two songs in varying tempos. As the tempo changes, the speed of the disk rotation also changes correspondingly.
We used two FSRs to select the song to play and a potentiometer to change the tempo of the song. A DC motor rotates the cardboard "play disc." We used Play Doh for the button. We fixed the FSRs beneath the Play Doh buttons, so when a button was pressed, the corresponding song would be played. The Potentiometer could be turned to increase/decrease the tempo of the song. As the tempo changes, the user gets a visual (increase in speed of rotation) and an audio (an obvious increase in tempo) feedback. We also made legends for the user to associate button color with a song. Our songs were nursery rhymes that played from a Piezo speaker. Even though the current version plays only two songs, there is a provision to play more.
Components
Arduino
2 FSRs
Potentiometer
DC Motor
Piezo Speaker
Battery
Play Doh
Cardboard
Cork
Water color
Arduino Code
/*
Created by JinYoung Baik and Janani Vasudev
12 October 2009
*/
int fsr1Pin = 0; // select the input pin for the FSR 1
int fsr2Pin = 1; // select the input pin for the FSR 2
int fsr3Pin = 2; // select the input pin for the FSR 3
int motorPin = 9; // select the pin for the Motor
int potPin = 3; // select the input pin for the potentiometer
int fsr1val = 0; // variable to store the value coming from FSR 1
int fsr2val = 0; // variable to store the value coming from FSR 2
int fsr3val = 0; // variable to store the value coming from FSR 3
int potval = 0; // variable to store the value coming from the sensor
int speakerPin = 7; //piezo speaker
int tone1Length = 25; // the number of tone1 notes
char tone1Notes[] = "gagfefgdefefggagfefgdgec "; // a space represents a rest
int tone1Beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 4 };
int tone1Tempo1 = 600;
int tone1Tempo2 = 300;
int tone1Tempo3 = 150;
int tone2Length = 32; // the number of tone2 notes
char tone2Notes[] = "geggegeggeggeaggegfedcdegfedcde ";
int tone2Beats[] = { 2, 2, 1, 1, 2, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tone2Tempo1 = 500;
int tone2Tempo2 = 250;
int tone2Tempo3 = 125;
/*int tone3Length = 27; // the number of tone3 notes
char tone3Notes[] = "cccegecdddBAGcccegecddGABcc ";
int tone3Beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 2, 4 };
int tone3Tempo1 = 75;
int tone3Tempo2 = 150;
int tone3Tempo3 = 350;*/
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
void setup()
{
pinMode(speakerPin , OUTPUT);
Serial.begin(9600);
}
void playTone(int tone, int duration)
{
for (long i = 0; i < duration * 1000L; i += tone * 2)
{
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration)
{
char names[] = {'G', 'A', 'B', 'c', 'd', 'e', 'f', 'g', 'a'};
int tones[] = {2560, 2311, 2090, 1915, 1700, 1519, 1432, 1275, 1136};
// play the tone corresponding to the note name
for (int i = 0; i <= 9; i++)
{
if (names[i] == note)
{
playTone(tones[i], duration);
}
}
}
void loop()
{
fsr1val = analogRead(fsr1Pin); // read the value from FSR1, between 0 - 1024
fsr2val = analogRead(fsr2Pin); // read the value from FSR2, between 0 - 1024
//fsr3val = analogRead(fsr3Pin); // read the value from FSR3, between 0 - 1024
//Serial.println(fsr3val);
potval = analogRead(potPin);
if(potval ==0)
{
Serial.println();
Serial.print("Potentiometer Value:");
Serial.print("\t");
Serial.print(potval);
Serial.println();
Serial.println("FSR 1 Value:");
Serial.print("\t");
Serial.print(fsr1val);
Serial.println();
Serial.println("FSR 2 Value:");
Serial.print("\t");
Serial.print(fsr2val);
Serial.println();
Serial.println("Play No tune!");
}
if(potval > 0 && potval < 341)
{
Serial.println("In Potval >0 && Potval < 341");
Serial.println();
Serial.print("Potentiometer Value:");
Serial.print("\t");
Serial.print(potval);
Serial.println();
analogWrite(motorPin, 50); // analogWrite can be between 0-255
if (fsr1val > 50 && fsr2val ==0 && fsr3val==0)
{
Serial.println("Play Song 1 in Tempo 1!");
Serial.print("FSR 1 Val:");
Serial.print("\t");
Serial.print(fsr1val);
// Play Start
for (int i = 0; i < tone1Length; i++)
{
if (tone1Notes[i] == ' ')
{
delay(tone1Beats[i] * tone1Tempo1); // rest
}
else
{
playNote(tone1Notes[i], tone1Beats[i] * tone1Tempo1);
}
delay(tone1Tempo1 / 2); // pause between notes
}
}
if(fsr2val > 50 && fsr1val ==0 && fsr3val ==0)
{
Serial.println("Play Song 2 in Tempo 1!");
Serial.print("FSR 2 Val:");
Serial.print("\t");
Serial.print(fsr2val);
// Play Start
for (int i = 0; i < tone2Length; i++)
{
if (tone2Notes[i] == ' ')
{
delay(tone2Beats[i] * tone2Tempo1); // rest
}
else
{
playNote(tone2Notes[i], tone2Beats[i] * tone2Tempo1);
}
delay(tone2Tempo1 / 2); // pause between notes
}
}
}
else if (potval >=341 && potval < 682)
{
Serial.println("In Potval >= 341 && Potval < 682");
Serial.println();
Serial.print("Potentiometer Value:");
Serial.print("\t");
Serial.print(potval);
Serial.println();
analogWrite(motorPin, 250); // analogWrite can be between 0-255
if (fsr1val > 50 && fsr2val ==0 && fsr3val==0)
{
Serial.println("Play Song 1 in Tempo 2");
Serial.print("FSR 1 Val:");
Serial.print("\t");
Serial.print(fsr1val);
// Play Start
for (int i = 0; i < tone1Length; i++)
{
if (tone2Notes[i] == ' ')
{
delay(tone2Beats[i] * tone1Tempo2); // rest
}
else
{
playNote(tone1Notes[i], tone1Beats[i] * tone1Tempo2);
}
delay(tone1Tempo2 / 2); // pause between notes
}
}
if(fsr2val > 50 && fsr1val ==0 && fsr3val ==0)
{
Serial.println("Play Song 2 in Tempo 2!");
Serial.print("FSR 2 Val:");
Serial.print("\t");
Serial.print(fsr2val);
// Play Start
for (int i = 0; i < tone2Length; i++)
{
if (tone2Notes[i] == ' ')
{
delay(tone2Beats[i] * tone2Tempo2); // rest
}
else
{
playNote(tone2Notes[i], tone2Beats[i] * tone2Tempo2);
}
delay(tone2Tempo2 / 2); // pause between notes
}
}
}
else if (potval >= 682 && potval <= 1023)
{
Serial.println("In Potval >=682 && Potval <= 1023");
Serial.println();
Serial.print("Potentiometer Value:");
Serial.print("\t");
Serial.print(potval);
Serial.println();
analogWrite(motorPin, 700); // analogWrite can be between 0-255
if (fsr1val > 50 && fsr2val ==0 && fsr3val==0)
{
Serial.println("Play Song 1 in Tempo 3");
Serial.print("FSR 1 Val:");
Serial.print("\t");
Serial.print(fsr1val);
// Play Start
for (int i = 0; i < tone1Length; i++)
{
if (tone2Notes[i] == ' ')
{
delay(tone2Beats[i] * tone1Tempo3); // rest
}
else
{
playNote(tone1Notes[i], tone1Beats[i] * tone1Tempo3);
}
delay(tone1Tempo3 / 2); // pause between notes
}
}
if(fsr2val > 50 && fsr1val ==0 && fsr3val ==0)
{
Serial.println("Play Song 2 in Tempo 3!");
Serial.print("FSR 2 Val:");
Serial.print("\t");
Serial.print(fsr2val);
// Play Start
for (int i = 0; i < tone2Length; i++)
{
if (tone2Notes[i] == ' ')
{
delay(tone2Beats[i] * tone2Tempo3); // rest
}
else
{
playNote(tone2Notes[i], tone2Beats[i] * tone2Tempo3);
}
delay(tone2Tempo3 / 2); // pause between notes
}
}
}
/*else if (fsr3val > 50 && fsr1val ==0 && fsr2val == 0)
{
analogWrite(motorPin, 700); // analogWrite can be between 0-255
//play code
Serial.println("Play Song 3!");
// Play Start
for (int i = 0; i < tone3Length; i++)
{
if (tone3Notes[i] == ' ')
{
delay(tone3Beats[i] * tone3Tempo1); // rest
} else
{
playNote(tone3Notes[i], tone3Beats[i] * tone3Tempo1);
}
delay(tone3Tempo1 / 2); // pause between notes
}
}*/
}
Video Link
http://www.youtube.com/watch?v=UyLXPLV64Do