Sunday, October 16, 2011

Circuit-06 - Piezo Elements

Purpose: To play a short song using the Piezo Element

Equipment: 
  • Arduino x 1
  • Breadboard x 1
  • Arduino Holder x 1
  • CIRC-06 Breadboard Sheet x 1
  • Piezo Element x 1
  • Wire x 4

References:

Program Details:
The building of the circuit in this exercise very very simple. The only new thing is the introduction of the Piezo Element. The Piezo element has 2 leads and is in a circular shape, the positive lead must be connected to arduino an arduino pin, and the negative lead to the ground. The positive lead will have a plus sign on top of it.

Once again, there are a lot of new concepts introduced in the programming. First, an array, char notes[], is used to hold a bunch of the same type of data in one simple line, instead of creating a lot of different variables. Secondly, methods with parameters are introduced, playNote(char note, int duration). When a method has parameters, it must be called by using the same type of data as indicated in the parameters. For example, this line of code, playTone(tones[i], duration), calls the method playNote using a character (tones[i]) and an integer (duration). This method cannot be called by simply typing playNote(). Thirdly, when a method is called, the program goes to the method and progresses through it before continuing on with the next line of code.

Even by understand all the new concepts , this program is still quite hard to understand so the comments given should be looked at to fully understand the code. 

Time to Complete: 2 mins to assemble
                                15 mins to program

Results: Our Piezo element ran perfectly the first time we did this. It played the first verse of Twinkle Twinkle Little Star.

Photo:


Video:

Tips:
  • Make sure you understand your code, there are a lot of loops and many new concepts
  • If your Piezo element is not playing, try switching its two leads around 

Further Work: I will try to make another short song using the Piezo Element, such as Mary Had a Little Lamb or Happy Birthday. I will also attach an LED to display the beat of the song. 


Program Modifications:
This program is the exact same one from the link below:

Program:

int speakerPin = 9;                        // sets the input pin to pin9
int length = 15;                              // the number of notes
char notes[] = "ccggaagffeeddc ";  // the notes in order
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; // the beat of the notes
int tempo = 300;                          // the speed of the melody


void playTone(int tone, int duration)    // method playTone used to play a tone
{
  for (long i = 0; i < duration * 1000L; i += tone * 2) //note is only played as long as it is less than its duration
  {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }// ends for loop
}// ends playTone


void playNote(char note, int duration)   // method playNote used to play a note
{
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };        // Notes in the song
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };  // Tone of each note   
  // plays the tone corresponding to the note name
  for (int i = 0; i < 8; i++)   // playes all 8 notes
  {
    if (names[i] == note)       // corresponds the tone with the note
    {
      playTone(tones[i], duration); // refers to playTone method giving it the parameters
    }
  }// ends for loop
}// ends playNote


void setup()                                     // intializes once when program runs
{
  pinMode(speakerPin, OUTPUT); // sets speakerpin as an output
}


void loop()                                    // loop continues as long as there is power
{
  for (int i = 0; i < length; i++)        // plays 15 notes
  {
    if (notes[i] == ' ')                      // if the character is a space..
    {
      delay(beats[i] * tempo);         // then there is a rest
    }
    else                                         // otherwise...
    {
      playNote(notes[i], beats[i] * tempo);   // play the note according to the beat and tempo
    }
    delay(tempo / 2); // creates a slight pause
  }// ends for loop
}// ends the loop

No comments:

Post a Comment