Gonna Get Ya

Posted by arielhaney

arielhaney's picture

Video: http://www.youtube.com/watch?v=N64cGSj56rs

 

Description:

I wanted to create something using the photocell. I finally decided on creating a backpack that when opened plays ominous music. And there is no music more ominous that the Jaws theme. As you open your backpack and more light comes into the backpack the music plays faster (and more ominously). The music has three speeds that are determined by how much light the photocell receives. When the photocell doesn't receive any light the music stops. 

Materials

  • Arduino Uno Board
  • 10k Resistors
  • Wires/Board
  • Photocell
  • Backpack

Arduino code:

 /* Play Melody

 * -----------
 *
 * Program to play melodies stored in an array, it requires to know
 * about timing issues and about how to play tones.
 *
 * 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
 */
 
int sensorPin = 3;    // select the input pin for the photosensor
int speakerOut = 7; 
int sensorVal = 0;
int val = 0; 
 
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody1[] = "10e10f10e10f";
byte melody2[] = "8e8f8e8f";
byte melody3[] = "6e6f6e6f";
byte melody4[] = "2e2f2e2f";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//    10                  20                  30
 
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 48; //the longest song is 32 counts so this is also 32
int statePin = LOW;
 
void setup() {
  Serial.begin(9600);
  pinMode(speakerOut, OUTPUT);
}
 
void loop() {
  val = analogRead(sensorPin);    // read the value from the sensor, between 0 - 1024
  Serial.println(val);
  sensorVal = analogRead(sensorPin); // read the value of the sensor
  if (sensorVal > 0 && sensorVal < 150){ // if light is hitting the sensor then play.
   for (count = 0; count < MAX_COUNT; count++) { // this increments by one so it will stop
    for (count3 = 0; count3 <= (melody2[count*2] - 48) * 30; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody2[count*2 + 1]) {       
          digitalWrite(speakerOut,HIGH);
          delayMicroseconds(tones[count2]);
          digitalWrite(speakerOut, LOW);
          delayMicroseconds(tones[count2]);
        } 
      }
    }
  }
}
  
  else if (sensorVal > 150 && sensorVal< 200){ // if more light gets faster
    for (count = 0; count < MAX_COUNT; count++) { // this increments by one so it will stop
    for (count3 = 0; count3 <= (melody3[count*2] - 48) * 30; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody3[count*2 + 1]) {       
          digitalWrite(speakerOut,HIGH);
          delayMicroseconds(tones[count2]);
          digitalWrite(speakerOut, LOW);
          delayMicroseconds(tones[count2]);
        } 
      }
    }
  }
}
 
  else if (sensorVal > 200){ // if more light gets faster
     for (count = 0; count < MAX_COUNT; count++) { // this increments by one so it will stop
    for (count3 = 0; count3 <= (melody4[count*2] - 48) * 30; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody4[count*2 + 1]) {       
          digitalWrite(speakerOut,HIGH);
          delayMicroseconds(tones[count2]);
          digitalWrite(speakerOut, LOW);
          delayMicroseconds(tones[count2]);
      }
    }
  }
}
}
 
  else if (sensorVal == 0){ // need this to turn off loop
      digitalWrite(speakerOut, 0);
      return;
      }
}
0
Your rating: None