Hello all I am trying to have the arduino turn on an LED while a note plays. My goal is to have each note have its own designated LED so that when that specific note plays the corresponding LED also turns on. To do this I attempted to use if/else statements for each note and programmed the led to turn on, delay, turn off if that specific note was playing. So far I have only been able to get the "break" led to turn on when there is a break between the notes playing then starting over, all the other LEDs dont turn on at all while the notes are playing.
PS: All of the code used to play the notes is not original, for our assignment we were only told to have the lights correspond to the music
Any help is greatly appreciated
//pin to use to drive speaker
int speakerPin = 10;
int ledPins[] = {2,3,4,5,6,7,8,9};
// next line lists notes we want to be able to play
char names[] = {'c','d','e','f','g','a','b','C','D','E','F','G','A','B'};
//number of notes available in the above array
int rangeOfNotes = 14;
//next line lists values corresponding to note names in names[] array
int tones[] ={1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 850, 760, 716, 638, 568, 507};
// the number of notes and rests I want to play in my song
int length = 15;
// notes to play. A space represents a rest
char notes[] ="ccggaagffeeddc ";
/*next line lists how long to play each note or rest.(it has to be a double rather than an int to allow for decimals)*/
double beats[] = {1.5, .5, 1, 1, 1, 1, 2, 1.5, .5, 1, 1, 1, 1, 2, 4};
//how long one beat is in milliseconds
int tempo = 300;
void setup()
{
pinMode(speakerPin,OUTPUT);
for(int i = 0; i < 8; i++)
{
pinMode(ledPins[i], OUTPUT);
}
}
void loop()
{
for(int i = 0; i < length; i++)
{
//for each note in the character array (string)
if(notes[i] ==' ')
{
digitalWrite( ledPins[0], HIGH);
//if the character is a space, do the following line.
delay(beats[i] * tempo);
//rest (there is a pause in the music)
digitalWrite( ledPins[0], LOW);
}
else
{
//otherwise (if the character is not a space), do the following line.
playNote(notes[i], beats[i] * tempo);
//play the note notes[i] for the ammount of time beats[i] * tempo, by using the playNote function defined below
if(notes[i] =='a','A')
{
digitalWrite( ledPins[1], HIGH);
delay(beats[i] * tempo);
digitalWrite( ledPins[1], LOW);
}
else if(notes[i] =='b','B')
{
digitalWrite( ledPins[2], HIGH);
delay(beats[i] * tempo);
digitalWrite( ledPins[2], LOW);
}
else if(notes[i] =='c','C')
{
digitalWrite( ledPins[3], HIGH);
delay(beats[i] * tempo);
digitalWrite( ledPins[3], LOW);
}
else if(notes[i] =='d','D')
{
digitalWrite( ledPins[4], HIGH);
delay(beats[i] * tempo);
digitalWrite( ledPins[4], LOW);
}
else if(notes[i] =='e','E')
{
digitalWrite( ledPins[5], HIGH);
delay(beats[i] * tempo);
digitalWrite( ledPins[5], LOW);
}
else if(notes[i] =='f','F')
{
digitalWrite( ledPins[6], HIGH);
delay(beats[i] * tempo);
digitalWrite( ledPins[6], LOW);
}
else if(notes[i] =='g','G')
{
digitalWrite( ledPins[7], HIGH);
delay(beats[i] * tempo);
digitalWrite( ledPins[7], LOW);
}
}
//delay(tempo/2);//if you want to include a pause between notes, but throws rhythm off.
}
}
void playNote(char note,int duration){
//plays the note "note" for the time "duration"
//play the tone corresponding to the note name
for(int i = 0; i < rangeOfNotes; i++)
{
if(names[i] == note)
{
playTone(tones[i], duration);
}
}
}
void playTone(int tone, int duration)
{
for(long i = 0; i < duration *1000L; i +=tone* 2)
{
digitalWrite(speakerPin,LOW);
delayMicroseconds(tone);
digitalWrite(speakerPin,HIGH);
delayMicroseconds(tone);
}
}