// Arduino Mini Piano - ozandamci.com
const int buttons[7] = {2, 3, 4, 5, 6, 7, 8};
const int notes[7] = {262, 294, 330, 349, 392, 440, 494}; // C D E F G A B
const int buzzerPin = 9;
void setup() {
for (int i = 0; i < 7; i++) {
pinMode(buttons[i], INPUT_PULLUP);
}
pinMode(buzzerPin, OUTPUT);
startupTune();
}
void startupTune() {
int melody[] = {262, 330, 392, 523};
for (int i = 0; i < 4; i++) {
tone(buzzerPin, melody[i], 150);
delay(200);
}
}
void loop() {
int noteToPlay = 0;
// Find the first button that is pressed
for (int i = 0; i < 7; i++) {
if (digitalRead(buttons[i]) == LOW) {
noteToPlay = notes[i];
break;
}
}
if (noteToPlay > 0) {
tone(buzzerPin, noteToPlay);
} else {
noTone(buzzerPin);
}
}