LED with Music Notes

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.

robtillaart:
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:

The issue is I am still learning the code. so its not all making sense to me yet.
but ill keep plugin along.

John I am sorry but I can't figure it out. I will need your help.

Also when it plays, it looks like it just lighing the lights in order pin 2-9. I looks ok. but what I was going for was a not assigned to an LED

ie C Led1 G Led2 A Led3 and so on. this way when that noted played more then once the Led would light.

thanks

johncc:

kculm:
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

this way when that noted played more then once the Led would light.

So, you simply want to turn another LED on each time another note is played? That is not what you first asked for.

I am very thank for the help I am getting and I am learning a lot. Not to sound ungrateful but I did mention that in the begin of this post. Don't get me wrong I can live with it the way it is until I learn more. I know I have a long way to go.

kculm:
Hey gang

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 Led1 , 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

kculm:
...
Also when it plays, it looks like it just lighing the lights in order pin 2-9. I looks ok. but what I was going for was a not assigned to an LED
...

Yes, the "turn on" and "turn off" statements were in the wrong place (and looking at the wrong data). Notice in the below that I moved them to the playNote() function:

int speakerPin = 11;

char notes[] = "ccggaagffeeddcggffeedggffeedccggaagffeeddc "; // a space represents a rest
int length = sizeof(notes)/sizeof(notes[0]); // the number of notes
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 = 1000;  // make this smaller to speed the whole thing up, bigger to slow down
int ledPins[] = { 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 < 8; i++) {
    if (names[i] == note) {
      Serial.println( String(" Note: ") + names[i] + " dur: " + duration + " ledPin: " + ledPins[i]);
      digitalWrite(ledPins[i], HIGH);     // turn on the right LED                 <<********************************
      playTone(tones[i], duration);       // play the note
      digitalWrite(ledPins[i], LOW);      // turn off the LED
    }
  }
}

void setup() {
  pinMode(speakerPin, OUTPUT);
  for (int i=0;i<8; i++)
      pinMode(ledPins[i],OUTPUT);
  Serial.begin(9600);
}


void loop() {
  Serial.println("Start");
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } 
    else {
      playNote(notes[i], beats[i] * tempo);
      delay (100);  // make this smaller (or even delete the line) to have less time between the notes, bigger for more
    }
  }  
}

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

The names matching the notes says to me that you want Led1 on while note C is playing, Led2 on when note G is playing, Led3 on when note A is playing.

This is different from turning one led on when the first note plays, two leds on while the second note plays, three on while the third note plays, etc.

Or, maybe I'm not understanding your initial or current desire.

PaulS:

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

[1] The names matching the notes says to me that you want Led1 on while note C is playing, Led2 on when note G is playing, Led3 on when note A is playing.

[2] This is different from turning one led on when the first note plays, two leds on while the second note plays, three on while the third note plays, etc.

Or, maybe I'm not understanding your initial or current desire.

I think y'all are talking past each other a little bit. I think the code/fix I just posted provides behavior he wants, [1] above.

Cheers,
John

I am sorry, I must not be explaining it right.

The notes used in the song are A,C,D E F G

Played like this ccggaag ffeeddc ggffeed ggffeed ccggaagffeeddc

(BTW as I am writing this I see one of my errors. I said 8 Notes and there are only 6)

Anyway I was thinking it would be cool to assign an LED to each note.

LED1 C, LED2 G LED3,A and so on. This way every time it played C led1 came one G Led2 came on and so on.

Guy I am sorry I have not been explaining myself very clearly and my spelling is not the best.

Thank for all you time and effort. I am learning.

kculm:
I am sorry, I must not be explaining it right.

The notes used in the song are A,C,D E F G

Played like this ccggaag ffeeddc ggffeed ggffeed ccggaagffeeddc

(BTW as I am writing this I see one of my errors. I said 8 Notes and there are only 6)

Anyway I was thinking it would be cool to assign an LED to each note.

LED1 C, LED2 G LED3,A and so on. This way every time it played C led1 came one G Led2 came on and so on.

Guy I not I have been explaining myself very clearly and my spelling is not the best.

Thank for all you time and effort. I am learning.

I'm pretty sure I understand you kculm. I posted the entire sketch, copy and paste the whole thing, run it, and let me know if that works.

You do have 8 LEDs wired up (on pins 2-9), correct?

Cheers,
John

John, Yes to Led's On pins 2-9 Spk on 11.

When I run your code all I get is one Note And led on pin2.

Thats it.

kculm:
John, Yes to Led's On pins 2-9 Spk on 11.

When I run your code all I get is one Note And led on pin2.

Thats it.

Does the note play and end, or play and stay playing?

Try taking out all the lines that start with "Serial." There are three of them.

John

Note Plays and ends Led/Pin2 Comes on for a sec or so and then Off.

johncc:

Does the note play and end, or play and stay playing?

Try taking out all the lines that start with "Serial." There are three of them.

John

Thats its, you the Man..

kculm:

johncc:

Does the note play and end, or play and stay playing?

Try taking out all the lines that start with "Serial." There are three of them.

John

Thats its, you the Man..

Awesome. Give me some karma (click the little + under my name). I have a long way to go to catch up with PaulS :slight_smile:

Cheers,
John