pleasedontcode

Motion Control rev_02

Jul 13th, 2025
143
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: Motion Control
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-07-14 03:13:38
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Generate an ESP32 code to control a 4-channel */
  21.     /* relay and two 12V DC LEDs based on PIR motion */
  22.     /* detection, including timers for inactivity and */
  23.     /* motion detection, using only connected components. */
  24. /****** SYSTEM REQUIREMENT 2 *****/
  25.     /* generate a code for ESP32 microcontroller light */
  26.     /* automation using esp32,4channel relay,12v dc */
  27.     /* led,pir sensor such that:  the led turns on or off */
  28.     /* ehen motion is detected */
  29. /****** END SYSTEM REQUIREMENTS *****/
  30.  
  31. /* START CODE */
  32. /****** DEFINITION OF LIBRARIES *****/
  33. // No external libraries are required for this implementation.
  34.  
  35. /****** PIN DEFINITIONS *****/
  36. const int relayPins[4] = {14, 27, 26, 25}; // GPIO pins connected to relay channels
  37. const int ledPins[2] = {16, 17}; // GPIO pins connected to the 12V LEDs (via transistor or driver)
  38. const int pirSensorPin = 13; // GPIO pin connected to PIR sensor
  39.  
  40. /****** SYSTEM TIMERS AND STATES *****/
  41. unsigned long lastMotionTime = 0;
  42. const unsigned long inactivityTimeout = 300000; // 5 minutes in milliseconds
  43. bool motionDetected = false;
  44.  
  45. /****** FUNCTION PROTOTYPES *****/
  46. void setup(void);
  47. void loop(void);
  48. void initializePins(void);
  49. void handleMotionDetection();
  50. void turnOnRelaysAndLEDs();
  51. void turnOffRelaysAndLEDs();
  52.  
  53. void setup(void)
  54. {
  55.     initializePins();
  56.     // Initialize relay and LED states to OFF
  57.     turnOffRelaysAndLEDs();
  58.     // Initialize PIR sensor pin
  59.     pinMode(pirSensorPin, INPUT);
  60. }
  61.  
  62. void loop(void)
  63. {
  64.     handleMotionDetection();
  65.  
  66.     // Check for inactivity timeout
  67.     if (motionDetected && (millis() - lastMotionTime > inactivityTimeout))
  68.     {
  69.         // No motion detected for timeout period, turn off relays and LEDs
  70.         turnOffRelaysAndLEDs();
  71.         motionDetected = false;
  72.     }
  73. }
  74.  
  75. void initializePins(void)
  76. {
  77.     // Set relay pins as OUTPUT
  78.     for (int i = 0; i < 4; i++)
  79.     {
  80.         pinMode(relayPins[i], OUTPUT);
  81.     }
  82.     // Set LED pins as OUTPUT
  83.     for (int i = 0; i < 2; i++)
  84.     {
  85.         pinMode(ledPins[i], OUTPUT);
  86.     }
  87. }
  88.  
  89. void handleMotionDetection()
  90. {
  91.     int pirState = digitalRead(pirSensorPin);
  92.     if (pirState == HIGH)
  93.     {
  94.         // Motion detected
  95.         turnOnRelaysAndLEDs();
  96.         lastMotionTime = millis();
  97.         motionDetected = true;
  98.     }
  99. }
  100.  
  101. void turnOnRelaysAndLEDs()
  102. {
  103.     // Turn on all relay channels
  104.     for (int i = 0; i < 4; i++)
  105.     {
  106.         digitalWrite(relayPins[i], HIGH);
  107.     }
  108.     // Turn on LEDs
  109.     for (int i = 0; i < 2; i++)
  110.     {
  111.         digitalWrite(ledPins[i], HIGH);
  112.     }
  113. }
  114.  
  115. void turnOffRelaysAndLEDs()
  116. {
  117.     // Turn off all relay channels
  118.     for (int i = 0; i < 4; i++)
  119.     {
  120.         digitalWrite(relayPins[i], LOW);
  121.     }
  122.     // Turn off LEDs
  123.     for (int i = 0; i < 2; i++)
  124.     {
  125.         digitalWrite(ledPins[i], LOW);
  126.     }
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment