Sunday 16 October 2011

CIRC-06 - Music

Purpose:
To get a Piezo Element to play the nursery rhyme "Twinkle Twinkle Little Star".

Equipment:

  • 1x Arduino Uno
  • 1x Piezo Element
  • 1x Breadboard
  • 2x Wires
Reference:

Program Details:
Because this is a very simple circuit, not many new pieces of hardware were introduced. The only new piece of hardware is the Piezo Element. What a Piezo Element does esentially is make a cliking sound everytime it recieves a current. If we send alot of pulses in a short period, we can attain differnt frequencies, and thus play different notes. For this piece to work it needs a voltage, provided by Pin 9, and be grounded on the other end. 

Nothing new is introduced in terms of coding except for a new method: delayMicroseconds(us). This method is very similar to delay, as it stops the program for a very short amount of time. One microsecond is equal to 1/1000th of a millisecond, which in turn is 1/1000th of a second; meaning 1 microsecond = 1 millionth of a second. This is done in this program to give the program time to send the voltage to the speaker, rather than turning on and off instantly. The length of the delay determines the note which will be played in this case.

This is very simple to put together. First place the Piezo element onto the breadboard with each pin on its own row, and check which end is positive. This can be done by looking at the top, as the positive end will have a (+) with a circle around it. Then take a wire, and connect one end to Pin 9, and the other to the positive row. Afterwards take a wire, with one end connected to the ground, and the other to the negative row. 

Results:
The Piezo Element played "Twinkle Twinkle Little Star" flawlessly, as intended.

Photo:


Tips:
Make sure that you connect it the right way, as it only works in one direction. If you didn't its very simple to fix: just take it out , rotate it 180, and place it back in.

Further Work:
You can modify beats[] and the notes[] to essentially play any song that you want. The possibilities are endless.

Program Modifacations:
The program is exactly like the one from:

This is because this is the only way to play "Twinkle Twinkle Little Star". Modifying this would mean that you are no longer playing this song.

Program:

int speakerPin = 9;                                                   //Output pin for speaker

int length = 15;                                  // the number of notes in the song (w/ rest)     
char notes[] = “ccggaagffeeddc “;                                // The final space means rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };           //length of each note
int tempo = 300;                                              //Speed at which the music plays

void setup() 
{
  pinMode(speakerPin, OUTPUT);                                          //Sets Pin 9 to output
}


void loop() 
{
  for (int i = 0; i < length; i++)                             //Repeats 15 times (1 per note)
  {
    
    if (notes[i] == ' ')                                                     //Checks for rest
    {
      delay(beats[i] * tempo);      
                                    //rests for however long the beat lasts at the given tempo
    } 
   
    else                                                        //When there is no rest (note)
    {
      playNote(notes[i], beats[i] * tempo);                
                               //Calls method play note, gives current note and length of note
    }
   
    delay(tempo / 2);                                     //Little rest after the song is done
  }
}



void playNote(char note, int duration) 
{
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };           //all notes in Octave
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; //Corresponding 1/2timeHIGH

  for (int i = 0; i < 8; i++)                                 //Cycles 8 times (once per note)
  {
    if (names[i] == note)          //When the given note lines up with the notes in the octave
    {
      playTone(tones[i], duration);                       //Calls Playtone method to play note
    }
  }
}




void playTone(int tone, int duration)                   //Needs the 1/2timeHIGH and the length
{
  for (long i = 0; i < duration * 1000L; i += tone * 2)       //WTFQUISDHIUFSH
  {
    digitalWrite(speakerPin, HIGH);                                 //Sends voltage to speaker
    delayMicroseconds(tone);                   //Waits for signal to send (time = note played)
    digitalWrite(speakerPin, LOW);                                     //Stops sending voltage
    delayMicroseconds(tone);                                    //Waits AGAINOSDIFNOSD
  }
}




No comments:

Post a Comment