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:
Identify Motion Detection: Ensure your usermod has a way to check when motion is detected.
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:
Adjust Pin Number: Make sure to set sensorPin to the actual GPIO pin you are using for the HC-SR501.
Modify Timing: Adjust motionDelay as needed for how long you want to wait before switching to the preset.
Set Your Preset: Replace setLightsForNoMotion() with whatever logic you need to display your specific preset.
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!
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
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.