LED with Music Notes

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);

It working a little better.

The Lights to don't come until 3 notes in and they stop after about 8.

I am going to go over the code to see what i can see

Thanks for all the help.

Ok I fixed the issuse with it starting late..The code was

int ledPins[] = {0,1,2,3,4,5,6,7,8,9};

and Now

int ledPins[] = {2,3,4,5,6,7,8,9};

Like I stated earlyer in the Post. it will using and EL Escudo Dos to run EL wire

EL Escudo Dos use pins 2- 9 and 13.

I still get way it stops the lights 8 notes in tho.

You must read the code and be the processor and follow it step by step what it does. Use pencil and paper to write down the state of variables.

Although it may sounds stupid at first you will see through the eyes of the processor and learn a lot :wink:

kculm:
Ok I fixed the issuse with it starting late..The code was

int ledPins[] = {0,1,2,3,4,5,6,7,8,9};

and Now

int ledPins[] = {2,3,4,5,6,7,8,9};

Great!

I still get way it stops the lights 8 notes in tho.

If this means "I still don't why it stops after 8 notes though"....

Here is the problem

    else {
      // here, i is counting from 0 to 41
      digitalWrite(ledPins[i], HIGH);    // but there are only 9 ledPins!
      playNote(notes[i], beats[i] * tempo);
      digitalWrite(ledPins[i], LOW);  
      delay (100);
    }

but

  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      // Here i, represents the note that was found, between 0 and 7 inclusive
      playTone(tones[i], duration);
    }

See if you can figure out how to fix this. If not I will post the solution tonight...

Cheers,
John

johncc:

kculm:

I still get way it stops the lights 8 notes in tho.

If this means "I still don't why it stops after 8 notes though"....

It was late, lol but yes that is what I was trying to say.