/**
Three-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 3 to play the piano (1 is the lowest note,
3 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 10, 2024
for CS M125 at Moorpark College in California.
The goal of this modified code is to teach students about pointers.
*/
// 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)
int BUZZER_PIN = 8;
// Red piano key is wired to Digital Pin 12 on your Arduino Uno
int PIANO_PIN_1 = 12; // red piano-key pin number
// Orange piano key is wired to Digital Pin 11 on your Arduino Uno
int PIANO_PIN_2 = 11; // orange piano-key pin number
// Yellow piano key is wired to Digital Pin 10 on your Arduino Uno
int PIANO_PIN_3 = 10; // yellow piano-key pin number
/********* MUSICAL NOTES ASSIGNMENT **********/
// Musical notes for the piano keys
// C4 (C tone, fourth octave) is created by buzzing the buzzer at 262 Hz
int NOTE_1 = 262; // frequency (Hz) for red piano key
// D4 (D tone, fourth octave) is created by buzzing the buzzer at 294 Hz
int NOTE_2 = 294; // frequency (Hz) for orange piano key
// E4 (E tone, fourth octave) is created by buzzing the buzzer at 330 Hz
int NOTE_3 = 330; // frequency (Hz) for yellow piano key
/********* DEVICE INITIALIZATION FUNCTIONS **********/
void initPianoKey(int* pinPointer) {
// Initialize one piano key (its pin information is "passed by pointer")
int pin = *pinPointer;
pinMode(pin, INPUT_PULLUP);
}
void initBuzzer(int pin) {
// Initialize the buzzer speaker (its pin information is "passed by value")
int* pinPointer = &pin;
pinMode(*pinPointer, OUTPUT);
}
bool isKeyPressed(int& pin) {
// Check if key is depressed (its pin information is "passed by reference")
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() {
initPianoKey(&PIANO_PIN_1); // red key
initPianoKey(&PIANO_PIN_2); // orange key
initPianoKey(&PIANO_PIN_3); // yellow key
initBuzzer(BUZZER_PIN); // buzzer
}
// In Arduino, the loop() method is repeatedly called in an infinite while loop, once setup is completed
void loop() {
// Check whether any piano keys are depressed, and buzz accordingly
if (isKeyPressed(PIANO_PIN_1)) { // red key is pressed
tone(BUZZER_PIN, NOTE_1); // buzz out the musical note for the red key
}
else if (isKeyPressed(PIANO_PIN_2)) { // orange key is pressed
tone(BUZZER_PIN, NOTE_2); // buzz out the musical note for the orange key
}
else if (isKeyPressed(PIANO_PIN_3)) { // yellow key is pressed
tone(BUZZER_PIN, NOTE_3); // buzz out the musical note for the yellow key
}
else { // no keys are pressed
noTone(BUZZER_PIN); // silence the buzzer
}
}
/********* 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();
}
}
*/