Animated Staircase - run "no motion" preset

Finally got this usermod working with HC-SR501 sensors! I was wondering if there is a way to modify the code so that when there is no motion detected it runs a specific preset?

Basically, when motion is detected, it runs the usermod as intended. When no motion is detected, the power is turned back on and shows a specific present I create (a few pixels on the end of each strip always on so that the stairs get some light even when there is no motions detected). Thanks!

Donā€™t you already have an ā€œOffā€ or ā€œRun in reverseā€ or ā€œEnd of activityā€ preset?

A preset can be a playlist so you should be able to tack your desired ā€œminimum lightsā€ preset onto the end of the standard "End of Activity preset in a playlist.

You can achieve this by modifying the usermod code for the HC-SR501 sensor to include a check for motion detection and set a specific preset when no motion is detected. Hereā€™s a general approach to do this:

  1. Identify Motion Detection: Ensure your usermod has a way to check when motion is detected.

  2. Set Preset on No Motion: Add logic to run a specific preset when no motion is detected for a certain amount of time.

Hereā€™s a simplified example of how you might modify your usermod code:

#include "wled.h"

// Your usermod class
class MotionSensorMod : public Usermod {
private:
    int sensorPin = 12; // Example GPIO pin connected to the HC-SR501
    unsigned long lastMotionTime = 0;
    unsigned long motionDelay = 5000; // Time (in ms) to wait before running preset
    bool motionDetected = false;

public:
    void setup() {
        pinMode(sensorPin, INPUT);
    }

    void loop() {
        // Read motion sensor
        motionDetected = digitalRead(sensorPin);
        
        if (motionDetected) {
            lastMotionTime = millis();
            // Run your intended usermod functionality here
            // For example: ws2812fx.start(0); // Starting effects
        } else {
            // Check if enough time has passed without motion
            if (millis() - lastMotionTime > motionDelay) {
                // Set specific preset here
                // For example: setPreset(1); // Assuming preset 1 is your desired state
                // Alternatively: run your own function to set the lights as needed
                setLightsForNoMotion();
            }
        }
    }

    void setLightsForNoMotion() {
        // Set your lights to a specific state or effect
        // Example: Set specific pixels to ON
        for (int i = 0; i < NUM_PIXELS; i++) {
            if (i < 5) { // Assuming the last few pixels should always be on
                strip.setPixelColor(i, strip.Color(255, 255, 255)); // White
            } else {
                strip.setPixelColor(i, strip.Color(0, 0, 0)); // Off
            }
        }
        strip.show();
    }
};

Steps to Implement:

  1. Adjust Pin Number: Make sure to set sensorPin to the actual GPIO pin you are using for the HC-SR501.

  2. Modify Timing: Adjust motionDelay as needed for how long you want to wait before switching to the preset.

  3. Set Your Preset: Replace setLightsForNoMotion() with whatever logic you need to display your specific preset.

  4. Compile and Upload: Once youā€™ve made your changes, compile and upload the modified code to your ESP32/ESP8266.

This should allow your lights to respond to motion and return to a specific preset when no motion is detected. Let me know if you need further adjustments or clarifications!

1 Like

Hey Jack - thanks so much for the assist! Iā€™m currently in the middle of trying to change my sensor to a weatherproof JSN-SR04T since this is going to be installed outside. Got everything hooked up now trying to debug why it isnā€™t triggering. I will try and switch it back this weekend and give your code a try. Iā€™m amazed how helpful people can be on the internet, really appreciate you taking the time to respond so in-depth and offering to help me out :smile:

Be careful with anything ā€˜jackā€™ says. They are a spam bot. Please donā€™t click their links. They have been posing AI like replies to MANY topics in these forums. This is the 3rd time they have done such things.

woah, good thing I wasnā€™t ready to take action yet - thanks Jinx!

Iā€™ve tried ChatGPT myself and never am able to obtain a successful compile with their written code.

1 Like