/**
Mini 3-Button Piano for Arduino.
You can control the 3 buttons with your keyboard:
After starting the simulation, click anywhere in the diagram to focus it.
Then press any key between 1 and 3 to play the piano (1 is the lowest note,
3 is the highest).
Copyright (C) 2023, [Your Name].
Released under the MIT License.
*/
#include "pitches.h"
#define SPEAKER_PIN 8
const uint8_t buttonPins[] = { 4, 5, 6 };
const int buttonTones[] = {
NOTE_C4, NOTE_E4, NOTE_G4
};
const int numTones = sizeof(buttonPins) / sizeof(buttonPins[0]);
void setup() {
for (uint8_t i = 0; i < numTones; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
}
void loop() {
int pitch = 0;
for (uint8_t i = 0; i < numTones; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
pitch = buttonTones[i];
}
}
if (pitch) {
tone(SPEAKER_PIN, pitch);
} else {
noTone(SPEAKER_PIN);
}
}