// https://wokwi.com/projects/427049511998951425
// Using an array of buttons
// See also forum discussion at
// https://forum.arduino.cc/t/simple-keyboard-using-the-tone-function/1369567
// Adapted from Arduino Built-in example Keyboard
// https://wokwi.com/projects/409953647477353473
// Code from https://docs.arduino.cc/built-in-examples/digital/toneKeyboard
// Other simulations https://forum.arduino.cc/t/wokwi-simulations-for-arduino-built-in-examples/1304754
/*
Keyboard
Plays a pitch that changes based on a button press
circuit:
- five buttons from ground to A0 to A5
- 8 ohm speaker on digital pin 8
modified 2025-04-01
created 21 Jan 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneKeyboard
*/
#include "pitches.h"
const int numberOfKeys = 6;
const int pressedLevel = LOW; // reading of the keys that generates a note
// notes to play, corresponding to the keys:
int notes[] = {
NOTE_D4, NOTE_E4, NOTE_C4, NOTE_C3, NOTE_G3, NOTE_C3
};
const int buttons[] = {
A0, A1, A2, A3, A4, A5
};
void setup() {
for (int thisKey = 0; thisKey < numberOfKeys; thisKey++) {
pinMode(buttons[thisKey], INPUT_PULLUP);
}
}
void loop() {
for (int thisKey = 0; thisKey < numberOfKeys; thisKey++) {
// get a sensor reading:
int sensorReading = digitalRead(buttons[thisKey]);
// if the sensor is pressed:
if (sensorReading == pressedLevel) {
// play the note corresponding to this sensor:
tone(8, notes[thisKey], 20);
}
}
}