Advertisement
pleasedontcode

**Relay Control** rev_01

Nov 6th, 2024
71
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 Nano
  14.     - Source Code created on: 2024-11-06 12:36:02
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Ek realy Jo float megnetic sensor high ho tab */
  21.     /* realy on rahe or sensor low hone ke 15 second bad */
  22.     /* realy off ho jaye or 30 minutes ke baad automatic */
  23.     /* realy on ho jaye jab realy on ho uske baad sensor */
  24.     /* ke according realy work kare */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <ESP_AT_WiFiManager.h> // https://github.com/khoih-prog/ESP_AT_WiFiManager
  31. #include <Relay.h>              // https://github.com/rafaelnsantos/Relay
  32. #include <Arduino.h>           // Included for Arduino functions
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. const uint8_t Realy_RelayModule_Signal_PIN_D2 = 2; // This pin is used for the relay
  40. const int sensorPin = 3; // Changed to avoid conflict with Realy_RelayModule_Signal_PIN_D2
  41. const int relayPin = 7;  // Relay pin
  42.  
  43. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  44. bool Realy_RelayModule_Signal_PIN_D2_rawData = LOW;
  45.  
  46. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  47. float Realy_RelayModule_Signal_PIN_D2_phyData = 0.0;
  48.  
  49. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  50.  
  51. bool sensorState = false;
  52. bool relayState = false;
  53. unsigned long startTime = 0;
  54. unsigned long relayOffTime = 0;
  55.  
  56. void setup(void)
  57. {
  58.     pinMode(Realy_RelayModule_Signal_PIN_D2, OUTPUT);
  59.     pinMode(relayPin, OUTPUT); // Set relay pin as output
  60.     pinMode(sensorPin, INPUT); // Set sensor pin as input
  61.     Serial.begin(9600); // Initialize serial communication
  62. }
  63.  
  64. void loop(void)
  65. {
  66.     updateOutputs(); // Refresh output data
  67.  
  68.     int sensorValue = digitalRead(sensorPin);
  69.     if (sensorValue == HIGH) { // Sensor value check
  70.         sensorState = true;
  71.         digitalWrite(relayPin, HIGH);
  72.         Realy_RelayModule_Signal_PIN_D2_rawData = HIGH; // Update relay state
  73.         relayState = true;
  74.         startTime = millis(); // Reset start time when sensor is high
  75.         Serial.println("Relay On");
  76.     } else {
  77.         sensorState = false;
  78.         if (relayState) {
  79.             if (millis() - startTime > 15000) { // 15 second timer
  80.                 digitalWrite(relayPin, LOW);
  81.                 Realy_RelayModule_Signal_PIN_D2_rawData = LOW; // Update relay state
  82.                 relayState = false;
  83.                 relayOffTime = millis();
  84.                 Serial.println("Relay Off");
  85.             }
  86.         }
  87.         if (!relayState && millis() - relayOffTime > 1800000) { // 30 minute timer
  88.             digitalWrite(relayPin, HIGH);
  89.             Realy_RelayModule_Signal_PIN_D2_rawData = HIGH; // Update relay state
  90.             relayState = true;
  91.             Serial.println("Relay On (Auto)");
  92.         }
  93.     }
  94.  
  95.     delay(100);
  96. }
  97.  
  98. void updateOutputs()
  99. {
  100.     digitalWrite(Realy_RelayModule_Signal_PIN_D2, Realy_RelayModule_Signal_PIN_D2_rawData);
  101. }
  102.  
  103. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement