// Define the pin where the buzzer is connected
int buzzerPin = 8;
void setup() {
// Initialize the buzzer pin as an output
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Melody for "Twinkle, Twinkle, Little Star"
int melody[] = {
262, 262, 392, 392, 440, 440, 392,
349, 349, 330, 330, 294, 294, 262
};
// Note durations for each note
int noteDurations[] = {
4, 4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 4, 2
};
// Iterate through the melody array
for (int i = 0; i < 14; i++) {
int noteDuration = 1000 / noteDurations[i];
tone(buzzerPin, melody[i], noteDuration);
delay(noteDuration * 1.3); // Add a small delay between notes
noTone(buzzerPin); // Stop the tone playing
delay(50); // Add a pause between the tones
}
delay(2000); // Add a longer pause between repetitions of the melody
}