pleasedontcode

Relay Timer rev_01

Aug 13th, 2025
132
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 Timer
  13.     - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2025-08-13 11:26:21
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* When press button mobile relay 1 on and hold until */
  21.     /* finished set time.after finished time another */
  22.     /* delay start.its need to set from mobile as 1 to 5 */
  23.     /* minutes.delay finished lid open a message display */
  24.     /* in display.and off all. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <ezBuzzer.h> // https://github.com/ArduinoGetStarted/buzzer
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void startRelayTimer();
  36. void stopRelayTimer();
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. // Buzzer pin
  40. const uint8_t myPassiveBuzzer_PassiveBuzzer_Signal_PIN_D1 = 1;
  41. // Relay control pin (simulated as output)
  42. const uint8_t relayControlPin = 2;
  43. // Button to start relay timer
  44. const uint8_t relayButtonPin = 3;
  45.  
  46. /***** DEFINITION OF SYSTEM VARIABLES *****/
  47. // Relay timer duration in minutes (set from mobile, here simulated as a variable)
  48. uint8_t relayDelayMinutes = 3; // Example: 1 to 5 minutes
  49.  
  50. // State variables
  51. bool relayActive = false;
  52. unsigned long relayStartTime = 0;
  53. unsigned long relayDelayMillis = 0;
  54.  
  55. // Button state tracking
  56. int lastRelayButtonState = HIGH;
  57.  
  58. // Instantiate ezBuzzer object
  59. ezBuzzer buzzer(BUZZER_BEEP_DELAY); // Using default pin 3, but we will override with our pin
  60.  
  61. // Override buzzer pin with our defined pin
  62. // Since constructor uses default pin, we set the pin directly
  63. // But as per library, constructor can accept pin, so we instantiate with our pin
  64. // So, re-instantiate with correct pin
  65. // Let's re-instantiate properly
  66. // Remove previous instantiation and create with correct pin
  67. // So, replace above with:
  68. ezBuzzer buzzer(myPassiveBuzzer_PassiveBuzzer_Signal_PIN_D1);
  69.  
  70. void setup(void)
  71. {
  72.   // put your setup code here, to run once:
  73.   Serial.begin(9600);
  74.   pinMode(relayControlPin, OUTPUT);
  75.   digitalWrite(relayControlPin, LOW); // Ensure relay is off
  76.   pinMode(relayButtonPin, INPUT_PULLUP);
  77.   lastRelayButtonState = digitalRead(relayButtonPin);
  78. }
  79.  
  80. void loop(void)
  81. {
  82.   // put your main code here, to run repeatedly:
  83.   handleRelayButton();
  84.   updateRelayState();
  85.   buzzer.loop(); // Keep buzzer updated
  86. }
  87.  
  88. void handleRelayButton()
  89. {
  90.   int currentButtonState = digitalRead(relayButtonPin);
  91.   if (lastRelayButtonState == HIGH && currentButtonState == LOW)
  92.   {
  93.     // Button pressed
  94.     startRelayTimer();
  95.   }
  96.   lastRelayButtonState = currentButtonState;
  97. }
  98.  
  99. void startRelayTimer()
  100. {
  101.   if (!relayActive)
  102.   {
  103.     relayActive = true;
  104.     relayStartTime = millis();
  105.     relayDelayMillis = relayDelayMinutes * 60 * 1000UL; // Convert minutes to milliseconds
  106.     digitalWrite(relayControlPin, HIGH); // Turn relay ON
  107.     buzzer.beep(100); // Beep to indicate relay started
  108.     Serial.println("Relay activated");
  109.   }
  110. }
  111.  
  112. void updateRelayState()
  113. {
  114.   if (relayActive)
  115.   {
  116.     unsigned long currentTime = millis();
  117.     if (currentTime - relayStartTime >= relayDelayMillis)
  118.     {
  119.       // Delay finished
  120.       relayActive = false;
  121.       digitalWrite(relayControlPin, LOW); // Turn relay OFF
  122.       Serial.println("Delay finished, relay off");
  123.       // Display message
  124.       Serial.println("Delay completed. System OFF.");
  125.       // Optionally, add code to reset or wait for next activation
  126.     }
  127.   }
  128. }
  129.  
  130. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment