Lab 5 - Input/Output Coincidence a.k.a. Musical Owl

Posted by emily

emily's picture

 

Description

Use the inputs and output devices in lab thus far to create a project with input/output coincidence.

In this lab, I stuffed a red owl coin purse with tissue paper, a Piezo buzzer, and a force sensitive resistor to mimic a small stuffed toy that plays the tune "Twinkle, twinkle little star" when you squeeze it.

Components Used

  • Arduino board x 1
  • wires x several
  • Force Sensitive Resistor (FSR) x 1
  • 220 Ohm resistor x 1
  • Piezo buzzer x 1
  • Red owl coin purse x 1
  • whimsical melody x 1

Arduino Code

/* 
Squeeze_me
A Piezo buzzer inside an owl purse plays a melody
when you squeeze an FSR in the coin purse.
*/
 
int fsrPin = 1;
int fsrVal = 0;
 
int speakerPin = 7;
 
int count = 0;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'}; 
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
int hold[] = {100, 112, 126, 134, 150, 169, 189, 200}; 
int note = 0;
int lastNote = 15;
byte melody[] = "ccggaagppffeeddc";
int count3 = 0;
 
 
void setup() {
  //initialize the Piezo sensor as output
  pinMode(speakerPin, OUTPUT);
  Serial.begin(9600);
}
 
void loop() {
  digitalWrite(speakerPin, LOW);
  fsrVal = analogRead(fsrPin);
  
  if (fsrVal > 10) {
    Serial.println(fsrVal);
    //for each note in the melody
    for (note = 0; note <= lastNote; note++) {
      
      //look for the note in the Piezo scale c - C
      for (count = 0; count <= 8; count++) {
      //if the note is found, play it for a number of cycles
      //so that each note seems to be the same length (100 * (1915/tone))
        if (melody[note] == names[count]) {
          for (int i = 0; i < hold[count]; i++) {
            digitalWrite(speakerPin, HIGH);
            delayMicroseconds(tones[count]);
            digitalWrite(speakerPin, LOW);
            delayMicroseconds(tones[count]);
          }
        }  
        else if (melody[note] == 'p') {
          //make a pause of a certain size
          digitalWrite(speakerPin, LOW);
          delay(20);
        }
      }
    }
  }
 
  else {
    Serial.println("no squeeze");
  }
}
 
 

Video

 

Creator Comments

I updated the code (and video) to play each note for equal lengths of time.

0
Your rating: None