Arduino twinkle, twinkle little star

I recently bought an arduino board and yesterday I took it out for a spin.

They have some thorough tutorials on their website here

I’ve used this tutorial to play some sounds with a piezo speaker and then I’ve changed a little bit the program to play my own baby. Since I don’t know which cord is which note (I suck reading guitar notes) I’ve used this tutorial to find what’s what.

This is what it came out .

And here is the complete source code:

 #include "pitches.h"
// notes in the melody:
int melody[] = {

    N_B4, N_B4, N_FS5, N_FS5, N_GS5, N_GS5, N_FS5
  , N_NONE
  , N_E5, N_E5, N_DS5, N_DS5, N_CS5, N_CS5, N_B4
  , N_NONE
  , N_FS5, N_FS5, N_E5, N_E5, N_DS5, N_DS5, N_CS5
  , N_NONE
  , N_FS5, N_FS5, N_E5, N_E5, N_DS5, N_DS5, N_DS5, N_DS5, N_CS5
  , N_NONE
  , N_B4, N_B4, N_FS5, N_FS5, N_GS5, N_GS5, N_FS5
  , N_NONE
  , N_E5, N_E5, N_DS5, N_DS5, N_CS5, N_CS5, N_B4
  , N_NONE
  };

int noteDurations[] = {
   2,2,2,2,2,2,2
  ,2
  ,2,2,2,2,2,2,2
  ,2
  ,2,2,2,2,2,2,2
  ,2
  ,2,2,2,2,4,4,4,4,2
  ,2
  ,2,2,2,2,2,2,2
  ,2
  ,2,2,2,2,2,2,2
  ,2
  };

void setup() {}

void playSong(){
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < sizeof(melody)/sizeof(int); thisNote++) {

    // to calculate the note duration, take one second divided by the note type.
    //e.g. half note = 1000 / 2
    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    delay(noteDuration);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  playSong();
  delay(1000);
}
This entry was posted in arduino. Bookmark the permalink.

7 Responses to Arduino twinkle, twinkle little star

  1. ADrian says:

    Twinkle, Twikle, Little star.. Nice! :)

  2. Ovi says:

    Ai prea mult timp liber :)

    • admin says:

      nuu ca l-am facut repede… (sau nu mi-am dat eu seama cat timp a luat? :) ) oricum, l-am facut ieri dupa ce au plecat ai mei din vizita :D

  3. Viorel Spinu says:

    Cool :) ! De unde ti-ai luat Arduino ?

  4. Alex says:

    Do you happen to have a copy of your pitches.h file? I noticed that that variables you use are different then the file on the tutorial you used. Also, the one on the tutorial doesn’t make reference to N_NONE nor NOTE_NONE.

    Thanks, my daughter will love this

Leave a Reply to Ovi Cancel reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>