pleasedontcode

Debounced Relay rev_01

Aug 18th, 2025
396
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: Debounced Relay
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2025-08-18 13:27:22
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* esp32 with i2c 16/1 display.pin no 12 for start */
  21.     /* swich.pin no 13 for stop swich,pin no 15 for time */
  22.     /* setting up swich,pin 16 for time setting down */
  23.     /* swich.pin no 14 for relay output.delay 1 is 1 to 5 */
  24.     /* minute,delay 2 is 1 to 10 minutes. */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* esp32 with i2c 16/1 lcd display timer */
  27.     /* controller.pin no 12 start swich,pin no 13 stop */
  28.     /* swich,pin no 15 for time u setting,pin no 16 time */
  29.     /* setting down swich.pin no 14 relay ouput */
  30.     /* module.delay 1 is 1 to 5 minutes,delay 2 is 1 to */
  31.     /* 10 minutes. */
  32. /****** END SYSTEM REQUIREMENTS *****/
  33.  
  34.  
  35. /* START CODE */
  36.  
  37. /****** DEFINITION OF LIBRARIES *****/
  38. #include <Relay.h>
  39.  
  40. /****** FUNCTION PROTOTYPES *****/
  41. void setup(void);
  42. void loop(void);
  43.  
  44. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  45. const uint8_t RelayPin = 14; // Relay control pin as per system requirements
  46.  
  47. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  48. Relay light(RelayPin, false); // false = Normally Closed by default in this setup
  49.  
  50. /****** ADDITIONAL GLOBALS FOR INPUTS AND STATE ******/
  51. const uint8_t START_PIN = 12;       // start switch
  52. const uint8_t STOP_PIN  = 13;       // stop switch
  53. const uint8_t TIME_UP_PIN = 15;      // time up / increase
  54. const uint8_t TIME_DOWN_PIN = 16;    // time down / decrease
  55.  
  56. const unsigned long DEBOUNCE_DELAY = 50; // simple debounce delay in ms
  57.  
  58. bool countdownActive = false;
  59. unsigned long countdownStartMs = 0;
  60. unsigned long countdownTargetMs = 0;
  61.  
  62. uint8_t mode = 0; // 0 = adjust delay1 (1-5 min), 1 = adjust delay2 (1-10 min)
  63. uint8_t delay1Minutes = 1; // 1..5
  64. uint8_t delay2Minutes = 1; // 1..10
  65. bool relayOn = false;
  66.  
  67. // Debounce state
  68. bool prevStart = HIGH;
  69. bool prevStop = HIGH;
  70. bool prevUp = HIGH;
  71. bool prevDown = HIGH;
  72.  
  73. /******** SETUP ********/
  74. void setup(void) {
  75.   Serial.begin(115200);
  76.   // Initialize relay
  77.   light.begin();
  78.   // Set input pins
  79.   pinMode(START_PIN, INPUT_PULLUP);
  80.   pinMode(STOP_PIN, INPUT_PULLUP);
  81.   pinMode(TIME_UP_PIN, INPUT_PULLUP);
  82.   pinMode(TIME_DOWN_PIN, INPUT_PULLUP);
  83.  
  84.   // Ensure relay is off at startup
  85.   light.turnOff();
  86.   relayOn = false;
  87.   countdownActive = false;
  88.  
  89.   Serial.println("System initialized");
  90. }
  91.  
  92. /******** MAIN LOOP ********/
  93. void loop(void) {
  94.   // Debounced edge detection for Start
  95.   bool startPressedNow = false;
  96.   int currStart = digitalRead(START_PIN);
  97.   if (currStart != prevStart) {
  98.     delay(20);
  99.     currStart = digitalRead(START_PIN);
  100.   }
  101.   if (currStart == LOW && prevStart == HIGH) {
  102.     startPressedNow = true;
  103.   }
  104.   prevStart = currStart;
  105.  
  106.   // Debounced edge for Stop
  107.   bool stopPressedNow = false;
  108.   int currStop = digitalRead(STOP_PIN);
  109.   if (currStop != prevStop) {
  110.     delay(20);
  111.     currStop = digitalRead(STOP_PIN);
  112.   }
  113.   if (currStop == LOW && prevStop == HIGH) {
  114.     stopPressedNow = true;
  115.   }
  116.   prevStop = currStop;
  117.  
  118.   // Debounced edge for Time Up
  119.   bool timeUpPressedNow = false;
  120.   int currUp = digitalRead(TIME_UP_PIN);
  121.   if (currUp != prevUp) {
  122.     delay(20);
  123.     currUp = digitalRead(TIME_UP_PIN);
  124.   }
  125.   if (currUp == LOW && prevUp == HIGH) {
  126.     timeUpPressedNow = true;
  127.   }
  128.   prevUp = currUp;
  129.  
  130.   // Debounced edge for Time Down
  131.   bool timeDownPressedNow = false;
  132.   int currDown = digitalRead(TIME_DOWN_PIN);
  133.   if (currDown != prevDown) {
  134.     delay(20);
  135.     currDown = digitalRead(TIME_DOWN_PIN);
  136.   }
  137.   if (currDown == LOW && prevDown == HIGH) {
  138.     timeDownPressedNow = true;
  139.   }
  140.   prevDown = currDown;
  141.  
  142.   // Time setting adjustments
  143.   if (timeUpPressedNow) {
  144.     if (mode == 0) {
  145.       if (delay1Minutes < 5) delay1Minutes++;
  146.     } else {
  147.       if (delay2Minutes < 10) delay2Minutes++;
  148.     }
  149.   }
  150.   if (timeDownPressedNow) {
  151.     if (mode == 0) {
  152.       if (delay1Minutes > 1) delay1Minutes--;
  153.     } else {
  154.       if (delay2Minutes > 1) delay2Minutes--;
  155.     }
  156.   }
  157.  
  158.   // Mode switch: toggle mode when Stop is pressed
  159.   if (stopPressedNow) {
  160.     mode = (mode + 1) % 2;
  161.     Serial.print("Mode switched to: ");
  162.     Serial.println(mode == 0 ? "Delay1 (1-5 min)" : "Delay2 (1-10 min)");
  163.   }
  164.  
  165.   // Start countdown
  166.   if (startPressedNow && !countdownActive && !relayOn) {
  167.     countdownActive = true;
  168.     countdownStartMs = millis();
  169.     countdownTargetMs = ((mode == 0) ? delay1Minutes : delay2Minutes) * 60000UL;
  170.     Serial.print("Countdown started: ");
  171.     Serial.print((mode == 0) ? delay1Minutes : delay2Minutes);
  172.     Serial.println(" min");
  173.   }
  174.  
  175.   // Countdown progress
  176.   if (countdownActive) {
  177.     unsigned long elapsed = millis() - countdownStartMs;
  178.     if (elapsed >= countdownTargetMs) {
  179.       countdownActive = false;
  180.       light.turnOn();
  181.       relayOn = true;
  182.       Serial.println("Countdown finished. Relay ON.");
  183.     }
  184.   }
  185.  
  186.   // Stop action turns relay OFF
  187.   if (relayOn && stopPressedNow) {
  188.     light.turnOff();
  189.     relayOn = false;
  190.     Serial.println("Relay OFF due to STOP.");
  191.   }
  192.  
  193.   // Periodic status update
  194.   static unsigned long lastStatus = 0;
  195.   if (millis() - lastStatus > 1000) {
  196.     lastStatus = millis();
  197.     Serial.print("Mode: ");
  198.     Serial.print(mode == 0 ? "Delay1" : "Delay2");
  199.     Serial.print(" | Delay1: ");
  200.     Serial.print(delay1Minutes);
  201.     Serial.print("m | Delay2: ");
  202.     Serial.print(delay2Minutes);
  203.     Serial.print("m | CountdownActive: ");
  204.     Serial.print(countdownActive);
  205.     Serial.print(" | RelayOn: ");
  206.     Serial.println(relayOn);
  207.     if (countdownActive) {
  208.       unsigned long remaining = countdownTargetMs - (millis() - countdownStartMs);
  209.       uint16_t remMin = remaining / 60000;
  210.       uint16_t remSec = (remaining % 60000) / 1000;
  211.       Serial.print("Time remaining: ");
  212.       Serial.print(remMin);
  213.       Serial.print("m ");
  214.       Serial.print(remSec);
  215.       Serial.println("s");
  216.     }
  217.   }
  218. }
  219.  
  220. /* END CODE */
  221.  
Advertisement
Add Comment
Please, Sign In to add comment