CCCPaste

**Relay Control** rev_01

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: **Relay Control**
	- Source Code NOT compiled for: Arduino Nano
	- Source Code created on: 2024-11-06 12:36:02

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Ek realy Jo float megnetic sensor high ho tab */
	/* realy on rahe or sensor low hone ke 15 second bad */
	/* realy off ho jaye or 30 minutes ke baad automatic */
	/* realy on ho jaye jab realy on ho uske baad sensor */
	/* ke according realy work kare */
/****** END SYSTEM REQUIREMENTS *****/

/* START CODE */

/****** DEFINITION OF LIBRARIES *****/
#include  // https://github.com/khoih-prog/ESP_AT_WiFiManager
#include               // https://github.com/rafaelnsantos/Relay
#include            // Included for Arduino functions

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t Realy_RelayModule_Signal_PIN_D2 = 2; // This pin is used for the relay
const int sensorPin = 3; // Changed to avoid conflict with Realy_RelayModule_Signal_PIN_D2
const int relayPin = 7;  // Relay pin

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
bool Realy_RelayModule_Signal_PIN_D2_rawData = LOW;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
float Realy_RelayModule_Signal_PIN_D2_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/

bool sensorState = false;
bool relayState = false;
unsigned long startTime = 0;
unsigned long relayOffTime = 0;

void setup(void)
{
    pinMode(Realy_RelayModule_Signal_PIN_D2, OUTPUT);
    pinMode(relayPin, OUTPUT); // Set relay pin as output
    pinMode(sensorPin, INPUT); // Set sensor pin as input
    Serial.begin(9600); // Initialize serial communication
}

void loop(void)
{
    updateOutputs(); // Refresh output data

    int sensorValue = digitalRead(sensorPin);
    if (sensorValue == HIGH) { // Sensor value check
        sensorState = true;
        digitalWrite(relayPin, HIGH);
        Realy_RelayModule_Signal_PIN_D2_rawData = HIGH; // Update relay state
        relayState = true;
        startTime = millis(); // Reset start time when sensor is high
        Serial.println("Relay On");
    } else {
        sensorState = false;
        if (relayState) {
            if (millis() - startTime > 15000) { // 15 second timer
                digitalWrite(relayPin, LOW);
                Realy_RelayModule_Signal_PIN_D2_rawData = LOW; // Update relay state
                relayState = false;
                relayOffTime = millis();
                Serial.println("Relay Off");
            }
        }
        if (!relayState && millis() - relayOffTime > 1800000) { // 30 minute timer
            digitalWrite(relayPin, HIGH);
            Realy_RelayModule_Signal_PIN_D2_rawData = HIGH; // Update relay state
            relayState = true;
            Serial.println("Relay On (Auto)");
        }
    }

    delay(100);
}

void updateOutputs()
{
    digitalWrite(Realy_RelayModule_Signal_PIN_D2, Realy_RelayModule_Signal_PIN_D2_rawData);
}

/* END CODE */