/**
Eight-key piano for Arduino Uno
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).
Original code was copyright (C) 2021, Uri Shaked. Released under the MIT License.
Original code is found here: https://wokwi.com/projects/291958456169005577
Uri Shaked's code was heavily modified by Dr. Scott Feister, July 19, 2024
for CS M125 at Moorpark College in California.
The goal of this modified code is to teach students about C-style arrays.
*/
// NOTE FROM DR. FEISTER
// The Arduino IDE hides some include statements from the programmer.
// One include statement gives you access to all methods defined at https://www.arduino.cc/reference/en/
// If you could see this hidden include statement, it would look something like this:
/*
#include "arduino.h"
*/
/********* PIN ASSIGNMENT **********/
// Digital Pin 8 on your Arduino Uno is connected to a buzzer speaker
// (buzzes at different frequencies to create different tones)
#define BUZZER_PIN 8
// Digital Pins 12,11,10,9,7,6,5,4 on your Arduino Uno are connected to piano buttons
// (Note that Digital Pin 8 is skipped since it is already in use by the buzzer speaker)
const int PIANO_PIN_0 = 12;
const int PIANO_PIN_1 = 11;
const int PIANO_PIN_2 = 10;
const int PIANO_PIN_3 = 9;
const int PIANO_PIN_4 = 7;
const int PIANO_PIN_5 = 6;
const int PIANO_PIN_6 = 5;
const int PIANO_PIN_7 = 4;
const int PIANO_PINS[8] = {PIANO_PIN_0, PIANO_PIN_1, PIANO_PIN_2, PIANO_PIN_3, PIANO_PIN_4, PIANO_PIN_5, PIANO_PIN_6, PIANO_PIN_7};
// Musical notes for the piano keys
// C4 (C tone, fourth octave) is created by buzzing the buzzer at 262 Hz
// D4 (D tone, fourth octave) is created by buzzing the buzzer at 294 Hz
// E4 (E tone, fourth octave) is created by buzzing the buzzer at 330 Hz
// ...
// C5 (C tone, fifth octave) is created by buzzing the buzzer at 523 Hz
const int NOTE_0 = 262;
const int NOTE_1 = 295;
const int NOTE_2 = 335;
const int NOTE_3 = 349;
const int NOTE_4 = 392;
const int NOTE_5 = 440;
const int NOTE_6 = 494;
const int NOTE_7 = 523;
const int NOTES[8] = {NOTE_0, NOTE_1, NOTE_2, NOTE_3, NOTE_4, NOTE_5, NOTE_6, NOTE_7};
/********* DEVICE INITIALIZATION FUNCTIONS **********/
void initPianoKey(int pin) {
// Initialize one piano key
pinMode(pin, INPUT_PULLUP);
}
void initBuzzer(int pin) {
// Initialize the buzzer speaker
pinMode(pin, OUTPUT);
}
bool isKeyPressed(int pin) {
// Check if key is depressed
if (digitalRead(pin) > 0) {
return false; // key is currently not depressed
} else {
return true; // key is currently depressed
}
}
/********* REQUIRED ARDUINO FUNCTIONS *********/
// In Arduino, the setup() method runs first, once per power cycle of the Arduino
void setup() {
for (int i = 0; i < 8; i++){
initPianoKey(PIANO_PINS[i]);
}
initBuzzer(BUZZER_PIN); // buzzer speaker
}
// In Arduino, the loop() method is repeatedly called in an infinite while loop, once setup is completed
void loop() {
bool keyPressed = false; // true if *any* piano key is currently pressed
// initialized to "false" and then is
// re-assigned to "true" if key press is detected
// Check whether any piano keys are depressed, and buzz accordingly
for (int i = 0; i < 8; i++){
if (isKeyPressed(PIANO_PINS[i])) { // piano key #0 is pressed
tone(BUZZER_PIN, NOTES[i]); // buzz out the musical note corresponding to piano key #0
keyPressed = true;
}
}
if (!keyPressed) { // no keys are pressed
noTone(BUZZER_PIN); // silence the buzzer speaker
}
}
/********* MAIN FUNCTION (Hidden) *********/
// NOTE FROM DR. FEISTER
// The Arduino IDE hides the main() function from the programmer.
// If you could see the main function, it would look something like this:
/*
int main() {
setup();
while (true) {
loop();
}
}
*/