Pages: [1] 2 3   Go Down
Author Topic: LED with Music Notes  (Read 1891 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 168
Posts: 12433
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you post your code sofar?

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

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

    }
  } 
}

« Last Edit: December 27, 2012, 06:27:06 pm by kculm » Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 168
Posts: 12433
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

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

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 168
Posts: 12433
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

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

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 168
Posts: 12433
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 168
Posts: 12433
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

some patches for the right direction
Code:
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]);
    }
  }  
}

« Last Edit: December 27, 2012, 06:40:50 pm by robtillaart » Logged

Rob Tillaart

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

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 168
Posts: 12433
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Rob Tillaart

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

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2 3   Go Up
Jump to: