CCCPaste

"LED Toggle" 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 Toggle"
	- Source Code NOT compiled for: Arduino Nano ESP32
	- Source Code created on: 2024-06-13 13:10:56

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* when b1_pushbutton is pressed log to internal */
	/* memory and write how many times and the state to */
	/* mysql using php */
/****** SYSTEM REQUIREMENT 2 *****/
	/* when b1_pushbutton or b2_Pushbutton has updated */
	/* mysql database turn on led5_LED for 2 seconds */
/****** END SYSTEM REQUIREMENTS *****/

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

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void onButton1Pressed(void);
void onButton2Pressed(void);
void logButtonPressToMemoryAndDatabase(int buttonID);
void turnOnLed5For2Seconds(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t b1_PushButton_PIN_D2 = 2;
const uint8_t b2_PushButton_PIN_D3 = 3;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t led1b1_LED_PIN_D4 = 4;
const uint8_t led2b2_LED_PIN_D5 = 5;
const uint8_t led5_LED_PIN_D6 = 6;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool led1b1_LED_PIN_D4_rawData = 0;
bool led2b2_LED_PIN_D5_rawData = 0;
bool led5_LED_PIN_D6_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float led1b1_LED_PIN_D4_phyData = 0.0;
float led2b2_LED_PIN_D5_phyData = 0.0;
float led5_LED_PIN_D6_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton button1(b1_PushButton_PIN_D2);  // Initialize EasyButton for button 1
EasyButton button2(b2_PushButton_PIN_D3);  // Initialize EasyButton for button 2

void setup(void)
{
  // put your setup code here, to run once:

  Serial.begin(115200);  // Initialize serial communication
  Serial.println();
  Serial.println(">>> EasyButton multiple buttons example <<<");

  pinMode(b1_PushButton_PIN_D2, INPUT_PULLUP);
  pinMode(b2_PushButton_PIN_D3, INPUT_PULLUP);

  pinMode(led1b1_LED_PIN_D4, OUTPUT);
  pinMode(led2b2_LED_PIN_D5, OUTPUT);
  pinMode(led5_LED_PIN_D6, OUTPUT);

  button1.begin();  // Initialize button 1
  button2.begin();  // Initialize button 2

  button1.onPressed(onButton1Pressed);  // Attach callback for button 1
  button2.onPressed(onButton2Pressed);  // Attach callback for button 2
}

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

  button1.read();  // Read the state of button 1
  button2.read();  // Read the state of button 2

  updateOutputs(); // Refresh output data
}

void updateOutputs()
{
  digitalWrite(led1b1_LED_PIN_D4, led1b1_LED_PIN_D4_rawData);
  digitalWrite(led2b2_LED_PIN_D5, led2b2_LED_PIN_D5_rawData);
  digitalWrite(led5_LED_PIN_D6, led5_LED_PIN_D6_rawData);
}

void onButton1Pressed() {
  Serial.println("Button1 pressed");
  led1b1_LED_PIN_D4_rawData = !led1b1_LED_PIN_D4_rawData;  // Toggle LED state
  logButtonPressToMemoryAndDatabase(1);  // Log button press to memory and database
  turnOnLed5For2Seconds();  // Turn on LED5 for 2 seconds
}

void onButton2Pressed() {
  Serial.println("Button2 pressed");
  led2b2_LED_PIN_D5_rawData = !led2b2_LED_PIN_D5_rawData;  // Toggle LED state
  logButtonPressToMemoryAndDatabase(2);  // Log button press to memory and database
  turnOnLed5For2Seconds();  // Turn on LED5 for 2 seconds
}

void logButtonPressToMemoryAndDatabase(int buttonID) {
  // Log to internal memory (this is a placeholder, replace with actual logging code)
  static int button1PressCount = 0;
  static int button2PressCount = 0;
  
  if (buttonID == 1) {
    button1PressCount++;
    Serial.print("Button 1 Press Count: ");
    Serial.println(button1PressCount);
  } else if (buttonID == 2) {
    button2PressCount++;
    Serial.print("Button 2 Press Count: ");
    Serial.println(button2PressCount);
  }

  // Write to MySQL database using PHP (this is a placeholder, replace with actual database code)
  // Example: http://yourserver.com/log_button_press.php?button_id=1&count=button1PressCount
}

void turnOnLed5For2Seconds() {
  led5_LED_PIN_D6_rawData = 1;  // Turn on LED5
  updateOutputs();  // Update outputs to reflect the change
  delay(2000);  // Wait for 2 seconds
  led5_LED_PIN_D6_rawData = 0;  // Turn off LED5
  updateOutputs();  // Update outputs to reflect the change
}

/* END CODE */