[feasibility] LEDs and Piezo Buzzer Synchronization - Noob Question

Introduction

I am about as noob as you can get. I've been at this for about two weeks.

Background

Using sample sketches, I've figured out how to...
* blink/fade multiple LEDs at the same time
* write a song to play through a piezo buzzer

I want to create a sketch that plays a song ("Jingle Bells" for example) and have four sets of LEDs blink to the music.

QUESTION

Is this even possible, and if so, how would I go about doing it?

Thanks in advance! :smiley:

Post both sketches please! Use code tags.

Paul

Yes, it is possible but post your buzzer code. In principal you can have a play function that takes a note and play it, while cycling through leds. The play function wil have a static variable to store the next led to be turned on.

Sorry, guys, I'm not too good at this yet. Basically all I've done is taken other code samples and manipulated them a bit in an attempt to learn what it all means and how it all works. If you look, you'll see a lot of the comments "//_____" are the originals and haven't been updated to fit what I've done.

Here is the code from my Flashing and Fading project.

// Example of flashing and fading multiple LEDs at different rates without delays
// Adapted from http://www.gammon.com.au/forum/?id=11411
// Author: Nick Gammon
// Date: 23 December 2012

#include <LedFlasher.h>
#include <LedFader.h>

// set up some LEDs
LedFlasher floodLight (3, 200, 300);
LedFader shuttleBayDoors (6, 10, 300, 600);
LedFlasher impulseEngine (9, 900, 100);
LedFader strobe     (11, 10, 200,  1000);


void setup() 
  {      
  floodLight.begin ();
  shuttleBayDoors.begin ();
  impulseEngine.begin ();
  strobe.begin ();
  }  // end of setup

void loop() 
  {
  // update lights
  floodLight.update ();
  shuttleBayDoors.update ();
  impulseEngine.update ();
  strobe.update ();
  
  
  // do other useful stuff here ...

 
  }  // end of loop

As you can see, all I've done was taken two of Nick Gammon's codes/libraries and cut and pasted to find out what happens. I changed pin numbers and length of time, but that was all.

Here is my sketch for the buzzer. I have four year old girls, so forgive me for the simplicity of the song.

/*
  "Jesus Loves Me" Melody
 
 Plays "Jesus Loves Me"
 
 circuit:
 * 8-ohm speaker on digital pin 8
 
 Based on code created 21 Jan 2010
               modified 30 Aug 2011
               by Tom Igoe 

This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/Tone
 
 */
 #include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_G4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_G4, NOTE_G4,
  NOTE_A4, NOTE_A4, NOTE_C5, NOTE_A4, NOTE_A4, NOTE_G4, NOTE_G4,
  NOTE_G4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_G4, NOTE_G4,
  NOTE_A4, NOTE_A4, NOTE_G4, NOTE_C4, NOTE_E4, NOTE_D4, NOTE_C4,
  NOTE_G4, NOTE_E4, NOTE_G4, NOTE_A4, NOTE_C5,
  NOTE_G4, NOTE_E4, NOTE_C4, NOTE_E4, NOTE_D4,
  NOTE_G4, NOTE_E4, NOTE_G4, NOTE_A4, NOTE_C5,
  NOTE_A4, NOTE_G4, NOTE_C4, NOTE_E4, NOTE_D4, NOTE_C4};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 4, 4, 4, 4, 4, 2,  4, 4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 4, 2,  4, 4, 4, 4, 4, 4, 2,
  2, 4, 4, 4, +2,       2, 4, 4, 4, +2,
  2, 4, 4, 4, 2,        4, 4, 4, 4, 4, 1};

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 49; thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  // no need to repeat the melody.
}

As you can see, again all I did was used Tom Igoe's library to find the notes I wanted and then adjusted the length of each note until it sounded right.

I've been researching sound sketching and am really feeling sort of lost on it. For example, I'm not really sure what "(int thisNote = 0; thisNote < 49; thisNote++)" means.

Also, my girls told me when I played it for them that is was too fast because they couldn't sing with it. How do you slow it down to play at say 100 beats/minute?

The way I have my project set up right now, I have LEDs on pins 3, 6, 9, and 11, with the buzzer on pin 7. I've already discovered that I can't run six LEDs off of one pin without more equipment, so I'm either going to have to get a few transistors and learn how to set them up.

If it helps, I'm using an Arduino Leonardo compatible board a friend bought off of indiegogo.

OK, I don't know about Nick's libraries but let's start with something simple enough, such as just blinking LEDs in a sequence like 1234 1234 as the notes are playing, like a kids toy. Here is the code that plays a note:

    tone(8, melody[thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);

What you need to do is to toggle on one led after tone and toggle it off after noTone. Here is a simple version:

    tone(8, melody[thisNote],noteDuration);
    LEDon();
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
    LEDoff()

This won't work until you add the following at the end of your program, beyond the {} of loop:

int currentLEDpin=3;
void LEDon()
{
    digitalWrite(currentLEDpin, HIGH);
}

void LEDoff()
{
    digitalWrite(currentLEDpin, LOW);
    if (currentLEDpin==3) currentLEDpin=6;
    else if (currentLEDpin==6) currentLEDpin=9;
    else if (currentLEDpin==9) currentLEDpin=11;
    else currentLEDpin=3;
}

Also add the following in your setup()

    pinMode(3,OUTPUT);
    pinMode(6,OUTPUT);
    pinMode(9,OUTPUT);
    pinMode(11,OUTPUT);
    digitalWrite(3, LOW);
    digitalWrite(6, LOW);
    digitalWrite(9, LOW);
    digitalWrite(11, LOW);

Don't try to merge Nick's code with the song code, add the above to the song code. Once you make this work, think about the fade next.

Thank you so much for the input, liudr. I tried your suggestions, and the song and lights worked great! :smiley:

Here is the sketch I came up with.

[size=8pt]
/*
  "Jingle Bells" Melody
 
 Plays "Jingle Bells"
 
 circuit:
 * 8-ohm speaker on digital pin 7
  
 */
#include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_D4, NOTE_D4, NOTE_D4,
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_E4,
  NOTE_E4, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_FS4,
  NOTE_D5, NOTE_D5, NOTE_C5, NOTE_A4, NOTE_B4, NOTE_D4,
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_D4, NOTE_D4, NOTE_D4,
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_E4, NOTE_E4,
  NOTE_E4, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_D5, NOTE_D5, NOTE_D5, NOTE_D5,
  NOTE_E5, NOTE_D5, NOTE_C5, NOTE_A4, NOTE_G4,
  NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_B4, NOTE_D5, NOTE_G4, NOTE_A4, NOTE_B4,
  NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_B4, NOTE_A4, NOTE_A4, NOTE_B4, NOTE_A4, NOTE_D5,
  NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_B4, NOTE_D5, NOTE_G4, NOTE_A4, NOTE_B4,
  NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_D5, NOTE_D5, NOTE_C5, NOTE_A4, NOTE_G4 };

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {

  8, 8, 8, 8, 3, 16, 16,
  8, 8, 8, 8, 2,
  8, 8, 8, 8, 2, 
  8, 8, 8, 8, 3, 8,
  8, 8, 8, 8, 3, 16, 16,
  8, 8, 8, 8, 3, 8,
  8, 8, 8, 8, 8, 8, 8, 8,
  8, 8, 8, 8, 2,
  8, 8, 4, 8, 8, 4,
  8, 8, 6, 16, 2,
  8, 8, 6, 16, 8, 8, 8, 16, 16,
  8, 8, 8, 8, 4, 4,
  8, 8, 4, 8, 8, 4, 8, 8, 6, 16, 2,
  8, 8, 6, 16, 8, 8, 8, 16, 16,
  8, 8, 8, 8, 2 };

void setup() {
    pinMode(3,OUTPUT);
    pinMode(6,OUTPUT);
    pinMode(9,OUTPUT);
    pinMode(11,OUTPUT);
    digitalWrite(3, LOW);
    digitalWrite(6, LOW);
    digitalWrite(9, LOW);
    digitalWrite(11, LOW);
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 100; thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1800/noteDurations[thisNote];
    // I found that by lengthening the noteDuration,
    // I was able to "slow" the song down.
    tone(7, melody[thisNote],noteDuration);
    LEDon();

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(7);
    LEDoff();
  }
}

void loop() {
  // no need to repeat the melody.
}

int currentLEDpin=3;
void LEDon()
{
    digitalWrite(currentLEDpin, HIGH);
}

void LEDoff()
{
    digitalWrite(currentLEDpin, LOW);
    if (currentLEDpin==3) currentLEDpin=6;
    else if (currentLEDpin==6) currentLEDpin=9;
    else if (currentLEDpin==9) currentLEDpin=11;
    else currentLEDpin=3;
}
[/size]

I figured out a bit more about slowing down the speed of the song by adjusting the noteDuration. I am still not sure what this means,

  (int thisNote = 0; thisNote < 100; thisNote++)

but I discovered that if the condition is too low then the song cuts off, but if it's too long, then the piezo "clicks" indefinitely.

My next question...

Thanks to your advice I was able to make the lights blink in a sequence around my breadboard. Now, what if I want to make different lights blink at different times during the song?

For example,

Jin-gle Bells (pins 6 and 9 continuously for all three notes)
Jin-gle Bells (pins 3 and 11 continuously for all three notes)
Jin (pin 6)
gle (pin 9)
all (pin 11)
the (pin 3)
way (all pins)

Thanks again for your help!

Good progress! I was "driving by wire" when I was writing the code, having no really hardware to feel how the code will work. Glad it worked. The (int thisNote = 0; thisNote < 100; thisNote++) line decides how many times the program fetches for a new note from melody[]. Of course if you don't let it fetch enough notes the song is cut off. If you let it fetch more than your song, it starts grabbing stuff in memory that is not your song. If you change it to (int thisNote = 0; thisNote < sizeof(melody); thisNote++) then I bet you don't have to find out the exact number. The compiler will find it for you.

If you want the fancy blink according to the note playing feature, please first write an array like the melody[]. I will give you an idea how to write:
Now assume 3 is led0, 6 is led1, 9 is led2, 11 is led3: led0 on and everything else off is: B0001
led1 on and everything else off is B0010, led0 and led 2 both on will be B0101.

byte LEDs[]={B1010, B1010, B1010, B0101,B0101B0101,B0010,B0100,B1000,B0001,B1111,....};

The above code has all your example led sequences. Fill it with your entire sequence. I'll get back to you tomorrow :slight_smile:

Hi liudr,

Thanks for the encouragement. Glad to know I'm picking up some of this. :slight_smile:

liudr:
If you want the fancy blink according to the note playing feature, please first write an array like the melody[]. I will give you an idea how to write:
Now assume 3 is led0, 6 is led1, 9 is led2, 11 is led3: led0 on and everything else off is: B0001
led1 on and everything else off is B0010, led0 and led 2 both on will be B0101.

byte LEDs[]={B1010, B1010, B1010, B0101,B0101B0101,B0010,B0100,B1000,B0001,B1111,....};

The above code has all your example led sequences. Fill it with your entire sequence. I'll get back to you tomorrow :slight_smile:

Okay, so I get how to write the B1010, B1010, B1010, etc. to create the pattern I want the lights to blink in. You also said "Now assume 3 is led0..." How does the program know to make that same assumption? Do I need to create a "library" for the LEDs like the one I used for the notes of the song?

#define NOTE_AS6 1865 was an example from the melody where NOTE_AS6 played A#

Would I need to create something like

#define B0001 led0
#define B0010 led1
#define B0100 led2
#define B1000 led3

if so, would I write a combination of LEDs like this

#define B0011 led0, led1 ? Or would the program know that a 1 means "on" and a 0 means "off"?

Also, reading up on Arrays on here, one example shows setting up an array like this:

int ledPins[] = { 
  2, 7, 4, 6, 5, 3 };       // an array of pin numbers to which LEDs are attached
int pinCount = 6;           // the number of pins (i.e. the length of the array)

That was for a simple sketch where the LEDs light up in one direction and then the other. Would mine need to be different since mine will be more complex?

Here's what I came up with for the "pattern" of the lights during the song:

byte LEDs[]={
   B0010,   B0100,   B1000,   B0001,   B1111,       B0001,    B0001,
   B0001,   B1000,   B0100,   B0010,   B1111,
   B0010,   B1000,   B0100,   B0001,   B1111,
   B0001,   B0100,   B1000,   B0010,   B1111,       B0010,
   B0010,   B0100,   B1000,   B0001,   B1111,       B0001,    B0001,
   B0001,   B1000,   B0100,   B0010,   B1111,       B0010,
   B0010,   B0100,   B1000,   B0001,   B1001,     B0110,    B1100,       B0011,
   B1010,    B0101,    B1010,     B0101,    B1111,
   B0110,    B0110,    B0110,     B1001,    B1001,      B1001,
   B0010,   B0100,   B1000,   B0001,   B1111,
   B0110,    B0110,    B0110,     B0110,    B0110,      B1001,    B1001,       B1001,      B1001,
   B0010,   B0100,   B1000,   B0001,   B0010,    B1111,
   B0110,    B0110,    B0110,     B1001,    B1001,     B1001,
   B0010,   B0100,   B1000,   B0001,   B1111,
   B0110,    B0110,    B0110,     B0110,    B0110,     B1001,     B1001,       B1001,      B1001,
   B1111,       B1111,       B1100,    B0011,    B1111};

I apologize for not having more to show for the day. I'm trying to make sure I understand the why and the how of all of this.

Yes, a "1" means the corresponding LED is on and "0" means off. If you have the following definition:

#define led0 B0001
#define led1 B0010
#define led2 B0100
#define led3 B1000

Note, the defined name such as "led0" goes before the content such as B0001. When the compiler sees led0, it replaces it with B0001, not the other way around :slight_smile:

You don't have to define more such as led0+led1, the following will do the effect:

(led0|led1)

It's hard to see but I wrote led0 vertical bar led1. The vertical bar does a bit-wise "OR" so that 0001 and 0010 are slapped together, with the "OR" rule. "OR" rule is 1 OR 0 is 1, 1 OR 1 is 1, 0 OR 0 is 0. The result is to collect all the "1's" together.

You learn pretty fast. Already using #define ? I was also hoping that you will get to arrays at some time but you're really pretty fast. So yes I meant to define an array just like the one you mentioned.

So let's do the following for your pins:

int ledPins[] = { 
  3,6,9,11 };       // an array of pin numbers to which LEDs are attached
int pinCount = 4;           // the number of pins (i.e. the length of the array)

Then all you need to do is to decode the numbers such as B0011 and turn on or off certain LED. This can be done with yet another bit-wise operator "AND".

if ((LEDs[0]&led0)!=0) digitalWrite(ledPins[0],HIGH);

The above looks at the first element of LEDs array and decide whether to turn on led0, with the pin number stored in ledPins array. So I took the liberty to incorporate your current code with the blinking in sync and here is the complete code (tested):
Notice there are some changes to the code to take advantage of the ledPins array.

/*
  "Jingle Bells" Melody
 
 Plays "Jingle Bells"
 
 circuit:
 * 8-ohm speaker on digital pin 7
  
 */
#include "pitches.h"

#define led0 B0001
#define led1 B0010
#define led2 B0100
#define led3 B1000

int ledPins[] = { 
  3,6,9,11 };       // an array of pin numbers to which LEDs are attached
int pinCount = 4;           // the number of pins (i.e. the length of the array)

// notes in the melody:
int melody[] = {
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_D4, NOTE_D4, NOTE_D4,
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_E4,
  NOTE_E4, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_FS4,
  NOTE_D5, NOTE_D5, NOTE_C5, NOTE_A4, NOTE_B4, NOTE_D4,
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_D4, NOTE_D4, NOTE_D4,
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_E4, NOTE_E4,
  NOTE_E4, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_D5, NOTE_D5, NOTE_D5, NOTE_D5,
  NOTE_E5, NOTE_D5, NOTE_C5, NOTE_A4, NOTE_G4,
  NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_B4, NOTE_D5, NOTE_G4, NOTE_A4, NOTE_B4,
  NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_B4, NOTE_A4, NOTE_A4, NOTE_B4, NOTE_A4, NOTE_D5,
  NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_B4, NOTE_D5, NOTE_G4, NOTE_A4, NOTE_B4,
  NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_D5, NOTE_D5, NOTE_C5, NOTE_A4, NOTE_G4 };

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {

  8, 8, 8, 8, 3, 16, 16,
  8, 8, 8, 8, 2,
  8, 8, 8, 8, 2, 
  8, 8, 8, 8, 3, 8,
  8, 8, 8, 8, 3, 16, 16,
  8, 8, 8, 8, 3, 8,
  8, 8, 8, 8, 8, 8, 8, 8,
  8, 8, 8, 8, 2,
  8, 8, 4, 8, 8, 4,
  8, 8, 6, 16, 2,
  8, 8, 6, 16, 8, 8, 8, 16, 16,
  8, 8, 8, 8, 4, 4,
  8, 8, 4, 8, 8, 4, 8, 8, 6, 16, 2,
  8, 8, 6, 16, 8, 8, 8, 16, 16,
  8, 8, 8, 8, 2 };

byte LEDs[]={
   B0010,   B0100,   B1000,   B0001,   B1111,       B0001,    B0001,
   B0001,   B1000,   B0100,   B0010,   B1111,
   B0010,   B1000,   B0100,   B0001,   B1111,
   B0001,   B0100,   B1000,   B0010,   B1111,       B0010,
   B0010,   B0100,   B1000,   B0001,   B1111,       B0001,    B0001,
   B0001,   B1000,   B0100,   B0010,   B1111,       B0010,
   B0010,   B0100,   B1000,   B0001,   B1001,     B0110,    B1100,       B0011,
   B1010,    B0101,    B1010,     B0101,    B1111,
   B0110,    B0110,    B0110,     B1001,    B1001,      B1001,
   B0010,   B0100,   B1000,   B0001,   B1111,
   B0110,    B0110,    B0110,     B0110,    B0110,      B1001,    B1001,       B1001,      B1001,
   B0010,   B0100,   B1000,   B0001,   B0010,    B1111,
   B0110,    B0110,    B0110,     B1001,    B1001,     B1001,
   B0010,   B0100,   B1000,   B0001,   B1111,
   B0110,    B0110,    B0110,     B0110,    B0110,     B1001,     B1001,       B1001,      B1001,
   B1111,       B1111,       B1100,    B0011,    B1111};

void setup() {
  for (int i=0;i<pinCount;i++)
  {
    pinMode(ledPins[i],OUTPUT);
    digitalWrite(ledPins[i], LOW);
  }
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 100; thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1800/noteDurations[thisNote];
    // I found that by lengthening the noteDuration,
    // I was able to "slow" the song down.
    tone(7, melody[thisNote],noteDuration);
  
    LEDon(thisNote);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    delay(noteDuration);
    LEDoff(thisNote);

    int pauseBetweenNotes = noteDuration * 0.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(7);
  }
}

void loop() {
  // no need to repeat the melody.
}

int currentLEDpin=3;
void LEDon(int thisNote)
{
  if ((LEDs[thisNote]&led0)!=0) digitalWrite(ledPins[0],HIGH);
  if ((LEDs[thisNote]&led1)!=0) digitalWrite(ledPins[1],HIGH);
  if ((LEDs[thisNote]&led2)!=0) digitalWrite(ledPins[2],HIGH);
  if ((LEDs[thisNote]&led3)!=0) digitalWrite(ledPins[3],HIGH);
}

void LEDoff(int thisNote)
{
  if ((LEDs[thisNote]&led0)!=0) digitalWrite(ledPins[0],LOW);
  if ((LEDs[thisNote]&led1)!=0) digitalWrite(ledPins[1],LOW);
  if ((LEDs[thisNote]&led2)!=0) digitalWrite(ledPins[2],LOW);
  if ((LEDs[thisNote]&led3)!=0) digitalWrite(ledPins[3],LOW);
}

Just played the tune again with my wife, very nice LED sequence! I've wanted to do something like this for a while but never found the time to start. So thank you!

I got a video and a still picture already but this is YOUR project so please consider posting some pictures and videos before I post anything :wink:

Well, I can't say I completely understand all of what I did, but I'm getting there! XD

Honestly, I didn't even know for sure what #define ? meant. I found out how to play sounds using one of the tutorials on arduino.cc, and it said to create a new tab and copy and paste a bunch of #define ? statements, so I did. It wasn't until you brought up arrays and I reread some stuff I'd already read that it started to make sense. I'm still a little lost on some of it, but your explanations have really helped me a lot!

Thanks for fixing the code for me. I wasn't exactly sure where to put some of the parts we'd talked about.

So, how do you go about uploading a video on here? I'm guessing because of file size, you have to upload to YouTube then post a link. Am I correct?

Attached is a picture of my project. It's probably not the "prettiest" layout, and I know now that I can't run that many lights off of each pin. They all work, but the green ones aren't as bright partly because they are at the end of each row in the pattern. I'm going to try to make sense of some of the transistor stuff I've been reading.

I'll upload a couple of my videos tomorrow and link them to the post. Then you can post yours and I can see how it's supposed to look! :stuck_out_tongue_closed_eyes:

Thanks again for all your help!

You don't have to completely understand the code to use it. But when it works, it gives you motive to learn the code.

The" #define X Y" sets up a substitution rule so the X in your code is replaced with Y. Now you know what, but why? Suppose you decide to wire pin 2 instead of pin 3 to led0, now without the led0 definition, you will have to chase down all the "3's" that mean pin 3 and replace with 2. That is difficult and prone to mistake (replaced some wrong 3 or missed a few 3). Now with the #define you just replace the #define and be done.

The forum has no provisions for uploading videos, you will have to have the video hosted elsewhere and post a link. If you upload on youtube.com and post its link here, the forum automatically makes it into a video box like on most news outlet websites.

Here is a video with some sound effect I did a while ago:

All I did was pasting the address like this:

www.youtube.com/watch?v=hJjaIgh4nUc

OK, you just have to remove the https:// when posting to get the video box.

Wow. that's pretty cool. Did you do that with push buttons on each side?

So, here's my video. Like I said before, I know now that I can't run that many LEDs through on pin with just a capacitor. You can see the green and yellows, but not nearly as bright as the reds. I'll have to study how the transistors work and give those a shot sometime. I'm also thinking about adding a few more songs to this now that I know how it works!

www.youtube.com/watch?v=V6TcAhXCL6w

Awesome! I don't see resistors with LEDs though. You could be damaging your board. At least through in a resistor in series with the LEDs. Yeah, you need MOSFET (transistors) to turn that many LEDs. Let me know if you have the next song and LED sequence. It's fun!

Thanks! It was fun putting it together. :slight_smile:

:~ I am a little confused though. What did you mean by you don't see any LEDs? I thought the blinky lights were LEDs? Am I missing something?

Also, I think I misspoke earlier. In my last post, I said

I know now that I can't run that many LEDs through on pin with just a capacitor.

I meant resistor. The kit that I was given that got me into Arduino came with 330ohm resistors (or at least that's what the website says). If you can make it out on the picture, each pin on the Arduino is connected to the yellow breadboard. From there a separate resistor goes to the corresponding bus for each of the four quadrants of LEDs. The red wires are the grounds (there are wires under the Arduino making on long grounded bus on the outermost edge of each side of the breadboard. Essentially I had one resistor for six LEDs coming off of each pin. Is that something that could damage the Arduino?

In reading other forums, I've found one post that talks about using NPN Transistors and a 24v power source to power multiple LEDs. I went ahead and included the schematic they posted. Is this the kind of thing I would need to set up to use more than one LED per pin? Also, would I then run the power directly to the breadboard from, say a battery pack?

My girls got a kick out of watching the lights dance to the music. For now I'm going to work on Christmas songs, mainly ones that they would recognize. I'll let you know as soon as I get another song together!

Sorry it was typo. I meant resistors. I think the Arduino pin needs a resistor too although I am unsure.

Sorry about the delay. I can't remember my password, so I can only get into the forum on one of my computers. LOL

So which part do I need to put a resistor on? I have resistors for each LED pin, and one for the piezo. Right now the Arduino is getting its power from the USB cable. It's sitting on the larger breadboard, but it's not receiving any power from it.

Please let me know where you'd recommend putting another resistor. I haven't used it since your last post because I really don't want to fry my Arduino!

As a side note, with all things Thanksgiving around here, I haven't had a chance to add any more code for other songs yet. Hopefully I'll have more coming soon...

Never mind. I thought you didn't use resistors. Happy turkey holiday!

This is very similar to what I was trying to do. This helped me very much. Thanks

liuzengqiang:
Yes, it is possible but post your buzzer code. In principal you can have a play function that takes a note and play it, while cycling through leds. The play function wil have a static variable to store the next led to be turned on.

Is it possible to do this I'm reverse? Like the buzzer will play the song and the led will stay solid?