#include "pitches.h"
#define SPEAKER_PIN 8
#define NUM_BUTTONS 8
const uint8_t buttonPins[NUM_BUTTONS] = { 12, 11, 10, 9, 7, 6, 5, 4 };
const uint8_t ledPins[NUM_BUTTONS] = { A0, A1, A2, A3, A4, A5, 3, 2 };
const int buttonTones[] = {
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
const int numTones = sizeof(buttonPins) / sizeof(buttonPins[0]);
void setup() {
for (uint8_t i = 0; i < NUM_BUTTONS; i++) {
pinMode(buttonPins[i], INPUT_PULLUP); // Set each button pin as input with pull-up
pinMode(ledPins[i], OUTPUT); // Set each LED pin as output
digitalWrite(ledPins[i], LOW); // Turn off LEDs initially
}
pinMode(SPEAKER_PIN, OUTPUT); // Set speaker pin as output
}
void loop() {
int pitch = 0;
for (int i = 0; i < NUM_BUTTONS; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
pitch = buttonTones[i]; // Set pitch according to the pressed button
digitalWrite(ledPins[i], HIGH); // Turn on the corresponding LED
} else {
digitalWrite(ledPins[i], LOW); // Turn off the LED if button is not pressed
}
}
if (pitch) {
tone(SPEAKER_PIN, pitch); // Play tone if pitch is set
} else {
noTone(SPEAKER_PIN); // Stop tone if no button is pressed
}
}