/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Buzzer Melody"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2023-12-19 21:51:28

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* play melody of jingle bells. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <Arduino.h>
#include <ezBuzzer.h>

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t START_BUTTON_PIN = 7;
const uint8_t STOP_BUTTON_PIN = 8;
const uint8_t buzzer_PassiveBuzzer_Signal_PIN = 3;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
ezBuzzer buzzer(buzzer_PassiveBuzzer_Signal_PIN);

// Melody for Jingle Bells
int melody[] = {
  NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_G4, NOTE_C4, NOTE_D4, NOTE_E4,
  NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4,
  NOTE_D4, NOTE_G4
};

int noteDurations[] = {
  4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 1,
  4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2,
  4, 4
};

void setup(void)
{
    // put your setup code here, to run once:
    pinMode(START_BUTTON_PIN, INPUT_PULLUP);
    pinMode(STOP_BUTTON_PIN, INPUT_PULLUP);
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    buzzer.loop();

    int startButtonState = digitalRead(START_BUTTON_PIN);
    int stopButtonState = digitalRead(STOP_BUTTON_PIN);

    if (startButtonState == LOW)
    {
        if (buzzer.getState() == BUZZER_IDLE)
        {
            int length = sizeof(noteDurations) / sizeof(int);
            buzzer.playMelody(melody, noteDurations, length);
        }
    }

    if (stopButtonState == LOW)
    {
        if (buzzer.getState() != BUZZER_IDLE)
        {
            buzzer.stop();
        }
    }
}
