Advertisement
pleasedontcode

"Relay Control" rev_01

Jun 16th, 2024
624
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: "Relay Control"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-16 20:03:21
  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 onButton2Released(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.   button2.onReleased(onButton2Released); // Attach callback for button2 release
  93.  
  94.   relay1.begin(); // Initialize relay1
  95.   relay2.begin(); // Initialize relay2
  96.   relay3.begin(); // Initialize relay3
  97. }
  98.  
  99. void loop(void) {
  100.   // put your main code here, to run repeatedly:
  101.  
  102.   button1.read(); // Read the state of button1
  103.   button2.read(); // Read the state of button2
  104.  
  105.   updateOutputs(); // Refresh output data
  106. }
  107.  
  108. void updateOutputs() {
  109.   digitalWrite(Relay1_RelayModule_Signal_PIN_D4, Relay1_RelayModule_Signal_PIN_D4_rawData);
  110.   digitalWrite(Relay2_RelayModule_Signal_PIN_D5, Relay2_RelayModule_Signal_PIN_D5_rawData);
  111.   digitalWrite(Relay3_RelayModule_Signal_PIN_D6, Relay3_RelayModule_Signal_PIN_D6_rawData);
  112. }
  113.  
  114. void onButton1Pressed() {
  115.   Serial.println("Button1 pressed");
  116.   // SYSTEM REQUIREMENT 1
  117.   relay1.turnOn(); // Activate Relay1
  118.   delay(10000); // Wait for 10 seconds
  119.   relay1.turnOff(); // Deactivate Relay1
  120.   relay2.turnOn(); // Activate Relay2
  121.   delay(20000); // Wait for 20 seconds
  122.   relay2.turnOff(); // Deactivate Relay2
  123. }
  124.  
  125. void onButton2Pressed() {
  126.   Serial.println("Button2 pressed");
  127.   // SYSTEM REQUIREMENT 2
  128.   relay3.turnOn(); // Activate Relay3
  129. }
  130.  
  131. void onButton2Released() {
  132.   Serial.println("Button2 released");
  133.   // SYSTEM REQUIREMENT 2
  134.   relay3.turnOff(); // Deactivate Relay3
  135. }
  136.  
  137. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement