I have a circuit useing the arduino lilypad snap and i want the code to detect when a sound is being played and then light up an LED. I wanted to have it do this so that i do not have to write out each led turning on and off. The code is
#include "pitches.h"
int sound = 0;
int led = 5;
int melody[] = {
NOTE_E4,NOTE_D4,NOTE_C4,NOTE_D4,NOTE_E4,NOTE_E4,NOTE_E4,NOTE_D4,NOTE_D4,NOTE_D4,NOTE_E4,NOTE_G4,NOTE_G4,
NOTE_E4,NOTE_D4,NOTE_C4,NOTE_D4,NOTE_E4,NOTE_E4,NOTE_E4,NOTE_E4,NOTE_D4,NOTE_D4,NOTE_E4,NOTE_D4,NOTE_C4};
int noteDurations[] = {
4,4,4,4,4,4,2,4,4,2,4,4,2,4,4,4,4,4,4,4,4,4,4,4,4,2};
void loop() {
pinMode(led, OUTPUT);
sound = digitalRead (9);
if ( sound == HIGH){
digitalWrite (led, HIGH);
}
else if(sound == LOW){
digitalWrite (led, LOW);
}
}
void setup() {
for (int thisNote = 0; thisNote < 26; thisNote++) {
int noteDuration = 1000/noteDurations[thisNote];
tone(9, melody[thisNote],noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
}
}
Any help you can provide would be greatly appreciated.