LEDS keep blinking

I use a BH1750 light sensor, HC-SR501 PIR Motion Sensor, a KY-040 Rotary encoder with a Wemos D1 mini (esp8266 wifi chip) board to control some LEDs. By default, they turn on an off automatically (or rather with the PIR sensor) and their brightness is calculated based on the Light Sensor’s readings. Clicking the Rotary Encoder switches it to manual mode where I control the brightness manually. The LEDs fade in and out as they turn on and off and their brightness depends on the ambient light.

I use Blynk to monitor the following:

  • Light levels from the BH1750 sensor
  • motion detected
  • Rotary Encoder is on “Manual Mode”
  • brightness level whilst in “manual mode”

Apart from the light levels and brightness values, all these states are Bool values, I assumed it would be pretty straightforward to set up considering it’s either true or false, but apparently this isn’t the case.

Currently, the only things I can monitor is the light level and whether or not Manual Mode is enabled. Anything else seems to cause the fade-in function to slow down dramatically. I solved this by using BlynkTimer and creating functions to specifically check for the Bool states, but while this fixes the fade issues, the LEDs flicker every second.

To do this, I’ve staggered the timers like so:

timer.setTimeout(100, []() {
    timer.setInterval(1000L, luxMonitoring);
  });
  timer.setTimeout(200, []() {
    timer.setInterval(1000L, motionMonitoring);
  });

…and so on.

I’ve tried eliminating issues and found that it was both “Motion Detected” and the Brightness levels that causes it. I’m not sure it’s the actual functions that are causing the issue or the fact that I’m trying to monitor so many things. I tried putting it all into a single function to eliminate the fact that I was staggering the timers, but to no avail.

Has anyone else had similar issues?

That is a very complicated and resource heavy way to stagger timers :open_mouth:
This will do the same thing with less code and processing…

timer.setInterval(1000L, luxMonitoring);
timer.setInterval(1100L, motionMonitoring);

Or (and perhaps better if always predictable start of timing is required)…

timer.setInterval(1000L, luxMonitoring);
delay(100);
timer.setInterval(1000L, motionMonitoring);

As for any other issues that might be the cause… impossible to say without seeing full code (properly formatted for this forums viewing of course)

This will do the same thing with less code and processing…

timer.setInterval(1000L, luxMonitoring);
timer.setInterval(1100L, motionMonitoring);

I’ve tried this in the past with the same results unfortunately. I opted for the staggering method used above as I saw it somewhere on here and figured I’d give it a try. I’ve now gone back to the method you’ve recommended.

For Reference, this is the luxMonitoring() function:

void luxMonitoring()
{
  Blynk.virtualWrite(V0, lux);
  Blynk.virtualWrite(V4, brightness); // Causes Flashing
  motionDetect(); // Causes Flashing
}

Both the 2 functions I’ve pointed out here seem to cause the flashing. It doesn’t matter if I disable either one, but if even 1 of them is included, the flashing starts to happen.

Here’s the code for the motionDetect() function:

void motionDetect()
{
  if (digitalRead(D0) == 1)
  {
    Blynk.virtualWrite(V2, 255); 
  }
  else
  {
    Blynk.virtualWrite(V2, 0);
  }
}

I thought that the light sensor was clashing with the timer as it checks every second, so I’ve offset that as well, but with no luck. Also for reference, I’m using FastLED and this code to fade the lights in and out:

void fade(int val)
{
  int targetBrightness = val;
  if (targetBrightness > brightness)
  {
    brightness++;
  }
  else if (targetBrightness < brightness)
  {
    brightness--;
    if (brightness < 0)
      brightness = 0;
  }

  fill_solid(leds, NUM_LEDS, CHSV(32, 178, brightness));
  FastLED.show();
}

Just thought I’d add that in case there was something here that I’ve missed.

Maybe if, instead of posting snippets of code and minicam details, you posted your full code with full details of what types of widgets are connected to each virtual pin and how your additional hardware is wired then we’d be able to assist.

Pete.

I wanted to do this at the start, but assumed it would be a lot to go through. Regardless, here’s all the messy code I’m in the process of cleaning up right now.

For starters, you need to sort-out your void loop. Read this:

Pete.