void setup() {
pinMode(2, OUTPUT); // Piezo buzzer connected to pin 2
for (int pin = 6; pin <= 13; pin++) {
pinMode(pin, OUTPUT); // LEDs connected to pins 6 to 13
}
}
void loop() {
// Define the notes for Littleroot Town theme from Pokémon
int notes[] = {
392, 349, 330, 349, 392, 392, 392, 392, 392,
392, 440, 392, 349, 330, 349, 392, 349,
392, 349, 330, 349, 392, 392, 392, 392, 392,
392, 440, 392, 349, 330, 349, 392, 349
};
// Define the corresponding LEDs for visual feedback
int leds[] = {13, 12, 11, 10, 9, 8, 7, 6,
6, 7, 8, 6, 9, 10, 9, 6,
7, 8, 6, 9, 10, 9, 6, 7, 6,
7, 8, 11, 11, 8, 7, 6, 9};
int duration = 300; // Note duration in milliseconds
int pause = 50; // Pause between notes
// Play the melody and light up corresponding LEDs
for (int i = 0; i < sizeof(notes) / sizeof(notes[0]); i++) {
tone(2, notes[i]);
digitalWrite(leds[i], HIGH); // Turn on corresponding LED
delay(duration);
noTone(2);
digitalWrite(leds[i], LOW); // Turn off LED
delay(pause);
}
delay(1000); // Delay before repeating the song
}