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: Power Timer
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-08-13 03:25:52
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Der ESP32 soll auf Stromspannung reagieren, nach */
- /* 10 Sekunden das Gerät einschalten, 10 Sekunden */
- /* laufen lassen, dann ausschalten. Ein manueller */
- /* Schalter kann das Gerät einschalten und nach 10 */
- /* Sekunden ausschalten Auch wenn der Schalter */
- /* gedrückt bleibt */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- // No external libraries needed
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // Define GPIO pins
- const int powerDetectPin = 34; // GPIO for detecting power supply
- const int manualSwitchPin = 35; // GPIO for manual switch input
- const int deviceControlPin = 2; // GPIO to control the device
- // Timing variables
- unsigned long powerOnTime = 0;
- unsigned long deviceOnTime = 0;
- unsigned long manualControlStartTime = 0;
- bool deviceOn = false;
- bool manualControlActive = false;
- void setup(void)
- {
- // Initialize serial communication for debugging
- Serial.begin(115200);
- // Configure pins
- pinMode(powerDetectPin, INPUT);
- pinMode(manualSwitchPin, INPUT_PULLUP); // Assuming switch connects to GND when pressed
- pinMode(deviceControlPin, OUTPUT);
- // Ensure device is off at start
- digitalWrite(deviceControlPin, LOW);
- }
- void loop(void)
- {
- unsigned long currentTime = millis();
- // Read power detection
- int powerState = digitalRead(powerDetectPin);
- // Assuming power supply presence is indicated by HIGH
- bool powerAvailable = (powerState == HIGH);
- // Read manual switch
- int switchState = digitalRead(manualSwitchPin);
- // Switch pressed when LOW
- bool switchPressed = (switchState == LOW);
- // Handle manual control
- if (switchPressed)
- {
- // Manual control active
- manualControlActive = true;
- manualControlStartTime = currentTime;
- // Turn device on manually
- digitalWrite(deviceControlPin, HIGH);
- deviceOn = true;
- }
- else
- {
- // Manual switch released
- if (manualControlActive)
- {
- // Check if 10 seconds have passed since manual control started
- if (currentTime - manualControlStartTime >= 10000)
- {
- // Turn off device after 10 seconds
- digitalWrite(deviceControlPin, LOW);
- deviceOn = false;
- manualControlActive = false;
- }
- }
- }
- // Automatic control based on power supply
- if (powerAvailable)
- {
- if (!deviceOn && !manualControlActive)
- {
- // Power just supplied
- powerOnTime = currentTime;
- }
- // Check if 10 seconds have passed since power detected
- if ((currentTime - powerOnTime >= 10000) && !deviceOn && !manualControlActive)
- {
- // Turn on device automatically
- digitalWrite(deviceControlPin, HIGH);
- deviceOn = true;
- deviceOnTime = currentTime;
- }
- // Keep device on for 10 seconds after turned on automatically
- if (deviceOn && !manualControlActive)
- {
- if (currentTime - deviceOnTime >= 10000)
- {
- // Turn off device
- digitalWrite(deviceControlPin, LOW);
- deviceOn = false;
- }
- }
- }
- else
- {
- // Power not available
- // Ensure device is off
- digitalWrite(deviceControlPin, LOW);
- deviceOn = false;
- manualControlActive = false;
- }
- // Small delay to debounce and reduce CPU usage
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment