Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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: Debounced Relay
- - Source Code NOT compiled for: Arduino Nano ESP32
- - Source Code created on: 2025-08-18 13:27:22
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* esp32 with i2c 16/1 display.pin no 12 for start */
- /* swich.pin no 13 for stop swich,pin no 15 for time */
- /* setting up swich,pin 16 for time setting down */
- /* swich.pin no 14 for relay output.delay 1 is 1 to 5 */
- /* minute,delay 2 is 1 to 10 minutes. */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* esp32 with i2c 16/1 lcd display timer */
- /* controller.pin no 12 start swich,pin no 13 stop */
- /* swich,pin no 15 for time u setting,pin no 16 time */
- /* setting down swich.pin no 14 relay ouput */
- /* module.delay 1 is 1 to 5 minutes,delay 2 is 1 to */
- /* 10 minutes. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Relay.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t RelayPin = 14; // Relay control pin as per system requirements
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- Relay light(RelayPin, false); // false = Normally Closed by default in this setup
- /****** ADDITIONAL GLOBALS FOR INPUTS AND STATE ******/
- const uint8_t START_PIN = 12; // start switch
- const uint8_t STOP_PIN = 13; // stop switch
- const uint8_t TIME_UP_PIN = 15; // time up / increase
- const uint8_t TIME_DOWN_PIN = 16; // time down / decrease
- const unsigned long DEBOUNCE_DELAY = 50; // simple debounce delay in ms
- bool countdownActive = false;
- unsigned long countdownStartMs = 0;
- unsigned long countdownTargetMs = 0;
- uint8_t mode = 0; // 0 = adjust delay1 (1-5 min), 1 = adjust delay2 (1-10 min)
- uint8_t delay1Minutes = 1; // 1..5
- uint8_t delay2Minutes = 1; // 1..10
- bool relayOn = false;
- // Debounce state
- bool prevStart = HIGH;
- bool prevStop = HIGH;
- bool prevUp = HIGH;
- bool prevDown = HIGH;
- /******** SETUP ********/
- void setup(void) {
- Serial.begin(115200);
- // Initialize relay
- light.begin();
- // Set input pins
- pinMode(START_PIN, INPUT_PULLUP);
- pinMode(STOP_PIN, INPUT_PULLUP);
- pinMode(TIME_UP_PIN, INPUT_PULLUP);
- pinMode(TIME_DOWN_PIN, INPUT_PULLUP);
- // Ensure relay is off at startup
- light.turnOff();
- relayOn = false;
- countdownActive = false;
- Serial.println("System initialized");
- }
- /******** MAIN LOOP ********/
- void loop(void) {
- // Debounced edge detection for Start
- bool startPressedNow = false;
- int currStart = digitalRead(START_PIN);
- if (currStart != prevStart) {
- delay(20);
- currStart = digitalRead(START_PIN);
- }
- if (currStart == LOW && prevStart == HIGH) {
- startPressedNow = true;
- }
- prevStart = currStart;
- // Debounced edge for Stop
- bool stopPressedNow = false;
- int currStop = digitalRead(STOP_PIN);
- if (currStop != prevStop) {
- delay(20);
- currStop = digitalRead(STOP_PIN);
- }
- if (currStop == LOW && prevStop == HIGH) {
- stopPressedNow = true;
- }
- prevStop = currStop;
- // Debounced edge for Time Up
- bool timeUpPressedNow = false;
- int currUp = digitalRead(TIME_UP_PIN);
- if (currUp != prevUp) {
- delay(20);
- currUp = digitalRead(TIME_UP_PIN);
- }
- if (currUp == LOW && prevUp == HIGH) {
- timeUpPressedNow = true;
- }
- prevUp = currUp;
- // Debounced edge for Time Down
- bool timeDownPressedNow = false;
- int currDown = digitalRead(TIME_DOWN_PIN);
- if (currDown != prevDown) {
- delay(20);
- currDown = digitalRead(TIME_DOWN_PIN);
- }
- if (currDown == LOW && prevDown == HIGH) {
- timeDownPressedNow = true;
- }
- prevDown = currDown;
- // Time setting adjustments
- if (timeUpPressedNow) {
- if (mode == 0) {
- if (delay1Minutes < 5) delay1Minutes++;
- } else {
- if (delay2Minutes < 10) delay2Minutes++;
- }
- }
- if (timeDownPressedNow) {
- if (mode == 0) {
- if (delay1Minutes > 1) delay1Minutes--;
- } else {
- if (delay2Minutes > 1) delay2Minutes--;
- }
- }
- // Mode switch: toggle mode when Stop is pressed
- if (stopPressedNow) {
- mode = (mode + 1) % 2;
- Serial.print("Mode switched to: ");
- Serial.println(mode == 0 ? "Delay1 (1-5 min)" : "Delay2 (1-10 min)");
- }
- // Start countdown
- if (startPressedNow && !countdownActive && !relayOn) {
- countdownActive = true;
- countdownStartMs = millis();
- countdownTargetMs = ((mode == 0) ? delay1Minutes : delay2Minutes) * 60000UL;
- Serial.print("Countdown started: ");
- Serial.print((mode == 0) ? delay1Minutes : delay2Minutes);
- Serial.println(" min");
- }
- // Countdown progress
- if (countdownActive) {
- unsigned long elapsed = millis() - countdownStartMs;
- if (elapsed >= countdownTargetMs) {
- countdownActive = false;
- light.turnOn();
- relayOn = true;
- Serial.println("Countdown finished. Relay ON.");
- }
- }
- // Stop action turns relay OFF
- if (relayOn && stopPressedNow) {
- light.turnOff();
- relayOn = false;
- Serial.println("Relay OFF due to STOP.");
- }
- // Periodic status update
- static unsigned long lastStatus = 0;
- if (millis() - lastStatus > 1000) {
- lastStatus = millis();
- Serial.print("Mode: ");
- Serial.print(mode == 0 ? "Delay1" : "Delay2");
- Serial.print(" | Delay1: ");
- Serial.print(delay1Minutes);
- Serial.print("m | Delay2: ");
- Serial.print(delay2Minutes);
- Serial.print("m | CountdownActive: ");
- Serial.print(countdownActive);
- Serial.print(" | RelayOn: ");
- Serial.println(relayOn);
- if (countdownActive) {
- unsigned long remaining = countdownTargetMs - (millis() - countdownStartMs);
- uint16_t remMin = remaining / 60000;
- uint16_t remSec = (remaining % 60000) / 1000;
- Serial.print("Time remaining: ");
- Serial.print(remMin);
- Serial.print("m ");
- Serial.print(remSec);
- Serial.println("s");
- }
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment