Advertisement
pleasedontcode

Relay Control rev_01

Oct 20th, 2024
72
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-10-20 13:41:40
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* turns on relay when button is pushed */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  25. #include <Relay.h>  //https://github.com/rafaelnsantos/Relay
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30.  
  31. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  32. const uint8_t Switch1_PushButton_PIN_D2     = 2;
  33.  
  34. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  35. const uint8_t Relay1_RelayModule_Signal_PIN_D3      = 3;
  36.  
  37. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  38. bool Relay1_RelayModule_Signal_PIN_D3_rawData       = false; // Initialize as false (relay off)
  39.  
  40. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  41. // Instance of the EasyButton for the push button
  42. EasyButton button(Switch1_PushButton_PIN_D2); // Initialize EasyButton with the push button pin
  43. // Instance of the Relay for the relay module
  44. Relay relay(Relay1_RelayModule_Signal_PIN_D3, true); // Initialize Relay with the relay pin and Normally Open configuration
  45.  
  46. void setup(void)
  47. {
  48.     Serial.begin(115200); // Initialize Serial for debugging
  49.     Serial.println("Setup started");
  50.  
  51.     pinMode(Switch1_PushButton_PIN_D2, INPUT_PULLUP); // Set the push button pin as input with pull-up
  52.     pinMode(Relay1_RelayModule_Signal_PIN_D3, OUTPUT); // Set the relay pin as output
  53.  
  54.     // Initialize the button
  55.     button.begin(); // Start the EasyButton
  56.     button.onPressed([]() { // Lambda function for button press event
  57.         Serial.println("Button pressed");
  58.         Relay1_RelayModule_Signal_PIN_D3_rawData = true; // Set relay state to on
  59.         updateOutputs(); // Refresh output data
  60.     });
  61. }
  62.  
  63. void loop(void)
  64. {
  65.     button.read(); // Continuously read the status of the button
  66.     relay.loop(); // Call relay loop to manage relay timing
  67. }
  68.  
  69. void updateOutputs()
  70. {
  71.     digitalWrite(Relay1_RelayModule_Signal_PIN_D3, Relay1_RelayModule_Signal_PIN_D3_rawData); // Update relay state
  72. }
  73.  
  74. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement