CCCPaste

LED Buzzer Control rev_01

/********* 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: LED Buzzer Control
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-10-05 11:59:36

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* The application shall read input from a push */
	/* button and control two LEDs and a passive buzzer, */
	/* leveraging EasyButton for button handling and */
	/* ezBuzzer for sound output. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 	// https://github.com/evert-arias/EasyButton
#include 	// https://github.com/ArduinoGetStarted/buzzer

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t StartStop_PushButton_PIN_D4 = 4; // Push button pin

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t LED1_LED_PIN_D13 = 13; // LED1 pin
const uint8_t LED2_LED_PIN_D14 = 14; // LED2 pin
const uint8_t Bzzz_PassiveBuzzer_Signal_PIN_D16 = 16; // Buzzer pin

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
bool LED1_LED_PIN_D13_rawData = 0; // Raw data for LED1
bool LED2_LED_PIN_D14_rawData = 0; // Raw data for LED2
bool Bzzz_PassiveBuzzer_Signal_PIN_D16_rawData = 0; // Raw data for Buzzer

/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
// EasyButton instance for Start/Stop button
EasyButton startStopButton(StartStop_PushButton_PIN_D4); 
// ezBuzzer instance for the buzzer, initialized with the buzzer pin
ezBuzzer buzzer(Bzzz_PassiveBuzzer_Signal_PIN_D16); 

void setup(void) {
    // Initialize Serial for debugging purposes
    Serial.begin(115200);
    
    // Set pin modes
    pinMode(StartStop_PushButton_PIN_D4, INPUT_PULLUP); // Set button pin as input
    pinMode(LED1_LED_PIN_D13, OUTPUT); // Set LED1 pin as output
    pinMode(LED2_LED_PIN_D14, OUTPUT); // Set LED2 pin as output
    pinMode(Bzzz_PassiveBuzzer_Signal_PIN_D16, OUTPUT); // Set Buzzer pin as output

    // Initialize the button
    startStopButton.begin();
    // Add callback function for button press
    startStopButton.onPressed([]() {
        Serial.println("Start/Stop button pressed");
        // Toggle the state of the LEDs and buzzer
        LED1_LED_PIN_D13_rawData = !LED1_LED_PIN_D13_rawData; // Toggle LED1
        LED2_LED_PIN_D14_rawData = !LED2_LED_PIN_D14_rawData; // Toggle LED2
        Bzzz_PassiveBuzzer_Signal_PIN_D16_rawData = !Bzzz_PassiveBuzzer_Signal_PIN_D16_rawData; // Toggle Buzzer
        if (Bzzz_PassiveBuzzer_Signal_PIN_D16_rawData) {
            buzzer.beep(100); // Beep for 100ms when button is pressed
        }
    });
}

void loop(void) {
    // Continuously read the status of the button
    startStopButton.read();
    
    // Refresh output data
    updateOutputs();
}

void updateOutputs() {
    // Update the state of the LEDs
    digitalWrite(LED1_LED_PIN_D13, LED1_LED_PIN_D13_rawData);
    digitalWrite(LED2_LED_PIN_D14, LED2_LED_PIN_D14_rawData);
    
    // Update buzzer state
    if (Bzzz_PassiveBuzzer_Signal_PIN_D16_rawData) {
        buzzer.loop(); // Call buzzer loop to handle non-blocking behavior
    } else {
        buzzer.stop(); // Stop the buzzer if not active
    }
}

/* END CODE */