Advertisement
pleasedontcode

Relay Toggle rev_01

Jul 28th, 2024
190
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 Toggle
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-07-28 08:32:29
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Accendi e spegni il rele ogni 12 ore */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Relay.h>  //https://github.com/rafaelnsantos/Relay
  25.  
  26. /****** FUNCTION PROTOTYPES *****/
  27. void setup(void);
  28. void loop(void);
  29. void updateOutputs(void); // Function prototype for updating outputs
  30.  
  31. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  32. const uint8_t rele_RelayModule_Signal_PIN_D2        = 2;
  33.  
  34. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  35. /***** used to store raw data *****/
  36. bool    rele_RelayModule_Signal_PIN_D2_rawData      = 0;
  37.  
  38. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  39. /***** used to store data after characteristic curve transformation *****/
  40. float   rele_RelayModule_Signal_PIN_D2_phyData      = 0.0;
  41.  
  42. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  43. // Create a relay on pin 2
  44. Relay relay(rele_RelayModule_Signal_PIN_D2, true); // Initialize relay in Normally Open mode
  45.  
  46. // Timing variables
  47. unsigned long previousMillis = 0; // Store the last time the relay was toggled
  48. const unsigned long interval = 12 * 60 * 60 * 1000; // 12 hours in milliseconds
  49.  
  50. void setup(void)
  51. {
  52.     // put your setup code here, to run once:
  53.     Serial.begin(9600); // Initialize serial communication for debugging
  54.     relay.begin(); // Initialize the relay
  55.     pinMode(rele_RelayModule_Signal_PIN_D2, OUTPUT); // Set the relay pin as output
  56. }
  57.  
  58. void loop(void)
  59. {
  60.     // put your main code here, to run repeatedly:
  61.     unsigned long currentMillis = millis(); // Get the current time
  62.  
  63.     // Check if 12 hours have passed
  64.     if (currentMillis - previousMillis >= interval) {
  65.         previousMillis = currentMillis; // Save the last time the relay was toggled
  66.         rele_RelayModule_Signal_PIN_D2_rawData = !rele_RelayModule_Signal_PIN_D2_rawData; // Toggle the relay state
  67.         updateOutputs(); // Refresh output data
  68.     }
  69.  
  70.     relay.loop(); // Call the relay loop to manage its state
  71. }
  72.  
  73. void updateOutputs()
  74. {
  75.     // Update the relay state based on the raw data
  76.     digitalWrite(rele_RelayModule_Signal_PIN_D2, rele_RelayModule_Signal_PIN_D2_rawData);
  77. }
  78.  
  79. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement