const int buttonPins[8] = {2,3,4,5,6,7,12,9};
const int buzzerPin = 10;
const int potPin = A5;
// Note Frequencies (C D E F G A B High C)
int notes[8] = {262, 294, 330, 349, 392, 440, 494, 523};
void setup() {
for(int i = 0; i < 8; i++){
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Read potentiometer
int potValue = analogRead(potPin);
// Convert 0–1023 into pitch multiplier (0.5x to 2.0x)
float pitchFactor = map(potValue, 0, 1023, 50, 200) / 100.0;
bool notePlaying = false;
// Check each button
for(int i = 0; i < 8; i++){
if(digitalRead(buttonPins[i]) == LOW){ // LOW = pressed
int adjustedFrequency = notes[i] * pitchFactor;
tone(buzzerPin, adjustedFrequency);
notePlaying = true;
break; // Only one note at a time
}
}
// If no buttons pressed then stop sound
if(!notePlaying){
noTone(buzzerPin);
}
}