Pages: [1]   Go Down
Author Topic: Make LEDs blink in rythm with music  (Read 1127 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello fellow Arduino users,

I have a code that plays the Popcorn song:

Code:
#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?
« Last Edit: August 01, 2012, 06:42:10 am by Mikkidan1 » Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

San Francisco
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Pages: [1]   Go Up
Jump to: