// Global variables
const int buttonPins[] = {0,1,2,3,4,5,6};
const int ledPin = 7;
const int buzzerPin = 9;
// Note frequencies for each button
const int noteFrequencies[] = {
262, 294, 330, 349, 392, 440, 494};
int buttonState[] = {HIGH, HIGH, HIGH, HIGH,HIGH, HIGH,HIGH};
int prevButtonState[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH,HIGH};
int noteDuration = 250;
int melodyLength = 7;
void setup(){
for(int i = 0; i < 7; i++){
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
for(int i = 0; i < 7;i++){
buttonState[i] = digitalRead(buttonPins[i]);
if(buttonState[i] == LOW && prevButtonState [i] == HIGH){
tone(buzzerPin, noteFrequencies[i], noteDuration);
digitalWrite(ledPin, HIGH);
}
if(buttonState[i] == HIGH && prevButtonState [i] == LOW){
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
}
//store the current state
prevButtonState[i] = buttonState[i];
}
}