/**
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.
*/
#include "pitches.h"
#include <LiquidCrystal.h>
#define SPEAKER_PIN 8
const uint8_t buttonPins[] = { 12, 11, 10, 9, 7, 6, 5, 4 };
const int buttonTones[] = {
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
const int numTones = sizeof(buttonPins) / sizeof(buttonPins[0]);
LiquidCrystal lcd(13, 3, 2, A5, A4, A3);
void setup() {
for (uint8_t i = 0; i < numTones; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
Serial.begin(9600);
lcd.begin(16, 4);
lcd.clear();
}
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);
}
if (digitalRead(NOTE_C4) == HIGH) {
lcd.setCursor(0, 0);
lcd.print("Button 1 pressed");
} else if (digitalRead(NOTE_D4) == HIGH) {
lcd.setCursor(0, 0);
lcd.print("Button 2 pressed");
} else if (digitalRead(NOTE_E4) == HIGH) {
lcd.setCursor(0, 0);
lcd.print("Button 3 pressed");
} else if (digitalRead(NOTE_F4) == HIGH) {
lcd.setCursor(0, 0);
lcd.print("Button 4 pressed");
} else if (digitalRead(NOTE_G4) == HIGH) {
lcd.setCursor(0, 0);
lcd.print("Button 5 pressed");
} else if (digitalRead(NOTE_A4) == HIGH) {
lcd.setCursor(0, 0);
lcd.print("Button 6 pressed");
} else if (digitalRead(NOTE_B4) == HIGH) {
lcd.setCursor(0, 0);
lcd.print("Button 7 pressed");
} else if (digitalRead(NOTE_C5) == HIGH) {
lcd.setCursor(0, 0);
lcd.print("Button 8 pressed");
}
}