Lab 5: Climbing game

Submitted by shaohan on Wed, 03/06/2013 - 00:25

 

Description

 
I created a small music game by using 3 photocells and the piezo speaker. When the snake climbs up to a certain location, a special tone will be played. If the snake climbs to the top, a full song will be played.
 
 

Components

3- Photocells
1- Arduino Uno
1- Breadboard
1- Piezo speaker
3- 10kΩ Resistors
1- 220kΩ Resistors
Lots of wires
 

Code

 
// Arduino Codes //
 
/* Sound Serial (aka Keyboard Serial)
 * ------------
 *
 * Program to play tones depending on the data coming from the serial port.
 *
 * The calculation of the tones is made following the mathematical
 * operation:
 *
 *       timeHigh = 1/(2 * toneFrequency) = period / 2
 *
 * where the different tones are described as in the table:
 *
 * note frequency period PW (timeHigh)
 * c        261 Hz        3830 1915
 * d        294 Hz        3400 1700
 * e        329 Hz        3038 1519
 * f        349 Hz        2864 1432
 * g        392 Hz        2550 1275
 * a        440 Hz        2272 1136
 * b        493 Hz        2028 1014
 * C        523 Hz        1912 956
 *
 * (cleft) 2005 D. Cuartielles for K3
 *
 * Updated by Tod E. Kurt <tod@todbot.com> to use new Serial. commands
 * and have a longer cycle time.
 *
 */
 
int speakerPin = 7;
int aPin = 0;
int aVal = 0;
 
int bPin = 2;
int bVal = 0;
 
int cPin = 5;
int cVal = 0;
 
int beep = 0;
 
 
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "4e4e4f4gp24g4f4e4dp24c4c4d4ep22e2ep22dp24d";
 
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
 
 
void setup() {
  pinMode(speakerPin, OUTPUT); 
  Serial.begin(9600);
}
 
void loop() {
  digitalWrite(speakerPin, LOW); 
  
  aVal = analogRead(aPin);
  bVal = analogRead(bPin);
  cVal = analogRead(cPin);
  
  Serial.print("aVal: ");
  Serial.print(aVal);
  Serial.println(","); 
  
  Serial.print("bVal: ");
  Serial.print(bVal);
  Serial.println(","); 
 
  Serial.print("cVal: ");
  Serial.print(cVal);
  Serial.println(","); 
  
  
if ( cVal < 100) {  // play 'c' if value is more than 780
    beep = 1915;
      for( int i = 0; i < 50; i++ ) {   
        digitalWrite(speakerPin, HIGH);
        delayMicroseconds(beep);
        digitalWrite(speakerPin, LOW);
        delayMicroseconds(beep);
        
      }     
    }
  
 if ( bVal < 580) {  // play 'd' if value is more than 480
    beep = 1700;
      for( int i = 0; i < 50; i++ ) {   
        digitalWrite(speakerPin, HIGH);
        delayMicroseconds(beep);
        digitalWrite(speakerPin, LOW);
        delayMicroseconds(beep);        
      }     
    }
  
 if ( aVal < 780 ) {  // play a song if value is more than 100
    for (count = 0; count < MAX_COUNT; count ++ ) {
        for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3 ++ ){
         for (count2 = 0; count2 < 8; count2 ++ ){
          if (names[count2] == melody[count*2 + 1]) {
           digitalWrite(speakerPin, HIGH);
           delayMicroseconds(tones[count2]);
           digitalWrite(speakerPin, LOW);
           delayMicroseconds(tones[count2]);
          }
          
          if (melody[count*2 + 1] == 'p') {
              digitalWrite(speakerPin, 0);
              delayMicroseconds(500);
           
      }     
    } 
   }
  }
 }
}
 
 
photo (1).JPG
photo.JPG
0
Your rating: None
Drupal theme by Kiwi Themes.