Go Down

Topic: LED with Music Notes (Read 3002 times) previous topic - next topic

kculm

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

PaulS

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.

kculm

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

PaulS

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.

robtillaart

Can you post your code sofar?

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

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

kculm

#5
Dec 28, 2012, 12:20 am Last Edit: Dec 28, 2012, 12:27 am by kculm Reason: 1
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.

Code: [Select]
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); 

    }
  } 
}



robtillaart

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,
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

Code: [Select]
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
Code: [Select]
pinMode(speakerPin, OUTPUT);
  for (int i=0; i< 10; i++)  pinMode(ledPins[i],OUTPUT);
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart


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 -
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

kculm

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.

robtillaart

#10
Dec 28, 2012, 12:32 am Last Edit: Dec 28, 2012, 12:40 am by robtillaart Reason: 1
some patches for the right direction
Code: [Select]

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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

Quote
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 :)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

johncc


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.

Code: [Select]

...
    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

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

johncc


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:
Code: [Select]

      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

Go Up