Hi Newbie here.probs with led and melody freezing at end of loop

Hi what instruction do i have to programme so when i hear a note from buzzer speaker, it is synced to an led? thanks
I have done this... A new prob in my 2nd post

Not much of that makes sense, I think you need to spell the requirements out more clearly.


Rob

sorry, ok i have done this now.. but the lights and the tones get stuck after one play...
i have basically taken examples of this forum and changed the code to fit my melody..perhaps you can tell me why the leds and notes don't go off at the end of the 5 note melody?

Thanks!
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,4,5,6}; // 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_E4, NOTE_C4, NOTE_C3,NOTE_G3
};

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

4, 4, 4, 4, 1};

byte LEDs[]={
B0010, B0100, B1000, B0001, B1111};

void setup() {
for (int i=0;i<pinCount;i++)
{
pinMode(ledPins*,OUTPUT);*
_ digitalWrite(ledPins*, 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);*
}

Getting there, now add code tags. Edit the post, select the code, and click the # button in the tool bar.


Rob

Eight ohm speaker does have a 180 ohm resistor (and preferably a capacitor) in series, doesn't it?