I am new to Arduino and enjoying the Starters Kit. I also have a kid, so I adjusted Project 07 Keyboard Instrument slightly so I could play "Mary had a little lamb".
I made two tweaks to the code, compared to the code in the book;
- Replaced the note F (349Hz) with the note G (392Hz)
- Print the note letter instead of value of the key
Here is the code if you want to play Mary had a little lamb:
int notes[] = {262,294,330,392}; //CDEG
void setup() {
Serial.begin(9600);
}
void loop() {
int keyValue = analogRead(A0);
if(keyValue == 1023) {
tone(8, notes[0]);
Serial.println("C");
}
else if(keyValue >= 990 && keyValue <= 1010) {
tone(8, notes[1]);
Serial.println("D");
}
else if (keyValue >= 505 && keyValue <= 515) {
tone(8, notes[2]);
Serial.println("E");
}
else if(keyValue >= 5 && keyValue <= 10) {
tone(8, notes[3]);
Serial.println("G");
}
else{
noTone(8);
}
}