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: Relay Timer
- - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
- - Source Code created on: 2025-08-13 11:26:21
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* When press button mobile relay 1 on and hold until */
- /* finished set time.after finished time another */
- /* delay start.its need to set from mobile as 1 to 5 */
- /* minutes.delay finished lid open a message display */
- /* in display.and off all. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <ezBuzzer.h> // https://github.com/ArduinoGetStarted/buzzer
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void startRelayTimer();
- void stopRelayTimer();
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- // Buzzer pin
- const uint8_t myPassiveBuzzer_PassiveBuzzer_Signal_PIN_D1 = 1;
- // Relay control pin (simulated as output)
- const uint8_t relayControlPin = 2;
- // Button to start relay timer
- const uint8_t relayButtonPin = 3;
- /***** DEFINITION OF SYSTEM VARIABLES *****/
- // Relay timer duration in minutes (set from mobile, here simulated as a variable)
- uint8_t relayDelayMinutes = 3; // Example: 1 to 5 minutes
- // State variables
- bool relayActive = false;
- unsigned long relayStartTime = 0;
- unsigned long relayDelayMillis = 0;
- // Button state tracking
- int lastRelayButtonState = HIGH;
- // Instantiate ezBuzzer object
- ezBuzzer buzzer(BUZZER_BEEP_DELAY); // Using default pin 3, but we will override with our pin
- // Override buzzer pin with our defined pin
- // Since constructor uses default pin, we set the pin directly
- // But as per library, constructor can accept pin, so we instantiate with our pin
- // So, re-instantiate with correct pin
- // Let's re-instantiate properly
- // Remove previous instantiation and create with correct pin
- // So, replace above with:
- ezBuzzer buzzer(myPassiveBuzzer_PassiveBuzzer_Signal_PIN_D1);
- void setup(void)
- {
- // put your setup code here, to run once:
- Serial.begin(9600);
- pinMode(relayControlPin, OUTPUT);
- digitalWrite(relayControlPin, LOW); // Ensure relay is off
- pinMode(relayButtonPin, INPUT_PULLUP);
- lastRelayButtonState = digitalRead(relayButtonPin);
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- handleRelayButton();
- updateRelayState();
- buzzer.loop(); // Keep buzzer updated
- }
- void handleRelayButton()
- {
- int currentButtonState = digitalRead(relayButtonPin);
- if (lastRelayButtonState == HIGH && currentButtonState == LOW)
- {
- // Button pressed
- startRelayTimer();
- }
- lastRelayButtonState = currentButtonState;
- }
- void startRelayTimer()
- {
- if (!relayActive)
- {
- relayActive = true;
- relayStartTime = millis();
- relayDelayMillis = relayDelayMinutes * 60 * 1000UL; // Convert minutes to milliseconds
- digitalWrite(relayControlPin, HIGH); // Turn relay ON
- buzzer.beep(100); // Beep to indicate relay started
- Serial.println("Relay activated");
- }
- }
- void updateRelayState()
- {
- if (relayActive)
- {
- unsigned long currentTime = millis();
- if (currentTime - relayStartTime >= relayDelayMillis)
- {
- // Delay finished
- relayActive = false;
- digitalWrite(relayControlPin, LOW); // Turn relay OFF
- Serial.println("Delay finished, relay off");
- // Display message
- Serial.println("Delay completed. System OFF.");
- // Optionally, add code to reset or wait for next activation
- }
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment