Go Down

Topic: **UPDATE** PROBLEM SOLVED (Read 531 times) previous topic - next topic

trogers123

Dec 30, 2012, 06:28 am Last Edit: Dec 30, 2012, 08:43 am by tonyrogers10195 Reason: 1
Thanks for the help but just needed to take a break from it and come back.


Ok I'm new to Arduino and programming in general, but i am capable and really interested in electronics and really want to learn.

So I'm staring off trying to make a little thing that plays this->http://arduino.cc/en/Tutorial/Tone melody and have a separate LED blink for each note after a button is pushed.



I understand all the wiring and had it set up to play the melody on the button push but im having trouble getting the LEDS to sync with the notes, could someone please help me with this:



Code: [Select]
#include "pitches.h"
int button = 2;
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;

int speaker = 8;
int melody[] = {
 NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
int noteDurations[] = {
 4, 8, 8, 4,4,4,4,4 };
 
int ledsequence[] = {
 led1, led2, led2, led3, led2, led4, led1};


void setup() {
 Serial.begin(9600);
 pinMode(button, INPUT_PULLUP);
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(led4, OUTPUT);
 pinMode(speaker, OUTPUT);
}
void loop()
{
 int state = digitalRead(button);
 Serial.println(state);
 delay(5);

 
 if (state == 0)
 {
   for (int thisNote = 0; thisNote < 8; thisNote++) {
   
   int noteDuration = 1000/noteDurations[thisNote];
   
   int pauseBetweenNotes = noteDuration * 1.30;
   
   digitalWrite(ledsequence[thisNote],HIGH);
   
   delay(noteDuration);
 
   digitalWrite(ledsequence[thisNote],LOW);
   
   delay(pauseBetweenNotes);
           
   tone(8, melody[thisNote],noteDuration);

   delay(pauseBetweenNotes);
   
   }
 }
}

James C4S

You haven't posted all of your code.  Nor have you said what is happening along with what you expect.  That makes it difficult to help.

One thing I did notice is in your for() loop, the variable thisNote will go from 0 through 7 (inclusive).  Your ledsequence array only has 7 elements so 0 through 6 (inclusive) are the only addressable elements.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

marco_c

#2
Dec 30, 2012, 07:04 am Last Edit: Dec 30, 2012, 07:24 am by marco_c Reason: 1
I would set the led high, play the note including the note delay, then turn the led off.

Btw, you are using the numeral 8 instead of speaker in the tone function call.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

trogers123

As i said i am very new to this, but that is all the code i have, it compiles and uploads, what am i missing?... and my goal is to have each LED blink with each note in sync, right now it alternates between a note and blink.

I tried to shortcut it and don't exactly know what I'm doing. One thing that would help is if you could explain how I could get one LED to blink at the same time as the tone and I could probably figure it out from there.

marco_c

If you are playing music you can't add in additional delays, so the suggestion is that you turn the led on just before you play the tone, then turn it off straight after you have paused between notes. This automatically synchronizes as the led can only be on while the sound is on.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

trogers123

how would i do that with strings? or would i have do each and led individually?

lloyddean

Code: [Select]

    {
        int noteDuration;
        int pauseBetweenNotes;
       
        for (int i = 0; i < 8; i++)
        {
            noteDuration = 1000 / noteDurations[i];
       
            digitalWrite(ledsequence[i], HIGH);
       
            tone(8, melody[i],noteDuration);
       
            digitalWrite(ledsequence[i], LOW);
           
            pauseBetweenNotes = noteDuration * 1.30;
            delay(pauseBetweenNotes);
        }
    }


marco_c

#7
Dec 30, 2012, 07:38 am Last Edit: Dec 30, 2012, 07:47 am by marco_c Reason: 1
What strings?

You are in a for loop in your code. You are already doing what you need to do but in the wrong sequence, which is what you say you see. Look at the code and read the other messages ...

Edit: lloyddean has given the solution.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

lloyddean

I recommend you keep your note event data together, this keep them in sync.

Also the count of events can be determined, and applied, at compile time,

Code: [Select]

#include "pitches.h"

#define ENTRY_COUNT(ARRAY)    (sizeof(ARRAY) / sizeof(ARRAY[0]))

struct event_t
{
   int _note;
   int _length;
   int _pin;
};

int button = 2;
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;

int speaker = 8;

event_t sequence[] =
{
     { NOTE_C4, 4, led1 }
   , { NOTE_G3, 8, led2 }
   , { NOTE_G3, 8, led2 }
   , { NOTE_A3, 4, led3 }
   , { NOTE_G3, 4, led2 }
   , {       0, 4, led4 }
   , { NOTE_B3, 4, led1 }
   , { NOTE_C4, 4, led1 }
};


void setup()
{
   Serial.begin(9600);
   
   pinMode(button, INPUT_PULLUP);
   pinMode(led1, OUTPUT);
   pinMode(led2, OUTPUT);
   pinMode(led3, OUTPUT);
   pinMode(led4, OUTPUT);
   pinMode(speaker, OUTPUT);
}

void loop()
{
   int state = digitalRead(button);
   Serial.println(state);

   delay(5);
   
   if ( state == 0 )
   {
       int noteDuration;
       int pauseBetweenNotes;

       for ( int i = 0; i < ENTRY_COUNT(sequence); i++)
       {
           noteDuration        = 1000 / sequence[i]._length;
           pauseBetweenNotes   = noteDuration * 1.30;
           
           digitalWrite(sequence[i]._pin, HIGH);

           tone(8, sequence[i]._note, noteDuration);

           digitalWrite(sequence[i]._pin, LOW);
           delay(pauseBetweenNotes);
       }
   }
}


Go Up