Advertisement
pleasedontcode

**Relay Control** rev_02

Nov 6th, 2024
83
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:45:27
  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.     /* off ho Jaye or 30 minutes ke baad automatic realy */
  23.     /* on ho jaye uske baad sensor ke according realy */
  24.     /* 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 <Arduino_APDS9960.h>   //https://github.com/arduino-libraries/Arduino_APDS9960
  32. #include <Arduino.h> // Required for Arduino functions like pinMode, digitalRead, etc.
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39.  
  40. const int sensorPin = 2; // Sensor pin (digital)
  41. const int relayPin = 7; // Relay pin
  42. bool sensorState = false;
  43. bool relayState = false;
  44. unsigned long startTime = 0;
  45. unsigned long relayOffTime = 0;
  46.  
  47. void setup(void)
  48. {
  49.     // put your setup code here, to run once:
  50.     Serial.begin(9600);
  51.     pinMode(sensorPin, INPUT);
  52.     pinMode(relayPin, OUTPUT);
  53. }
  54.  
  55. void loop(void)
  56. {
  57.     // put your main code here, to run repeatedly:
  58.     int sensorValue = digitalRead(sensorPin);
  59.     if (sensorValue == HIGH) { // Sensor value check
  60.         sensorState = true;
  61.         digitalWrite(relayPin, HIGH);
  62.         relayState = true;
  63.         startTime = millis(); // Reset start time when relay is turned on
  64.         Serial.println("Relay On");
  65.     } else {
  66.         sensorState = false;
  67.         if (relayState) {
  68.             if (millis() - startTime > 15000) { // 15 second timer
  69.                 digitalWrite(relayPin, LOW);
  70.                 relayState = false;
  71.                 relayOffTime = millis();
  72.                 Serial.println("Relay Off");
  73.             }
  74.         }
  75.         if (!relayState && millis() - relayOffTime > 1800000) { // 30 minute timer
  76.             digitalWrite(relayPin, HIGH);
  77.             relayState = true;
  78.             startTime = millis(); // Reset start time when relay is turned on automatically
  79.             Serial.println("Relay On (Auto)");
  80.         }
  81.     }
  82.  
  83.     delay(100);
  84. }
  85.  
  86. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement