#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "pitches.h"
#define SPEAKER_PIN 8
const uint8_t buttonPins[] = { 12, 11, 10, 9, 7, 6, 5, 4 };
const int buttonTones[][8] = {
{ NOTE_E4, NOTE_G4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_G5, NOTE_A5, NOTE_C6 }, // Tonal pattern for button 1
{ NOTE_F4, NOTE_A4, NOTE_C5, NOTE_D5, NOTE_F5, NOTE_A5, NOTE_B5, NOTE_D6 }, // Tonal pattern for button 2
{ NOTE_G4, NOTE_B4, NOTE_D5, NOTE_E5, NOTE_G5, NOTE_B5, NOTE_C6, NOTE_E6 }, // Tonal pattern for button 3
{ NOTE_A4, NOTE_C5, NOTE_E5, NOTE_F5, NOTE_A5, NOTE_C6, NOTE_D6, NOTE_F6 }, // Tonal pattern for button 4
{ NOTE_B4, NOTE_D5, NOTE_F5, NOTE_G5, NOTE_B5, NOTE_D6, NOTE_E6, NOTE_G6 }, // Tonal pattern for button 5
{ NOTE_C5, NOTE_E5, NOTE_G5, NOTE_A5, NOTE_C6, NOTE_E6, NOTE_F6, NOTE_A6 }, // Tonal pattern for button 6
{ NOTE_D5, NOTE_F5, NOTE_A5, NOTE_B5, NOTE_D6, NOTE_F6, NOTE_G6, NOTE_B6 }, // Tonal pattern for button 7
{ NOTE_E5, NOTE_G5, NOTE_B5, NOTE_C6, NOTE_E6, NOTE_G6, NOTE_A6, NOTE_C7 } // Tonal pattern for button 8
};
const int noteDurations[][8] = {
{ 400, 400, 400, 400, 400, 400, 400, 800 }, // Durations for button 1
{ 300, 300, 300, 300, 300, 300, 300, 600 }, // Durations for button 2
{ 350, 350, 350, 350, 350, 350, 350, 700 }, // Durations for button 3
{ 320, 320, 320, 320, 320, 320, 320, 640 }, // Durations for button 4
{ 280, 280, 280, 280, 280, 280, 280, 560 }, // Durations for button 5
{ 250, 250, 250, 250, 250, 250, 250, 500 }, // Durations for button 6
{ 200, 200, 200, 200, 200, 200, 200, 400 }, // Durations for button 7
{ 150, 150, 150, 150, 150, 150, 150, 300 } // Durations for button 8
};
const char* buttonNames[] = {
"DO", "RE", "MI", "FA", "SOL", "LA", "SI", "DO"
};
const int numTones = sizeof(buttonPins) / sizeof(buttonPins[0]);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.begin(16, 2);
lcd.backlight();
lcd.clear();
lcd.print("Halo Pak Julen");
for (uint8_t i = 0; i < numTones; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
}
void loop() {
for (uint8_t i = 0; i < numTones; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
playTonePattern(buttonTones[i], noteDurations[i], 8, buttonNames[i]);
delay(500); // Debounce delay
}
}
}
void playTonePattern(const int tones[], const int durations[], int length, const char* note) {
lcd.clear();
lcd.print(note);
for (int i = 0; i < length; i++) {
int duration = durations[i];
tone(SPEAKER_PIN, tones[i], duration);
delay(duration * 1.2); // Pause between tones
noTone(SPEAKER_PIN);
delay(50); // Short pause before next tone
}
lcd.clear();
lcd.print("Mini Piano");
}