#include <LiquidCrystal.h>
#include "notes.h"
#define BUZZER_PIN 2
LiquidCrystal lcd (A0, A1, A2, A3, A4, A5);
int buttonPins[] = { 8, 9, 10, 11 };
int ledPins[] = { 4, 5, 6, 7 };
const int numButtons = sizeof(buttonPins) / sizeof(buttonPins[0]);
const int numLeds = sizeof(ledPins) / sizeof(ledPins[0]);
const int notes[] = { NOTE_G4, NOTE_F4, NOTE_E4, NOTE_AS3 };
const String notesName[] = { "Note: G4 ", "Note: F4 ", "Note: E4 ", "Note: AS3 " };
void setup() {
for (int i = 0; i < numButtons; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(buttonPins[0], INPUT_PULLUP);
pinMode(buttonPins[1], INPUT_PULLUP);
pinMode(buttonPins[2], INPUT_PULLUP);
pinMode(buttonPins[3], INPUT_PULLUP);
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(BUZZER_PIN, OUTPUT);
lcd.begin(16,2);
lcd.print("Music!");
tone(BUZZER_PIN, NOTE_E4);
delay(100);
noTone(BUZZER_PIN);
delay(50);
tone(BUZZER_PIN, NOTE_F4);
delay(100);
noTone(BUZZER_PIN);
}
void loop() {
lcd.setCursor(0,1);
int note = 0;
String noteName;
int led;
for (int i = 0; i < numButtons; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
note = notes[i];
noteName = notesName[i];
led = ledPins[i];
}
}
if (note) {
tone(BUZZER_PIN, note);
digitalWrite(led, HIGH);
lcd.print(noteName);
} else {
noTone(BUZZER_PIN);
digitalWrite(led, LOW);
lcd.print("Note: NULL");
}
}