Lab 6 - Input/Output Coincidence

Posted by jlzych

jlzych's picture

Description

For this assignment I created a hat that plays a tune when when its user taps his or her head. This video describes it better (bonus points to anyone who can name that tune!): http://www.youtube.com/watch?v=FTP_JV2pjXw

Materials

  • FSR
  • Piezo Speaker
  • Arduino, breadboard, etc.

Code


 /* Plays a tune when the force sensed is over a certain threshold
 */
int inputPin = 2;    // select the input pin for the FSR
int speakerPin = 7;

byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "4c1p4c1p4f1p4f1p4d1p4d1p4g1p4g1p";
//byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// 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 MAX_COUNT = 24;
int val = 0;

void setup() {
  pinMode(speakerPin, OUTPUT); 
  Serial.begin(9600);
  Serial.println("ready");
}

void loop() {
  digitalWrite(speakerPin, LOW);
  val = analogRead(inputPin);
  if (val > 0) { Serial.println(val); }
  if (val > 300) { playSong(); }
}

void playSong() {
  int count = 0;
  int count2 = 0;
  int count3 = 0;

  digitalWrite(speakerPin, LOW);
  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') {
          // make a pause of a certain size
          digitalWrite(speakerPin, 0);
          delayMicroseconds(100);
        }
      }
    }
  }
}

 

0
Your rating: None