#define BUZZER_PIN 8
#define LED_PIN 13

// Define the note frequencies
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_D4  294
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_G4  392
#define NOTE_A4  440
#define NOTE_B4  494
#define NOTE_C5  523

// Melody for "My Heart Will Go On" (extended intro to chorus)
int melody[] = {
  NOTE_E4, NOTE_F4, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, // Intro part 1
  NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4, // Intro part 2
  NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_B4, NOTE_A4, // Chorus part 1
  NOTE_G4, NOTE_A4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_E4           // Chorus part 2
};

int noteDurations[] = {
  4, 4, 4, 4, 4, 4, 4, 4,  // Intro part 1 (quarter notes)
  4, 4, 4, 4, 4, 4, 4, 4,  // Intro part 2 (quarter notes)
  4, 4, 4, 4, 4, 4, 4, 4,  // Chorus part 1 (quarter notes)
  4, 4, 4, 4, 4, 4, 4      // Chorus part 2 (quarter notes)
};

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  // Play each note in the melody array
  for (int i = 0; i < sizeof(melody) / sizeof(int); i++) {
    int noteDuration = 1000 / noteDurations[i];
    tone(BUZZER_PIN, melody[i], noteDuration);

    // Flash the LED while the buzzer plays
    digitalWrite(LED_PIN, HIGH);  // Turn the LED on
    delay(noteDuration / 2);      // Half the note duration
    digitalWrite(LED_PIN, LOW);   // Turn the LED off

    int pauseBetweenNotes = noteDuration * 1.3;
    delay(pauseBetweenNotes);     // Pause before playing the next note
  }
  noTone(BUZZER_PIN);  // Stop the tone after playing the melody

  delay(1000);  // Wait 1 second before repeating the loop
}