Advertisement
pleasedontcode

Relay Toggle rev_02

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