//this code is written with the help of AI
#include <LiquidCrystal.h>
// Initialize the LCD (pins: RS, Enable, D4, D5, D6, D7)
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// Pin configuration for buzzer
const int buzzerPin = 6;
// Define note frequencies
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
// Melody for "Twinkle, Twinkle, Little Star"
int melody[] = {
NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4, // Line 1
NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4, // Line 2
NOTE_G4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, // Line 3
NOTE_G4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, // Line 4
NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4, // Line 5
NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4 // Line 6
};
// Note durations: 4 = quarter note, 8 = eighth note, etc.
int noteDurations[] = {
4, 4, 4, 4, 4, 4, 2, // Line 1
4, 4, 4, 4, 4, 4, 2, // Line 2
4, 4, 4, 4, 4, 4, 2, // Line 3
4, 4, 4, 4, 4, 4, 2, // Line 4
4, 4, 4, 4, 4, 4, 2, // Line 5
4, 4, 4, 4, 4, 4, 2 // Line 6
};
// Lyrics for each line
const char* lyrics[] = {
"Twinkle, twinkle,",
"little star,",
"How I wonder",
"what you are!",
"Up above the",
"world so high,",
"Like a diamond",
"in the sky.",
"Twinkle, twinkle,",
"little star,",
"How I wonder",
"what you are!"
};
void setup() {
// Initialize the LCD
lcd.begin(16, 2);
// Display greeting
lcd.setCursor(0, 0);
lcd.print(" Twinkle, ");
lcd.setCursor(0, 1);
lcd.print("Little Star...");
delay(2000);
lcd.clear();
// Play the melody and display lyrics
int melodyLength = sizeof(melody) / sizeof(melody[0]);
int lyricIndex = 0;
for (int i = 0; i < melodyLength; i++) {
int noteDuration = 1000 / noteDurations[i];
// Update lyrics line when necessary
if (i % 7 == 0 && lyricIndex < 12) { // Change lyrics every 7 notes
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(lyrics[lyricIndex]);
if (lyricIndex + 1 < 12) {
lcd.setCursor(0, 1);
lcd.print(lyrics[lyricIndex + 1]);
}
lyricIndex += 2;
}
// Play the note
tone(buzzerPin, melody[i], noteDuration);
delay(noteDuration * 1.3); // Add a slight pause
}
// End song message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Song Complete!");
}
void loop() {
// Do nothing; the song only plays once
}