/**
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 pitch definitions for musical notes
// Pin assignments for piezo sensors
const int piezoPins[] = { A0, A1, A2, A3, A4, A5, A6 }; // 7 analog pins for piezo sensors
const int numKeys = sizeof(piezoPins) / sizeof(piezoPins[0]);
// Corresponding musical tones for each key
const int pianoTones[] = {
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4
};
// Speaker pin for audio output
#define SPEAKER_PIN 8
// Threshold for impact detection
const int impactThreshold = 10; // Adjust this value as needed based on sensor response
void setup() {
Serial.begin(9600); // Start serial communication for debugging
// Set piezo pins as input
for (int i = 0; i < numKeys; i++) {
pinMode(piezoPins[i], INPUT);
}
pinMode(SPEAKER_PIN, OUTPUT);
Serial.println("Floor Piano Ready: Step on the sensors to play notes!");
}
void loop() {
for (int i = 0; i < numKeys; i++) {
int sensorValue = analogRead(piezoPins[i]);// checks presses on each key
if (sensorValue > impactThreshold) { // Check if the impact exceeds the threshold
tone(SPEAKER_PIN, pianoTones[i]); // Play the corresponding tone
Serial.print("Key ");
Serial.print(i + 1);
Serial.print(" activated with value: ");
Serial.println(sensorValue);
delay(300); // Duration of note
noTone(SPEAKER_PIN);
}
}
int potValue = analogRead(A0); // Read the potentiometer value
float voltage = potValue * (5.0 / 1023.0); // Convert to voltage
Serial.print("Potentiometer Voltage: ");
Serial.println(potValue);
delay(1000);
}