const int buzzerPin = 9;
const int button1Pin = 2;
const int button2Pin = 3;
const int button3Pin = 4;
const int button4Pin = 5;
const int button5Pin = 6;
const int C4 = 262;
const int D4 = 294;
const int E4 = 330;
const int F4 = 349;
const int G4 = 392;
const int noteDuration = 150;
int button1State = 0;
int button2State = 0;
int button3State = 0;
int button4State = 0;
int button5State = 0;
void setup() {
Serial.begin(9600);
Serial.println("Mini Musical Instrument Ready!");
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
pinMode(button4Pin, INPUT_PULLUP);
pinMode(button5Pin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
button3State = digitalRead(button3Pin);
button4State = digitalRead(button4Pin);
button5State = digitalRead(button5Pin);
if (button1State == LOW) {
Serial.println("Playing C4");
tone(buzzerPin, C4, noteDuration);
delay(noteDuration + 10);
}
if (button2State == LOW) {
Serial.println("Playing D4");
tone(buzzerPin, D4, noteDuration);
delay(noteDuration + 10);
}
if (button3State == LOW) {
Serial.println("Playing E4");
tone(buzzerPin, E4, noteDuration);
delay(noteDuration + 10);
}
if (button4State == LOW) {
Serial.println("Playing F4");
tone(buzzerPin, F4, noteDuration);
delay(noteDuration + 10);
}
if (button5State == LOW) {
Serial.println("Playing G4");
tone(buzzerPin, G4, noteDuration);
delay(noteDuration + 10);
}
delay(5);
}