CCCPaste

LED Button 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 Button
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-08-06 12:39:39

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* turn on LED ehen button is pressed */
/****** END SYSTEM REQUIREMENTS *****/

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

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t B1_PushButton_PIN_D2 = 2; // Button pin
const uint8_t LED_PIN = 13; // LED pin (usually the built-in LED on Arduino Uno)

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Initialize the EasyButton object with the defined button pin
EasyButton button(B1_PushButton_PIN_D2);

/****** FUNCTION DEFINITIONS *****/
void onPressed() {
    // Function to be called when the button is pressed
    Serial.println("Button pressed");
    digitalWrite(LED_PIN, HIGH); // Turn on the LED when the button is pressed
}

void onReleased() {
    // Function to be called when the button is released
    Serial.println("Button released");
    digitalWrite(LED_PIN, LOW); // Turn off the LED when the button is released
}

void setup(void) {
    // Start serial communication for debugging
    Serial.begin(115200);
    Serial.println();
    Serial.println(">>> EasyButton pressed example <<<");

    // Initialize the button
    button.begin();
    // Attach the onPressed function to the button press event
    button.onPressed(onPressed);
    // Attach the onReleased function to the button release event
    button.onReleased(onReleased);

    // Set the button pin as input with pull-up resistor
    pinMode(B1_PushButton_PIN_D2, INPUT_PULLUP);
    // Set the LED pin as output
    pinMode(LED_PIN, OUTPUT);
    digitalWrite(LED_PIN, LOW); // Ensure the LED is off at startup
}

void loop(void) {
    // Continuously read the button state
    button.read();
}

/* END CODE */