/**
Mini piano for Arduino.
You can control the colorful buttons with your keyboard:
After starting the simulation, click anywhere in the diagram to focus it.
Then press any key between 1 and 8 to play the piano (1 is the lowest note,
8 is the highest).
Copyright (C) 2021, Uri Shaked. Released under the MIT License.
OK changes
*/
// #include "ESP32Tone.h" // OK: this does not work when copied (for me it did)
// Use below instead and update imports to have ToneESP32 instead
#include "ToneESP32.h"
#include "pitches.h"
#define SPEAKER_PIN 2
// const uint8_t buttonPins[] = { 12, 11, 10, 9, 7, 6, 5, 4 };
const uint8_t buttonPins[] = { 12, 16, 11, 15, 10, 9, 14, 7, 13, 6, 3, 5, 4 };
const int buttonTones[] = {
NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4,
NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4, NOTE_C5
};
const int numTones = sizeof(buttonPins) / sizeof(buttonPins[0]);
void setup() {
// OK: We have to wait until serial is ready...
while (!Serial) delay(10);
Serial.begin(115200);
Serial.println("My piano");
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];
Serial.print("Pitch: ");
Serial.println(pitch);
break;
}
}
if (pitch) {
tone(SPEAKER_PIN, pitch);
} else {
noTone(SPEAKER_PIN);
}
delay(20);
}