#include "pitches.h"

#define PIN 2

const int NUM_NOTES = 172; // Número total de notas

// Estrutura para manter uma nota e sua duração juntas
struct Note {
  int pitch;
  int duration;
};

uint32_t melody[NUM_NOTES] = {
  NOTE_E5, NOTE_E5, 0, NOTE_E5, 0, NOTE_C5, NOTE_E5, 0, NOTE_G5, 0, NOTE_G4, 0,

  NOTE_C5, 0, NOTE_G4, 0, NOTE_E4, 0, NOTE_A4, 0, NOTE_B4, 0, NOTE_AS4, NOTE_A4,
  NOTE_G4, 0, NOTE_E5, 0, NOTE_G5, NOTE_A5, 0, NOTE_F5, NOTE_G5, 0, NOTE_E5, 0, NOTE_C5, NOTE_D5, NOTE_B4, 0,

  NOTE_C5, 0, NOTE_G4, 0, NOTE_E4, 0, NOTE_A4, 0, NOTE_B4, 0, NOTE_AS4, NOTE_A4,
  NOTE_G4, 0, NOTE_E5, 0, NOTE_G5, NOTE_A5, 0, NOTE_F5, NOTE_G5, 0, NOTE_E5, 0, NOTE_C5, NOTE_D5, NOTE_B4, 0,

  0, NOTE_G5, NOTE_FS5, NOTE_F5, NOTE_DS5, 0, NOTE_E5, 0, NOTE_GS4, NOTE_A4, NOTE_C5, 0, NOTE_A4, NOTE_C5, NOTE_D5,
  0, NOTE_G5, NOTE_FS5, NOTE_F5, NOTE_DS5, 0, NOTE_E5, 0, NOTE_C6, 0, NOTE_C6, NOTE_C6, 0, 0, 0,

  0, NOTE_G5, NOTE_FS5, NOTE_F5, NOTE_DS5, 0, NOTE_E5, 0, NOTE_GS4, NOTE_A4, NOTE_C5, 0, NOTE_A4, NOTE_C5, NOTE_D5,
  0, NOTE_DS5, 0, NOTE_D5, 0, NOTE_C5, 0, 0, 0,

  NOTE_C5, NOTE_C5, 0, NOTE_C5, 0, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_C5, 0, NOTE_A4, NOTE_G4, 0,
  NOTE_C5, NOTE_C5, 0, NOTE_C5, 0, NOTE_C5, NOTE_D5, NOTE_E5, 0, 0, 0, 0,

  NOTE_C5, NOTE_C5, 0, NOTE_C5, 0, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_C5, 0, NOTE_A4, NOTE_G4, 0,
  NOTE_E5, NOTE_E5, 0, NOTE_E5, 0, NOTE_C5, NOTE_E5, NOTE_G5, 0, 0, 0, 0
};

int noteDurations[] = {
  8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4,               // 12

  4, 8, 8, 4, 4, 8, 8, 8, 8, 8, 8, 4,               // 12
  8, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 8, // 16

  4, 8, 8, 4, 4, 8, 8, 8, 8, 8, 8, 4,               // 12
  8, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 8, // 16

  4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,      // 15
  4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,      // 15

  4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,      // 15
  4, 4, 8, 8, 4, 4, 4, 4, 4,                        // 9

  8, 8, 8, 8, 8, 8, 4, 8, 8, 8, 8, 4, 4,            // 13
  8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4,               // 12

  8, 8, 8, 8, 8, 8, 4, 8, 8, 8, 8, 4, 4,            // 13
  8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4                // 12
};

void playNote(int note, int duration) {
  int noteDuration = 1000 / duration;
  tone(PIN, note, noteDuration);

  int pauseBetweenNotes = noteDuration * 1.30;
  delay(pauseBetweenNotes);
  noTone(PIN);
}

void setup() {
  for (int thisNote = 0; thisNote < NUM_NOTES; thisNote++) {
    playNote(melody[thisNote], noteDurations[thisNote]);
  }
}

void loop() {
  // Nada aqui, a melodia toca uma vez e para
}