/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-piezo-buzzer

 MELODY CODE CHANGED FROM EXAMPLE
 melody: home alone
 melody code by tmekimyan
 https://github.com/hibit-dev/buzzer/blob/master/src/other/happy_birthday/happy_birthday.ino

Copyright (c) 2022 HiBit <https://www.hibit.dev>
 */

#include "pitches.h"

const int BUTTON_PIN = 2; // Arduino pin connected to button's pin
const int BUZZER_PIN = 4; // Arduino pin connected to Buzzer's pin

// notes in the melody:
int melody[] = {
  NOTE_E4, NOTE_C5, NOTE_B4, NOTE_B4,
  NOTE_C5, NOTE_B4, NOTE_F4, NOTE_G4,
  NOTE_G4, NOTE_E4, NOTE_G4, NOTE_E4,
  NOTE_C5, NOTE_G4,
  NOTE_D4, NOTE_A4, NOTE_G4, NOTE_C4, NOTE_F4,
  NOTE_E4, NOTE_D4,
  NOTE_G4, NOTE_E4, NOTE_G4, NOTE_E4,
  NOTE_C5, NOTE_G4,
  NOTE_A4, NOTE_C5, NOTE_F4, NOTE_E4, NOTE_G4, NOTE_C4,
  NOTE_E4, NOTE_D4,
 NOTE_E4,NOTE_C4, NOTE_F4, NOTE_D4,
 NOTE_FS4, NOTE_D4, NOTE_A4, NOTE_G4,
 NOTE_GS5, NOTE_E4, NOTE_B4, NOTE_A4,
  NOTE_F4, NOTE_C5, NOTE_A4, NOTE_B4,
  NOTE_G4, NOTE_E4, NOTE_G4, NOTE_E4,
  NOTE_C5, NOTE_G4,
  NOTE_D4, NOTE_A4, NOTE_G4, NOTE_C4, NOTE_F4,
  NOTE_E4, NOTE_D4,
  NOTE_G4, NOTE_E4, NOTE_G4, NOTE_E4,
  NOTE_C5, NOTE_G4,
  NOTE_A4, NOTE_B4, NOTE_C5, NOTE_G4, NOTE_E4,
  NOTE_A4, NOTE_B4, NOTE_C5, NOTE_G4, NOTE_E4,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_G4, NOTE_E4,
  NOTE_C4, NOTE_C5, NOTE_B4,
  NOTE_D5, NOTE_E4, NOTE_G4, NOTE_E4,
  NOTE_C5, NOTE_G4,
  NOTE_C5
};

// note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo:
int noteDurations[] = {
 4, 8, 8, 2,
  8, 8, 4, 4,
  4, 4, 4, 4,
  2, 2,
  4, 4, 4, 8, 8,
  2, 2,
  4, 4, 4, 4,
  2, 2,
  8, 8, 4, 8, 8, 4,
  2, 2,
  4, 8, 8, 4, 4,
  4, 8, 8, 4, 4,
  4, 8, 8, 4, 4,
  4, 8, 8, 2, 
  4, 4, 4, 4,
  2, 2,
  4, 4, 4, 8, 8,
  2, 2,
  4, 4, 4, 4,
  2, 2,
  4, 16, 16, 8, 4, 4,
  4, 16, 16, 8, 4, 4,
  4, 16, 16, 8, 4, 4,
  4, 2, 4,
  4, 4, 4, 4,
  2, 2,
  1
  
 
};

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
}

void loop() {
  int buttonState = digitalRead(BUTTON_PIN); // read new state

  if (buttonState == LOW) { // button is pressed
    Serial.println("The button is being pressed");
    buzzer();
  }
}

void buzzer() {
  // iterate over the notes of the melody:
  int size = sizeof(noteDurations) / sizeof(int);

  for (int thisNote = 0; thisNote < size; thisNote++) {
    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(BUZZER_PIN, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(BUZZER_PIN);
  }
}
