Go Down

Topic: Make LEDs blink in rythm with music (Read 1 time) previous topic - next topic

Mikkidan1

Aug 01, 2012, 01:17 pm Last Edit: Aug 01, 2012, 01:42 pm by Mikkidan1 Reason: 1
Hello fellow Arduino users,

I have a code that plays the Popcorn song:

Code: [Select]
#include "pitches.h"
int led = 13;

int melody[] = {
 NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_G5, NOTE_DS5, NOTE_G5, NOTE_C5, 0,
 NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_G5, NOTE_DS5, NOTE_G5, NOTE_C5, 0,
 NOTE_C6, NOTE_D6, NOTE_DS6, NOTE_D6, NOTE_DS6, NOTE_C6, NOTE_D6, NOTE_C6,
 NOTE_D6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_G5, NOTE_C6, 0,
 NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_G5, NOTE_DS5, NOTE_G5, NOTE_C5, 0,
 NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_G5, NOTE_DS5, NOTE_G5, NOTE_C5, 0,
 NOTE_C6, NOTE_D6, NOTE_DS6, NOTE_D6, NOTE_DS6, NOTE_C6, NOTE_D6, NOTE_C6,
 NOTE_D6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_G5, NOTE_C6,
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
 4,4,4,4,4,4,4,4,
 4,4,4,4,4,4,4,4,
 4,4,4,4,4,4,4,4,
 4,4,4,4,4,4,4,4,
 4,4,4,
 4,4,4,4,4,4,4,4,
 4,4,4,4,4,4,4,4,
 4,4,4,4,4,4,4,4,
 4,4,4,4,4,4
 };
void setup() {
 for (int thisNote = 0; thisNote < 64; thisNote++) {
   int noteDuration = 900/noteDurations[thisNote];
   tone(8, melody[thisNote],noteDuration);
   int pauseBetweenNotes = noteDuration * 1;
   delay(pauseBetweenNotes);
   noTone(8);
 }
 int led = 13;
 pinMode(led, OUTPUT);
}
void loop() {
}


And I wanted to make few LEDs blink in rhythm to the beeps.

I'm using an Arduino Mega 2560 R3

What can I add to the code to make one or more LEDs blink in rhythm to the music?

brandondrury

I'm  VERY new to Arduido and probably shouldn't  be giving advice. 

Have you toyed with http://arduino.cc/en/Tutorial/Blink ?

It seems you could put blink code in your "for" loop. 

Again, don't listen to me.   :D

jon_s

I'd move the LED pin declaration up before the tone loop, and then toggle the LEDs to go on before each tone and off before each rest. I haven't tested this but I think it'd work.

Code: [Select]

int led = 13;
pinMode(led, OUTPUT)

for (int thisNote = 0; thisNote < 64; thisNote++) {
    int noteDuration = 900/noteDurations[thisNote];
    digitalWrite(led, HIGH);
    tone(8, melody[thisNote],noteDuration);
    digitalWrite(led, LOW);
    int pauseBetweenNotes = noteDuration * 1;
    delay(pauseBetweenNotes);
    noTone(8);
  }



Go Up