Lab 5

carlos.sandoval's picture

Reads a specific sequence of notes. Based on how fast you hit a surface next to a photo sensor, the code speeds or slows the tempo of the melody. A green LED turns on with fast tempo, and a red LED turns on with slower tempo. The "motion" sensor consists of a beam of light projected directly to the photo sensor.

 

Materials:

-Photosensor

-Cardboards

-Tape

-Headlamp

-LEDs

-Piezo Speaker

 

/*
 *
 *
 * Program to play tones depending on the data coming from the serial port. The tempo of the tones varies
 *depending on how fast you move your hands around the photo sensor.

*Fast tempo turns an LED green, Slower tempo turns an LED Red.

*
 * 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 ledPin = 13;
int speakerPin = 7;
int beatRate = 0;     // select input pin for "beat rate" sensor
// Output
int redPin   = 11;   // Red LED,   connected to digital pin 11
int greenPin = 10;  // Green LED, connected to digital pin 10
// Program variables
int redVal   = 1;
int greenVal = 1;

// note names and their corresponding half-periods  
byte names[] ={ 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
 
int serByte = -1;
int ledState = LOW;
int count = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(speakerPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("ready");
   pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   


    Serial.begin(9600);  // ...set up the serial ouput on 0004 style
 
}

void loop() {
  int val;
  digitalWrite(speakerPin, LOW);
  val = analogRead(beatRate);
  val = val / 4;
 
  serByte = Serial.read();
  if (serByte != -1) {
    Serial.print(serByte,BYTE);
    ledState = !ledState;           // flip the LED state
    digitalWrite(ledPin, ledState); // write to LED
  }
  for (count=0;count<=8;count++) {  // look for the note
    if (names[count] == serByte) {  // ahh, found it
      for( int i=0; i<val; i++ ) {   // play it for 50 cycles
        digitalWrite(speakerPin, HIGH);
        delayMicroseconds(tones[count]);
        digitalWrite(speakerPin, LOW);
        delayMicroseconds(tones[count]);
        
  if (val < 200) // high beat
  {
    redVal   = 255; // Red up
    greenVal = 1; // Green dpwn
  }
  else if (val > 200) // low beat
  {
    redVal    = 1; // Red low
    greenVal = 255; // Green up

  }
  analogWrite(redPin,   redVal);   // Write current values to LED pins
  analogWrite(greenPin, greenVal);
      }
    }
  }
}

light sensor.jpg
2.jpg
1.jpg
0
Your rating: None