Hi everyone so im working on a piano type toy, where when I press a button a song plays with a piezo speaker. I got that code working and now want to add in 8 different leds corresponding to the 8 different notes that are used in the song. I want 1 led to___turn on/have the notes play/turn off___for every note using the 8 different leds. Is there a way to write the code so that the follwoing works?
if (piezo plays NOTE_C5)
{then led1 on}else{led1 off}
if (piezo plays NOTE_D5)
{then led2 on}else{led2 off}
if (piezo plays NOTE_E5)
{then led3 on}else{led3 off}
if (piezo plays NOTE_F5)
{then led4 on}else{led4 off}
etc...
Is there a way to write the code so that the follwoing works?
What you have already posted is a start in the right direction but in order to provide more help please post the complete program as it is now.
Ok here it is also I want to explain the code one more time. If one button is held down and I press buttons 1-8 single notes sound. If I hold down another button and press buttons 1-8 then songs play. I already have the leds blinking to the singles notes played but now I want the leds to also blink when the song is played corressponding to notes such as C,D,G,F,E etc..
Piano_1.0.6.ino (11.4 KB)
pitches.h (1.83 KB)
You could use a switch/case with the note as the switch variable controlling the appropriate LED
Im sorry could you please explain a little further im still somewhat new to coding and although my code is lengthy it took me a long time and a lot of trail and error in addition to taking bits and pieces of other peoples code and adapting it to mine to right
also I want to ask if there is a certain phrase of code that says
if piezo PLAYS this note then this
if piezo PLAYS this note then this
You could simply use 8 if statements based on the note being played which would be the equivalent of the switch/case, but you might find it easier to understand.
ok yeah that was what I was thinking but given the way that I wrote the code to play the notes how would you write
if (piezo plays this note)
{ then this }{else this}
im not sure how to write the "plays" part if there is a certain function or something that I have to use
tone(piezoPin, melody1[thisNote], noteDuration*1.30);
This line of code plays the note so you could test the value of the note being played and turn on the appropriate LED and turn it off afterwards