Advertisement
pleasedontcode

Relay Toggle rev_03

Jul 28th, 2024
187
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:58:41
  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.  
  24. /********* User code review feedback **********
  25. #### Feedback 1 ####
  26. - 'class Relay' has no member named 'loop'
  27. #### Feedback 2 ####
  28. - avvia con il rele normalmente chiuso
  29. ********* User code review feedback **********/
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32. #include <Relay.h>  //https://github.com/rafaelnsantos/Relay
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void updateOutputs(void); // Function prototype for updating outputs
  38.  
  39. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  40. const uint8_t rele_RelayModule_Signal_PIN_D2        = 2;
  41.  
  42. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  43. /***** used to store raw data *****/
  44. bool    rele_RelayModule_Signal_PIN_D2_rawData      = 1; // Start with relay ON (Normally Closed)
  45.  
  46. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  47. /***** used to store data after characteristic curve transformation *****/
  48. float   rele_RelayModule_Signal_PIN_D2_phyData      = 0.0;
  49.  
  50. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  51. // Create a relay on pin 2
  52. Relay relay(rele_RelayModule_Signal_PIN_D2, false); // Initialize relay in Normally Closed mode
  53.  
  54. // Timing variables
  55. unsigned long previousMillis = 0; // Store the last time the relay was toggled
  56. const unsigned long interval = 12 * 60 * 60 * 1000; // 12 hours in milliseconds
  57.  
  58. void setup(void)
  59. {
  60.     // put your setup code here, to run once:
  61.     Serial.begin(9600); // Initialize serial communication for debugging
  62.     relay.begin(); // Initialize the relay
  63.     pinMode(rele_RelayModule_Signal_PIN_D2, OUTPUT); // Set the relay pin as output
  64.     updateOutputs(); // Ensure the initial state is set correctly
  65. }
  66.  
  67. void loop(void)
  68. {
  69.     // put your main code here, to run repeatedly:
  70.     unsigned long currentMillis = millis(); // Get the current time
  71.  
  72.     // Check if 12 hours have passed
  73.     if (currentMillis - previousMillis >= interval) {
  74.         previousMillis = currentMillis; // Save the last time the relay was toggled
  75.         rele_RelayModule_Signal_PIN_D2_rawData = !rele_RelayModule_Signal_PIN_D2_rawData; // Toggle the relay state
  76.         updateOutputs(); // Refresh output data
  77.     }
  78. }
  79.  
  80. void updateOutputs()
  81. {
  82.     // Update the relay state based on the raw data
  83.     digitalWrite(rele_RelayModule_Signal_PIN_D2, rele_RelayModule_Signal_PIN_D2_rawData);
  84. }
  85.  
  86. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement