pleasedontcode

Power Timer rev_01

Aug 12th, 2025
326
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: Power Timer
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-08-13 03:25:52
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Der ESP32 soll auf Stromspannung reagieren, nach */
  21.     /* 10 Sekunden das Gerät einschalten, 10 Sekunden */
  22.     /* laufen lassen, dann ausschalten. Ein manueller */
  23.     /* Schalter kann das Gerät einschalten und nach 10 */
  24.     /* Sekunden ausschalten Auch wenn der Schalter */
  25.     /* gedrückt bleibt */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /* START CODE */
  29. /****** DEFINITION OF LIBRARIES *****/
  30. // No external libraries needed
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. // Define GPIO pins
  37. const int powerDetectPin = 34;   // GPIO for detecting power supply
  38. const int manualSwitchPin = 35;  // GPIO for manual switch input
  39. const int deviceControlPin = 2;  // GPIO to control the device
  40.  
  41. // Timing variables
  42. unsigned long powerOnTime = 0;
  43. unsigned long deviceOnTime = 0;
  44. unsigned long manualControlStartTime = 0;
  45.  
  46. bool deviceOn = false;
  47. bool manualControlActive = false;
  48.  
  49. void setup(void)
  50. {
  51.   // Initialize serial communication for debugging
  52.   Serial.begin(115200);
  53.  
  54.   // Configure pins
  55.   pinMode(powerDetectPin, INPUT);
  56.   pinMode(manualSwitchPin, INPUT_PULLUP); // Assuming switch connects to GND when pressed
  57.   pinMode(deviceControlPin, OUTPUT);
  58.  
  59.   // Ensure device is off at start
  60.   digitalWrite(deviceControlPin, LOW);
  61. }
  62.  
  63. void loop(void)
  64. {
  65.   unsigned long currentTime = millis();
  66.  
  67.   // Read power detection
  68.   int powerState = digitalRead(powerDetectPin);
  69.   // Assuming power supply presence is indicated by HIGH
  70.   bool powerAvailable = (powerState == HIGH);
  71.  
  72.   // Read manual switch
  73.   int switchState = digitalRead(manualSwitchPin);
  74.   // Switch pressed when LOW
  75.   bool switchPressed = (switchState == LOW);
  76.  
  77.   // Handle manual control
  78.   if (switchPressed)
  79.   {
  80.     // Manual control active
  81.     manualControlActive = true;
  82.     manualControlStartTime = currentTime;
  83.  
  84.     // Turn device on manually
  85.     digitalWrite(deviceControlPin, HIGH);
  86.     deviceOn = true;
  87.   }
  88.   else
  89.   {
  90.     // Manual switch released
  91.     if (manualControlActive)
  92.     {
  93.       // Check if 10 seconds have passed since manual control started
  94.       if (currentTime - manualControlStartTime >= 10000)
  95.       {
  96.         // Turn off device after 10 seconds
  97.         digitalWrite(deviceControlPin, LOW);
  98.         deviceOn = false;
  99.         manualControlActive = false;
  100.       }
  101.     }
  102.   }
  103.  
  104.   // Automatic control based on power supply
  105.   if (powerAvailable)
  106.   {
  107.     if (!deviceOn && !manualControlActive)
  108.     {
  109.       // Power just supplied
  110.       powerOnTime = currentTime;
  111.     }
  112.  
  113.     // Check if 10 seconds have passed since power detected
  114.     if ((currentTime - powerOnTime >= 10000) && !deviceOn && !manualControlActive)
  115.     {
  116.       // Turn on device automatically
  117.       digitalWrite(deviceControlPin, HIGH);
  118.       deviceOn = true;
  119.       deviceOnTime = currentTime;
  120.     }
  121.  
  122.     // Keep device on for 10 seconds after turned on automatically
  123.     if (deviceOn && !manualControlActive)
  124.     {
  125.       if (currentTime - deviceOnTime >= 10000)
  126.       {
  127.         // Turn off device
  128.         digitalWrite(deviceControlPin, LOW);
  129.         deviceOn = false;
  130.       }
  131.     }
  132.   }
  133.   else
  134.   {
  135.     // Power not available
  136.     // Ensure device is off
  137.     digitalWrite(deviceControlPin, LOW);
  138.     deviceOn = false;
  139.     manualControlActive = false;
  140.   }
  141.  
  142.   // Small delay to debounce and reduce CPU usage
  143.   delay(100);
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment