pleasedontcode

Motion Control rev_01

Jul 13th, 2025
63
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:08:43
  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. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27. /****** DEFINITION OF LIBRARIES *****/
  28. // No external libraries are required for basic GPIO control on ESP32
  29.  
  30. /****** PIN DEFINITIONS *****/
  31. const int relayPins[4] = {21, 22, 23, 24}; // GPIO pins for 4-channel relay
  32. const int ledPins[2] = {25, 26}; // GPIO pins for two 12V LEDs (via transistor or driver)
  33. const int pirPin = 27; // GPIO pin connected to PIR motion sensor
  34.  
  35. /****** SYSTEM TIMERS AND STATES *****/
  36. unsigned long lastMotionTime = 0;
  37. unsigned long motionTimeout = 300000; // 5 minutes inactivity timeout (in milliseconds)
  38. bool motionDetected = false;
  39.  
  40. /****** FUNCTION PROTOTYPES *****/
  41. void initializePins();
  42. void checkMotion();
  43. void controlRelaysAndLEDs(bool state);
  44.  
  45. void setup(void)
  46. {
  47.   // Initialize serial communication for debugging
  48.   Serial.begin(115200);
  49.   // Initialize pins
  50.   initializePins();
  51.   // Initialize PIR sensor input
  52.   pinMode(pirPin, INPUT);
  53.   // Set relays and LEDs to OFF initially
  54.   controlRelaysAndLEDs(false);
  55. }
  56.  
  57. void loop(void)
  58. {
  59.   checkMotion();
  60.  
  61.   unsigned long currentTime = millis();
  62.  
  63.   // If motion detected, turn on relays and LEDs
  64.   if (motionDetected)
  65.   {
  66.     controlRelaysAndLEDs(true);
  67.     lastMotionTime = currentTime; // Reset inactivity timer
  68.   }
  69.   else
  70.   {
  71.     // Check if inactivity timeout has elapsed
  72.     if (currentTime - lastMotionTime > motionTimeout)
  73.     {
  74.       // Turn off relays and LEDs after timeout
  75.       controlRelaysAndLEDs(false);
  76.     }
  77.   }
  78. }
  79.  
  80. // Initialize relay and LED pins
  81. void initializePins()
  82. {
  83.   for (int i = 0; i < 4; i++)
  84.   {
  85.     pinMode(relayPins[i], OUTPUT);
  86.     digitalWrite(relayPins[i], LOW); // Ensure relays are off
  87.   }
  88.   for (int i = 0; i < 2; i++)
  89.   {
  90.     pinMode(ledPins[i], OUTPUT);
  91.     digitalWrite(ledPins[i], LOW); // Ensure LEDs are off
  92.   }
  93. }
  94.  
  95. // Check PIR sensor for motion detection
  96. void checkMotion()
  97. {
  98.   int pirState = digitalRead(pirPin);
  99.   if (pirState == HIGH)
  100.   {
  101.     if (!motionDetected)
  102.     {
  103.       Serial.println("Motion detected!");
  104.     }
  105.     motionDetected = true;
  106.   }
  107.   else
  108.   {
  109.     // No motion detected
  110.     if (motionDetected)
  111.     {
  112.       Serial.println("Motion ended.");
  113.     }
  114.     motionDetected = false;
  115.   }
  116. }
  117.  
  118. // Control relays and LEDs based on motion detection
  119. void controlRelaysAndLEDs(bool state)
  120. {
  121.   for (int i = 0; i < 4; i++)
  122.   {
  123.     digitalWrite(relayPins[i], state ? HIGH : LOW);
  124.   }
  125.   for (int i = 0; i < 2; i++)
  126.   {
  127.     digitalWrite(ledPins[i], state ? HIGH : LOW);
  128.   }
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment