Lab6 - Super Mario mini turntable

Submitted by chan on Tue, 03/12/2013 - 02:23

 

Description

This is a mini turntable for playing Super Mario theme song. It consists of 3 parts: play button (FSR), power(DC motor), and brightness reading part (photocell). Basic idea is as soon as you push a FSR over a certain threshold, DC motor will start. The motor drives CD disk rotated so that the surface of the disk reflects ambient lights enough to play music. The reflected light will be received by a photocell. 
 
 
Components Used

1- Arduino

1- Piezo speaker

1- Photocell

1- FSR

2-10kΩ Resistors

1- DC motor

 
 

Code

 

/*

* Super Mario mini turntable
* Eungchan Kim
* 2013-03-12
* SuperMario Sound from http://www.youtube.com/watch?v=VqeYvJpibLY
* http://www.arduino.cc/en/Tutorial/Melody
*/
    
int speakerPin = 7;
int motorPin = 8; // select the pin for the Motor
int photoPin = A0; 
int FSRPin=A1; // play button
 
int val =0;
int motorval= 0;
int threshold = 500; // for photocell
 
int length = 295; // the number of notes
char notes[] = "EE E CE G  g  C  g e  a b ia gEGA FG E CDb C  g e  a b ia gEGA FG E CDb  GNFR E uaC aCD GNFR E 1 11   GNFR E uaC aCD L  D C   CC C CD EC ag  CC C CDE  CC C CD EC ag  EE E CE G  g  C  g e  a b ia gEGA FG E CDb C  g e  a b ia gEGA FG E CDb EC g u aF Fa  bAAAGFEC ag  EC g u aF Fa  bF FFEDCe ec  "; // a space represents a rest
float beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, //Page 1
                2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 4, //Page 2
                1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, //Page4
                1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, //Page 5
                1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1.3, 1.3, 1.3, 1.3, 1.3, 1.3, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1.3, 1.3, 1.3, 1, 1, 1, 1, 1, 1, 2 }; //Page 6
                
int tempo = 95;
 
void playTone(int ton1, int duration) {
  for (long i = 0; i < duration * 1000L; i += ton1) {
    tone(speakerPin, ton1);
    delayMicroseconds(ton1);
  }
  noTone(speakerPin);
}
 
void playNote(char note, int duration) {
//                        c    c#   d    d#   e    f    f#   g    g#   a    a#   b
  char names[] = { ' ',  '!', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B', 'i', 'N', 'R', 'u',  '1', 'L', 'k'}; // [i = b flat] [N = G flat] [R = D#] [u = g#] [1 = C oct. 5] [L = E flat]
  int tones[] =  {   0, 1046, 138, 146, 155, 164, 174, 184, 195, 207, 220, 233, 246, 261, 293, 329, 349, 391, 440, 493, 523, 587, 659, 698, 783, 880, 987, 466, 740, 622, 415, 1046, 622u, 227};
  
  // play the tone corresponding to the note name
  for (int i = 0; i < 34; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}
 
void setup() {
  pinMode(speakerPin, OUTPUT);
  pinMode(motorPin, OUTPUT);
  pinMode(photoPin, INPUT);
  pinMode(FSRPin,INPUT);
  digitalWrite(LED[0],HIGH);
  Serial.begin(9600);
}
 
void loop() {
 
  for (int i = 0; i < length; i++) {
    motorval = analogRead(FSRPin);
    Serial.println(motorval);
    
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      val = analogRead(photoPin);
      Serial.println(val);
      if (val > threshold){
        playNote(notes[i], beats[i] * tempo);
      }
      analogWrite(motorPin, motorval*0.25); 
    }
    
    // pause between notes
    delay(tempo / 2); 
  }
}

Demo

 

0
Your rating: None
Drupal theme by Kiwi Themes.