Tuesday, 1 November 2011

CIRC-06: Piezo Speaker

Purpose: 
Make the speaker play a simple tune using a range of frequencies.

Equipment:
•1 x Arduino Uno
•1 x Breadboard
•4 x Wire
•1 x Piezo Element

Reference:
http://www.oomlout.com/a/products/ardx/circ-06

Program Details: 
The same concepts are used, but for a different purpose for this lab. The initializing and loops are usually for the LED. This time, those simple concepts are used to play notes in a loop and initialize the speaker.

Time to Complete: 
10 minutes

Results:
The speaker in our Arduino set was not functioning properly. When we switched the speakers from another kit, the music began to play.

Photos of Project: 




Tips:
The speaker can be easily misplaced in the wrong holes. Double check to see if they are placed correctly.

Further Work:
We can attempt to use the creative part our minds and code a new song by using the different frequencies.

Program Modifications:
The program is exactly the one from http://www.oomlout.com/a/products/ardx/circ-06

Program:
int speakerPin = 9;
int length = 15; // the number of notes
char notes[] = “ccggaagffeeddc “; // creates an array of type char
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; /*creates an array of type int */
int tempo = 300;

void playTone(int tone, int duration) /* creates a function, playTone that has to parameters of type int */
 {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);      //changes state of speakerPin to high
    delayMicroseconds(tone);             //delay equal to the value of tone
    digitalWrite(speakerPin, LOW); //changes state of speakerPin to low
    delayMicroseconds(tone);             //delay equal to the value of tone
  }
}
void playNote(char note, int duration) {
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);      //send tones at i  and duration to playRone
    }
  }
}
void setup() {
  pinMode(speakerPin, OUTPUT);           //initializes pin 9 as an output
}
void loop() {
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats[i] * tempo);  //sends as parameters for playNote
    }


    delay(tempo / 2);
  }
}

No comments:

Post a Comment