LED with Music Notes

Hey gang

I need a little (lot) of help.

I just stared to play with Arduion and few days ago. So far I am loving it.

I have spent the last few days trying to learn the basics of the Board and code.

I have a little project in mind but not sure how to start.

I want the Arduino to Play twinkle twinkle little star. That part I can do.

Now that pat I cant get.

twinkle twinkle uses 6 notes, A C D E F G. I would like to blink an LED to correspond to a Note.

i.e. C Led2 , G Led2, A Led3

It would look something like this

CC GG AA G
Led1 Led1, Led2 Led2, Led3 Led3, Led2.

It would be great to someone had some code to look at or point me in the right direction

Lets get something straight right off the bat. You can't blink an LED. You can turn one on and you can turn one off.

Now, please try again, to explain what you want to do without using terms that make no sense.

Easy big boy, like I said I am new to all this.

I used the word blink because that was the first tutorial and it was called blink.

That being said, I guess Blink is the wrong word. I want it to turn on then turn with the notes.

In the end I’ll be connecting a EL Escudo Dos shield to it to control EL Wire.

Notes: CC GG AA

LED ON/OFF ON/OFF ON/OFF ON/OFF ON/OFF ON/OFF (in time with the music)

Thanks

I think the simplest thing to do is to turn the C LED on, play the C note, then turn the C LED off. Making the C LED turn on and off more than once while the C note is playing is going to be difficult (to see, at least).

I'd create functions, like playC(). In that function, I'd turn the C LED (pin) on, play the note, turn the C LED (pin) off, and return.

Can you post your code sofar?

It might help to give you advice when talking about the real thing ...

This is what I got, I found it online and just added the rest of the notes.

It works kind of. It turns on one lite per beat not per not.

I may be to green to take on a task like this.

Thanks for any help you can give.

int speakerPin = 11;

int length = 42; // the number of notes
char notes[] = "ccggaagffeeddcggffeedggffeedccggaagffeeddc "; // a space represents a rest
int beats[] = { 
  1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4,};
int tempo = 250;
int ledPins[] = {
  0,1,2,3,4,5,6,7,8,9};

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(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 < 45; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

void setup() {
  pinMode(speakerPin, OUTPUT);
  pinMode(ledPins[0],OUTPUT);
  pinMode(ledPins[1],OUTPUT);
  pinMode(ledPins[2],OUTPUT);
  pinMode(ledPins[3],OUTPUT);
  pinMode(ledPins[4],OUTPUT);
  pinMode(ledPins[5],OUTPUT);
  pinMode(ledPins[6],OUTPUT);
  pinMode(ledPins[7],OUTPUT);
  pinMode(ledPins[8],OUTPUT);
  pinMode(ledPins[9],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);
      digitalWrite(ledPins[i], HIGH); 
      delay (100);
      digitalWrite(ledPins[i], LOW);  

    }
  }  
}

Please do two things:

  1. go to your IDE and press CTRL-T to reindent your code
  2. when posting code on the forum please use the # button to get proper tags (you can modify your post, select the code and press # still)

Thanks,

 pinMode(speakerPin, OUTPUT);
  pinMode(ledPins[0],OUTPUT);
  pinMode(ledPins[1],OUTPUT);
  pinMode(ledPins[2],OUTPUT);
  pinMode(ledPins[3],OUTPUT);
  pinMode(ledPins[4],OUTPUT);
  pinMode(ledPins[5],OUTPUT);
  pinMode(ledPins[6],OUTPUT);
  pinMode(ledPins[7],OUTPUT);
  pinMode(ledPins[8],OUTPUT);
  pinMode(ledPins[9],OUTPUT);

can be written as

 pinMode(speakerPin, OUTPUT);
  for (int i=0; i< 10; i++)  pinMode(ledPins[i],OUTPUT);

I see a number of errors in the code that relate to the use of arrays.
Please spend some time reading the tutorial pages about array;s here - http://arduino.cc/en/Reference/Array -

Thanks I'll take a look at that.

I have no clue what I was doing, copied most of the code. hopefully your like will give me a better understanding.

Thanks Once More. I can use all the help I can get.

some patches for the right direction

void playTone(int tone, int duration, int pin) 
{
  digitalWrite(pin, HIGH); 
  for (long i = 0; i < duration * 1000L; i += tone * 2)
  {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
  digitalWrite(pin, LOW); 
}

void loop() 
{
  for (int i = 0; i < length; i++)
  {
    if (notes[i] == ' ') 
    {
      delay(beats[i] * tempo); // rest
    } 
    else 
    {
      playTone(notes[i], beats[i] * tempo, ledPins[i]);
    }
  }  
}

Thanks Once More. I can use all the help I can get.

One day you will be the one guiding people on this or another forum :slight_smile:

kculm:
This is what I got, I found it online and just added the rest of the notes.

It works kind of. It turns on one lite per beat not per not.

I may be to green to take on a task like this.

Thanks for any help you can give.

...

else {
      playNote(notes[i], beats[i] * tempo);
      digitalWrite(ledPins[i], HIGH);
      delay (100);
      digitalWrite(ledPins[i], LOW); 
...

Looks to me like you're pretty close. But I think what you want to do is:

  1. turn on the appropriate LED
  2. tell the note to play
  3. turn off the LED
  4. delay 100 milliseconds

Do you see what you need to change, above?

Cheers,
John

John,

Thanks but to tell you the truth its all Greek to me. I am an Network guy. I deal in IP''s address and routing tables.

I can understand some of it but not all. I am trying to find a good book that examples the Arduino Code and how to use it. But so far I have only seen books that are to Basic or way over my head.

If any one lives the West Palm Beach Fl area and would be willing to tutor or over the phone PM me.

Thanks

kculm:
John,

Thanks but to tell you the truth its all Greek to me. I am an Network guy. I deal in IP''s address and routing tables.

I can understand some of it but not all. I am trying to find a good book that examples the Arduino Code and how to use it. But so far I have only seen books that are to Basic or way over my head.

If any one lives the West Palm Beach Fl area and would be willing to tutor or over the phone PM me.

Thanks

You're copping out kculm. Try putting these lines in order:

      playNote(notes[i], beats[i] * tempo);   // 2) tell the note to play
      digitalWrite(ledPins[i], HIGH);         // 1) turn on the appropriate LED
      delay (100);                            // 4) delay 100 milliseconds
      digitalWrite(ledPins[i], LOW);          // 3) turn of the LED

Try putting these lines in order:

Is step 4 really necessary? That will REALLY slow the song down.

John,

I am not copping out, I am just not getting it.

I sure you have better things to do, but you get some free time do you think you can set it up on a breadboard and see what I see. Like I said I am sure you have better things to do.

I am not sure what you mean when you say "Try putting these lines in order".

I am very thankful for your time and help.

I am not sure what you mean when you say "Try putting these lines in order".

Did you look at the comments on those 4 lines John posted?

kculm:
...I am not sure what you mean when you say "Try putting these lines in order"....

Please stare at the following until you understand what "put in order" means :slight_smile:

???????? ?????? (?????????? [i], ??????? [i] * ????????)?  // 2) ????? ?? ???????? ??? ?? ??????
       ???????? ???????? (??????? ??? [i], HIGH)?          // 1) ??? ?? ????? ??? ??? ????????? LED
       ??????????? (100)?                                  // 4) 100 ???????? ??? ????????????? ???????????
       ???????? ???????? (??????? ??? [i], LOW)?           // 3) ?? ????? ??? LED

Ok Is this what you mean. I hope. If not I am going to feel like a real Dumb A%#.

digitalWrite(ledPins[i], HIGH); 
      playNote(notes[i], beats[i] * tempo);
      digitalWrite(ledPins[i], LOW);  
      delay (100);