CCCPaste

"EasyButton RelayControl" rev_02

/********* 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: "EasyButton RelayControl"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-06-16 20:05:17

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Quando il Pulsante1 (PIN D2) è premuto, attiva */
	/* Relay1 (PIN D4) per 10 secondi, poi attiva Relay2 */
	/* (PIN D5) per 20 secondi. Alla fine, entrambi i */
	/* relay devono essere disattivati. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* Attivare Relay3 (PIN D6) quando il Pulsante2 (PIN */
	/* D3) è premuto e disattivarlo quando non è premuto. */
	/* Implementare la logica utilizzando le librerie */
	/* EasyButton e Relay per garantire una risposta */
	/* rapida e affidabile. */
/****** END SYSTEM REQUIREMENTS *****/

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

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void onButton1Pressed(void);
void onButton2Pressed(void);
void checkButton2Released(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t Pulsante1_PushButton_PIN_D2 = 2;
const uint8_t Pulsante2_PushButton_PIN_D3 = 3;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t Relay1_RelayModule_Signal_PIN_D4 = 4;
const uint8_t Relay2_RelayModule_Signal_PIN_D5 = 5;
const uint8_t Relay3_RelayModule_Signal_PIN_D6 = 6;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool Relay1_RelayModule_Signal_PIN_D4_rawData = 0;
bool Relay2_RelayModule_Signal_PIN_D5_rawData = 0;
bool Relay3_RelayModule_Signal_PIN_D6_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float Relay1_RelayModule_Signal_PIN_D4_phyData = 0.0;
float Relay2_RelayModule_Signal_PIN_D5_phyData = 0.0;
float Relay3_RelayModule_Signal_PIN_D6_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton button1(Pulsante1_PushButton_PIN_D2); // Initialize button1 with pin D2
EasyButton button2(Pulsante2_PushButton_PIN_D3); // Initialize button2 with pin D3

Relay relay1(Relay1_RelayModule_Signal_PIN_D4, true); // Initialize relay1 on pin D4, Normally Open
Relay relay2(Relay2_RelayModule_Signal_PIN_D5, true); // Initialize relay2 on pin D5, Normally Open
Relay relay3(Relay3_RelayModule_Signal_PIN_D6, true); // Initialize relay3 on pin D6, Normally Open

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

  pinMode(Pulsante1_PushButton_PIN_D2, INPUT_PULLUP);
  pinMode(Pulsante2_PushButton_PIN_D3, INPUT_PULLUP);

  pinMode(Relay1_RelayModule_Signal_PIN_D4, OUTPUT);
  pinMode(Relay2_RelayModule_Signal_PIN_D5, OUTPUT);
  pinMode(Relay3_RelayModule_Signal_PIN_D6, OUTPUT);

  Serial.begin(115200);

  Serial.println();
  Serial.println(">>> EasyButton multiple buttons example <<<");

  button1.begin(); // Initialize button1
  button2.begin(); // Initialize button2
  button1.onPressed(onButton1Pressed); // Attach callback for button1
  button2.onPressed(onButton2Pressed); // Attach callback for button2

  relay1.begin(); // Initialize relay1
  relay2.begin(); // Initialize relay2
  relay3.begin(); // Initialize relay3
}

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

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

  checkButton2Released(); // Check if button2 is released

  updateOutputs(); // Refresh output data
}

void updateOutputs() {
  digitalWrite(Relay1_RelayModule_Signal_PIN_D4, Relay1_RelayModule_Signal_PIN_D4_rawData);
  digitalWrite(Relay2_RelayModule_Signal_PIN_D5, Relay2_RelayModule_Signal_PIN_D5_rawData);
  digitalWrite(Relay3_RelayModule_Signal_PIN_D6, Relay3_RelayModule_Signal_PIN_D6_rawData);
}

void onButton1Pressed() {
  Serial.println("Button1 pressed");
  // SYSTEM REQUIREMENT 1
  relay1.turnOn(); // Activate Relay1
  delay(10000); // Wait for 10 seconds
  relay1.turnOff(); // Deactivate Relay1
  relay2.turnOn(); // Activate Relay2
  delay(20000); // Wait for 20 seconds
  relay2.turnOff(); // Deactivate Relay2
}

void onButton2Pressed() {
  Serial.println("Button2 pressed");
  // SYSTEM REQUIREMENT 2
  relay3.turnOn(); // Activate Relay3
}

void checkButton2Released() {
  if (button2.isReleased()) {
    Serial.println("Button2 released");
    // SYSTEM REQUIREMENT 2
    relay3.turnOff(); // Deactivate Relay3
  }
}

/* END CODE */