Advertisement
pleasedontcode

"EasyButton RelayControl" rev_02

Jun 16th, 2024
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "EasyButton RelayControl"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-16 20:05:17
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Quando il Pulsante1 (PIN D2) è premuto, attiva */
  21.     /* Relay1 (PIN D4) per 10 secondi, poi attiva Relay2 */
  22.     /* (PIN D5) per 20 secondi. Alla fine, entrambi i */
  23.     /* relay devono essere disattivati. */
  24. /****** SYSTEM REQUIREMENT 2 *****/
  25.     /* Attivare Relay3 (PIN D6) quando il Pulsante2 (PIN */
  26.     /* D3) è premuto e disattivarlo quando non è premuto. */
  27.     /* Implementare la logica utilizzando le librerie */
  28.     /* EasyButton e Relay per garantire una risposta */
  29.     /* rapida e affidabile. */
  30. /****** END SYSTEM REQUIREMENTS *****/
  31.  
  32. /****** DEFINITION OF LIBRARIES *****/
  33. #include <EasyButton.h>  //https://github.com/evert-arias/EasyButton
  34. #include <Relay.h>       //https://github.com/rafaelnsantos/Relay
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void updateOutputs(void);
  40. void onButton1Pressed(void);
  41. void onButton2Pressed(void);
  42. void checkButton2Released(void);
  43.  
  44. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  45. const uint8_t Pulsante1_PushButton_PIN_D2 = 2;
  46. const uint8_t Pulsante2_PushButton_PIN_D3 = 3;
  47.  
  48. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  49. const uint8_t Relay1_RelayModule_Signal_PIN_D4 = 4;
  50. const uint8_t Relay2_RelayModule_Signal_PIN_D5 = 5;
  51. const uint8_t Relay3_RelayModule_Signal_PIN_D6 = 6;
  52.  
  53. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  54. /***** used to store raw data *****/
  55. bool Relay1_RelayModule_Signal_PIN_D4_rawData = 0;
  56. bool Relay2_RelayModule_Signal_PIN_D5_rawData = 0;
  57. bool Relay3_RelayModule_Signal_PIN_D6_rawData = 0;
  58.  
  59. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  60. /***** used to store data after characteristic curve transformation *****/
  61. float Relay1_RelayModule_Signal_PIN_D4_phyData = 0.0;
  62. float Relay2_RelayModule_Signal_PIN_D5_phyData = 0.0;
  63. float Relay3_RelayModule_Signal_PIN_D6_phyData = 0.0;
  64.  
  65. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  66. EasyButton button1(Pulsante1_PushButton_PIN_D2); // Initialize button1 with pin D2
  67. EasyButton button2(Pulsante2_PushButton_PIN_D3); // Initialize button2 with pin D3
  68.  
  69. Relay relay1(Relay1_RelayModule_Signal_PIN_D4, true); // Initialize relay1 on pin D4, Normally Open
  70. Relay relay2(Relay2_RelayModule_Signal_PIN_D5, true); // Initialize relay2 on pin D5, Normally Open
  71. Relay relay3(Relay3_RelayModule_Signal_PIN_D6, true); // Initialize relay3 on pin D6, Normally Open
  72.  
  73. void setup(void) {
  74.   // put your setup code here, to run once:
  75.  
  76.   pinMode(Pulsante1_PushButton_PIN_D2, INPUT_PULLUP);
  77.   pinMode(Pulsante2_PushButton_PIN_D3, INPUT_PULLUP);
  78.  
  79.   pinMode(Relay1_RelayModule_Signal_PIN_D4, OUTPUT);
  80.   pinMode(Relay2_RelayModule_Signal_PIN_D5, OUTPUT);
  81.   pinMode(Relay3_RelayModule_Signal_PIN_D6, OUTPUT);
  82.  
  83.   Serial.begin(115200);
  84.  
  85.   Serial.println();
  86.   Serial.println(">>> EasyButton multiple buttons example <<<");
  87.  
  88.   button1.begin(); // Initialize button1
  89.   button2.begin(); // Initialize button2
  90.   button1.onPressed(onButton1Pressed); // Attach callback for button1
  91.   button2.onPressed(onButton2Pressed); // Attach callback for button2
  92.  
  93.   relay1.begin(); // Initialize relay1
  94.   relay2.begin(); // Initialize relay2
  95.   relay3.begin(); // Initialize relay3
  96. }
  97.  
  98. void loop(void) {
  99.   // put your main code here, to run repeatedly:
  100.  
  101.   button1.read(); // Read the state of button1
  102.   button2.read(); // Read the state of button2
  103.  
  104.   checkButton2Released(); // Check if button2 is released
  105.  
  106.   updateOutputs(); // Refresh output data
  107. }
  108.  
  109. void updateOutputs() {
  110.   digitalWrite(Relay1_RelayModule_Signal_PIN_D4, Relay1_RelayModule_Signal_PIN_D4_rawData);
  111.   digitalWrite(Relay2_RelayModule_Signal_PIN_D5, Relay2_RelayModule_Signal_PIN_D5_rawData);
  112.   digitalWrite(Relay3_RelayModule_Signal_PIN_D6, Relay3_RelayModule_Signal_PIN_D6_rawData);
  113. }
  114.  
  115. void onButton1Pressed() {
  116.   Serial.println("Button1 pressed");
  117.   // SYSTEM REQUIREMENT 1
  118.   relay1.turnOn(); // Activate Relay1
  119.   delay(10000); // Wait for 10 seconds
  120.   relay1.turnOff(); // Deactivate Relay1
  121.   relay2.turnOn(); // Activate Relay2
  122.   delay(20000); // Wait for 20 seconds
  123.   relay2.turnOff(); // Deactivate Relay2
  124. }
  125.  
  126. void onButton2Pressed() {
  127.   Serial.println("Button2 pressed");
  128.   // SYSTEM REQUIREMENT 2
  129.   relay3.turnOn(); // Activate Relay3
  130. }
  131.  
  132. void checkButton2Released() {
  133.   if (button2.isReleased()) {
  134.     Serial.println("Button2 released");
  135.     // SYSTEM REQUIREMENT 2
  136.     relay3.turnOff(); // Deactivate Relay3
  137.   }
  138. }
  139.  
  140. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement