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: Motion Control
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-07-14 03:08:43
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Generate an ESP32 code to control a 4-channel */
- /* relay and two 12V DC LEDs based on PIR motion */
- /* detection, including timers for inactivity and */
- /* motion detection, using only connected components. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- // No external libraries are required for basic GPIO control on ESP32
- /****** PIN DEFINITIONS *****/
- const int relayPins[4] = {21, 22, 23, 24}; // GPIO pins for 4-channel relay
- const int ledPins[2] = {25, 26}; // GPIO pins for two 12V LEDs (via transistor or driver)
- const int pirPin = 27; // GPIO pin connected to PIR motion sensor
- /****** SYSTEM TIMERS AND STATES *****/
- unsigned long lastMotionTime = 0;
- unsigned long motionTimeout = 300000; // 5 minutes inactivity timeout (in milliseconds)
- bool motionDetected = false;
- /****** FUNCTION PROTOTYPES *****/
- void initializePins();
- void checkMotion();
- void controlRelaysAndLEDs(bool state);
- void setup(void)
- {
- // Initialize serial communication for debugging
- Serial.begin(115200);
- // Initialize pins
- initializePins();
- // Initialize PIR sensor input
- pinMode(pirPin, INPUT);
- // Set relays and LEDs to OFF initially
- controlRelaysAndLEDs(false);
- }
- void loop(void)
- {
- checkMotion();
- unsigned long currentTime = millis();
- // If motion detected, turn on relays and LEDs
- if (motionDetected)
- {
- controlRelaysAndLEDs(true);
- lastMotionTime = currentTime; // Reset inactivity timer
- }
- else
- {
- // Check if inactivity timeout has elapsed
- if (currentTime - lastMotionTime > motionTimeout)
- {
- // Turn off relays and LEDs after timeout
- controlRelaysAndLEDs(false);
- }
- }
- }
- // Initialize relay and LED pins
- void initializePins()
- {
- for (int i = 0; i < 4; i++)
- {
- pinMode(relayPins[i], OUTPUT);
- digitalWrite(relayPins[i], LOW); // Ensure relays are off
- }
- for (int i = 0; i < 2; i++)
- {
- pinMode(ledPins[i], OUTPUT);
- digitalWrite(ledPins[i], LOW); // Ensure LEDs are off
- }
- }
- // Check PIR sensor for motion detection
- void checkMotion()
- {
- int pirState = digitalRead(pirPin);
- if (pirState == HIGH)
- {
- if (!motionDetected)
- {
- Serial.println("Motion detected!");
- }
- motionDetected = true;
- }
- else
- {
- // No motion detected
- if (motionDetected)
- {
- Serial.println("Motion ended.");
- }
- motionDetected = false;
- }
- }
- // Control relays and LEDs based on motion detection
- void controlRelaysAndLEDs(bool state)
- {
- for (int i = 0; i < 4; i++)
- {
- digitalWrite(relayPins[i], state ? HIGH : LOW);
- }
- for (int i = 0; i < 2; i++)
- {
- digitalWrite(ledPins[i], state ? HIGH : LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment