Lcd player

#include<LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int buzzerPin = 9;
int buttonPin = 8;
int melodyIndex = 0;

int melody1[] = {261, 294, 329, 349, 391, 415, 440, 455, 466,
523, 554, 587, 662, 659, 698, 740, 784, 830, 880};
int melody2[] = {523, 494, 440, 392, 349, 330, 294, 262};
int melody3[] = {392, 330, 294, 262, 523, 494, 440, 392};

int melodyLength = 8;

void setup() {
lcd.begin(16, 2);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);// Nupu sisemine tõmbetakisti
}

void loop() {
// Kontroll, kas nupp on alla vajutatud (LOW)
if (digitalRead(buttonPin) == LOW) {
// Nuppu vajutatatud, liikuge järgmise meloodia juurde
cycleMelody();
delay(500);
}
Serial.println(“Button State: ” + String(digitalRead(buttonPin)));
Serial.println(“Melody Index: ” + String(melodyIndex));

lcd.clear();
lcd.setCursor(2, 0);
lcd.write(5);
lcd.write(6);

lcd.setCursor(13, 1);
lcd.write(3);

delay(400);

lcd.setCursor(4, 0);
playMelody(melody1);

lcd.setCursor(13, 1);
lcd.write(4);
playMelody(melody2);

lcd.clear();
playMelody(melody3);
}

void playMelody(int melody[]) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Melody: “);
switch (melodyIndex) {
case 0:
lcd.print(“Melody 1”);
break;
case 1:
lcd.print(“Melody 2”);
break;
case 2:
lcd.print(“Melody 3”);
break;
}

lcd.setCursor(0, 1);
lcd.print(“Playing…”);


// Iterate läbi meloodia nootide
for (int note = 0; note < melodyLength; note++) {
int duration = 500;
tone(buzzerPin, melody[note], duration);

// Oodame noodi kestust
delay(duration);


noTone(buzzerPin);


// Kontroll, kas nupp on alla vajutatud (LOW)
if (digitalRead(buttonPin) == LOW) {
  
// Nupp on vajutatud, väljumist meloodiatsüklist
  break;
}

}
}

void cycleMelody() {

// Suurendamine meloodiaindeksit ja lülituge tagasi 0-ni, kui see ületab meloodiate arvu

// используем модуло что бы мелодии были в диапазоне от 0 до 2, melody1,melody2,melody3
melodyIndex = (melodyIndex + 1) % 3;
}

1.