CCCPaste

Button 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: Button Control
	- Source Code compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-04-15 10:18:19

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Use esp now to turn on anoutput as long as the */
	/* button is pressed add power saving */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include     // Library for handling buttons
#include       // Library for ESP Now communication
#include          // Library for WiFi functionality

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void onButtonPressed(); // Callback function for button press event

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t Up_PushButton_PIN_D4 = 4;
const uint8_t Down_PushButton_PIN_D13 = 13;
const uint8_t Lift_PushButton_PIN_D14 = 14;
const uint8_t Drop_PushButton_PIN_D16 = 16;

/****** DEFINITION OF LIBRARY CLASS INSTANCES *****/
EasyButton upButton(Up_PushButton_PIN_D4);
EasyButton downButton(Down_PushButton_PIN_D13);
EasyButton liftButton(Lift_PushButton_PIN_D14);
EasyButton dropButton(Drop_PushButton_PIN_D16);

/****** DEFINITION OF ESP NOW VARIABLES *****/
const uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // Broadcast address for ESP Now
uint8_t outputPinState = LOW; // Initial state of the output pin
const uint8_t outputPin = 2; // Output pin to control

void setup(void)
{
    // put your setup code here, to run once:
    Serial.begin(115200);

    // Initialize ESP Now
    if (esp_now_init() != ESP_OK) {
        Serial.println("ESP Now initialization failed");
        while (true);
    }

    // Register callback function for ESP Now data reception
    esp_now_register_recv_cb([](const uint8_t* mac, const uint8_t* data, int len) {
        // Handle received data if needed
    });

    // Set ESP Now peer address to broadcast address
    esp_now_peer_info_t peerInfo;
    memcpy(peerInfo.peer_addr, broadcastAddress, 6);
    peerInfo.channel = 0;
    peerInfo.encrypt = false;

    // Add ESP Now peer
    if (esp_now_add_peer(&peerInfo) != ESP_OK) {
        Serial.println("Failed to add ESP Now peer");
        while (true);
    }

    // Set output pin as output
    pinMode(outputPin, OUTPUT);

    // Set callback function for button press event
    upButton.onPressed(onButtonPressed);
    downButton.onPressed(onButtonPressed);
    liftButton.onPressed(onButtonPressed);
    dropButton.onPressed(onButtonPressed);
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    upButton.read();
    downButton.read();
    liftButton.read();
    dropButton.read();

    // Send ESP Now message to control the output pin
    esp_now_send(broadcastAddress, &outputPinState, sizeof(outputPinState));

    delay(100);
}

void onButtonPressed()
{
    // Toggle the output pin state
    outputPinState = !outputPinState;
    digitalWrite(outputPin, outputPinState);

    // Print button pressed message
    Serial.println("Button pressed");
}