Sigmund Freud's Wild Ride

Submitted by m-craig on Tue, 03/12/2013 - 16:46

Description:

For this lab, I created a miniaturized carnival ride by mounting a paper plate and cup on top of a piece of cork used to secure the ride to the motor.  A finger puppet of Sigmund Freud (only thing I could find that wasn't too heavy for the motor) sits in the cup and enjoys the ride--while, presumably, analyzing its subconscious motivation for spinning him around.  Mounting the cork slightly off-center on the motor allows the plate to spin wildly without losing its equilibrium--in true (I hope) carnival fashion.

While the ride spins, three LEDs flash in quick patterns while a Piezo speaker plays the melody of Julius Fucik's "Entry of the Gladiators" (probably better known as "that circus song."  Original here: http://www.youtube.com/watch?v=_B0CyOAO8y0.)  A potentiometer controls the speed of the motor.  If you spin it too fast, an "emergency alarm" screeches and all three LEDs turn on to maximum brightness.  We can't have the ride giving Sigmund any new traumas to interpret, after all.

Unresolved Challenges:

1) How to get the LEDs to blink independently of the song.  Every code option I tried resulted in the lights either waiting until the end of the song to blink (and interrupting the song when they did) or slowing down the song's tempo by blinking after every note.
2) How to create a more realistic siren effect by having the speaker play two tones in rapid succession.  Every time I tried to create a multitone siren, the speaker would play the first tone followed by a click.  Delay commands made both tones audible but made the sound too choppy.

Components Used:

1 - Arduino UNO
1 - Solderless Breadboard
1 - DC Motor
1 - Transistor
1 - Diode
1 - 3V Batter Power Supply (to power motor)
1 - Piezo Speaker
3 - LEDs
3 - 220-ohm resistors
1 - Potentiometer
1 - USB Power Supply (to power everything else)
1 - Small cross-section of a wine cork
1 - Paper plate
1 - Paper drinking cup
1 - Sigmund Freud finger puppet

Code:


int potPin = 0;   // select the input pin for the potentiometer
int motorPin = 10; // select the pin for the Motor
int length = 44; // the number of notes
char notes[] = "CbZbaYXWVgaXWXWVfeUegffTdgffTdbCSDREFQGOABAPCbZbaY";
              // Melody of "Entry of the Gladiators"
int beats[] = { 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1,};
               // Number of beats for each note.
int tempo = 80;
int ledpin1 = 13;
int ledpin2 = 11;
int ledpin3 = 9;
int speakerpin = 7;
int potval = 0;      // variable to store the value coming from the sensor
int speakerval = 0;

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[] ={ 'c', 'T', 'd', 'U', 'e', 'f', 'V', 'W', 'g', 'X', 'Y', 'a', 'Z', 'b', 'C', 'S', 'D', 'R', 'E', 'F', 'Q', 'P', 'G', 'O', 'A', 'B'};  
  int tones[] = { 3830, 3780, 3400, 3350, 3038, 2864, 2810, 2600, 2550, 2500, 2340, 2272, 2200, 2028, 1988, 1915, 1875, 1700, 1519, 1490, 1450, 1430, 1410, 1300, 1275, 1240, 1136, 1014};
   for (int i = 0; i < 26; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

void lights(){   // controls the behavior of LEDs while the motor turns
      digitalWrite(ledpin1, LOW);
      digitalWrite(ledpin2, LOW);
      digitalWrite(ledpin3, LOW);
      delay(2);
      digitalWrite(ledpin1, HIGH);
      digitalWrite(ledpin2, LOW);
      digitalWrite(ledpin3, LOW);
      delay(2);
      digitalWrite(ledpin1, LOW);
      digitalWrite(ledpin2, HIGH);
      digitalWrite(ledpin3, LOW);
      delay(2);
      digitalWrite(ledpin1, LOW);
      digitalWrite(ledpin2, LOW);
      digitalWrite(ledpin3, HIGH);
      delay(2);
      digitalWrite(ledpin1, LOW);
      digitalWrite(ledpin2, LOW);
      digitalWrite(ledpin3, LOW);
      delay(2);
      digitalWrite(ledpin1, LOW);
      digitalWrite(ledpin2, LOW);
      digitalWrite(ledpin3, LOW);
      delay(2);
      digitalWrite(ledpin1, HIGH);
      digitalWrite(ledpin2, LOW);
      digitalWrite(ledpin3, LOW);
      delay(2);
      digitalWrite(ledpin1, LOW);
      digitalWrite(ledpin2, HIGH);
      digitalWrite(ledpin3, LOW);
      delay(2);
      digitalWrite(ledpin1, LOW);
      digitalWrite(ledpin2, LOW);
      digitalWrite(ledpin3, HIGH);
      delay(2);
      digitalWrite(ledpin1, LOW);
      digitalWrite(ledpin2, LOW);
      digitalWrite(ledpin3, LOW);
      delay(2);
}
      
 
void setup() {
  Serial.begin(9600);
  pinMode(speakerpin, OUTPUT);
  pinMode(ledpin1, OUTPUT);
  pinMode(ledpin2, OUTPUT);
  pinMode(ledpin3, OUTPUT);
}
void loop() {
  potval = analogRead(potPin); 
  Serial.println(potval);
  analogWrite(motorPin, potval/4); // analogWrite can be between 0-255
  if(potval>900){
                // Sounds the "emergency alarm" if motor starts spinning too fast
      digitalWrite(ledpin1, HIGH);
      digitalWrite(ledpin2, HIGH);
      digitalWrite(ledpin3, HIGH);
      tone(speakerpin, 3000, 200);
}
  else{
  for (int i = 0; i < length; i++) {  // Plays the song
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      lights();  // Flashes the LEDs while playing the song
      playNote(notes[i], beats[i] * tempo);
    }
    // pause between notes
    delay(tempo / 2);
  }
  }
}

lab6a.JPG
lab6b.JPG
lab6c.JPG
lab6d.JPG
0
Your rating: None
Drupal theme by Kiwi Themes.