/********* 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 22:06:30

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* play melody of jingle bells one time. */
/****** 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 int BUZZER_PIN = 2;

// Create an instance of ezBuzzer class with the buzzer pin
ezBuzzer buzzer(BUZZER_PIN);

/*********** SYSTEM REQUIREMENTS ***********/
/*********** SYSTEM REQUIREMENT 1 ***********/
// Play melody of jingle bells one time
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_E4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_G4
};
int noteDurations[] = {
  8, 8, 4, 8, 8, 32,
  8, 8, 8, 8, 4,
  8, 8, 8, 8, 8,
  4, 4, 4, 4, 2,
  4, 4, 2, 4, 4, 2
};

void setup(void) {
  // Initialize the Serial communication
  Serial.begin(9600);

  // Set buzzer pin as OUTPUT
  pinMode(BUZZER_PIN, OUTPUT);
}

void loop(void) {
  // Update the buzzer state
  buzzer.loop();

  // Play jingle bells melody once
  static bool jingleBellsPlayed = false;

  if (!jingleBellsPlayed && buzzer.getState() == BUZZER_IDLE) {
    int length = sizeof(noteDurations) / sizeof(int);

    // Play the melody
    buzzer.playMelody(melody, noteDurations, length);
    jingleBellsPlayed = true;
  }
}
