Difficulties with multiple timers

Hi,

I have some timing troubles with my code.
Despite the fact that I’ve read the article from Robin2 about doing multiple things at the same time.

In my code I use for 4x the millis() timer, 2 of them are working fine since they just do a ‘on/interval/off/interval’

The problem is with the other 2;
I use a LDR to decider whether it’s day or night outside. However, I want to measure an amount of time to make sure it really is day or night.

So I coded a millis() there as well, which you can see here below. It does not work, and when I read the code it does makes sense why it’s not working.
Whenever mode is changed (to day or night) it will start counting directly. So if I keep it dark for 30s and then shine a light on the LDR, the light pops on directly. It is obvious, but I’m not sure how to make it working.

Actually the millis() in this void should start counting when the sensor is below 850, or in the other case above 850.

At first I had separate names for timers, which also did not help.
Sorry for not sharing the full code, it’s 900 lines. Ive attached it though. The code below here is the specific 2 voids which don’t work. The disabled lines of the else function was also in my test, that did not help either.

void checkSensorDark() {                          //to be made: if debug == true short time

  lightSensor = analogRead (A0);

  if (lightSensor <= 850) {
    if (currentMillis - makingSureItIsDark > 30000)//default: 600000 )
      // check if it really is dark = set to 10 minutes.
      // for debug -- change this to X-seconds
    {
      dark = true;
      light = false;
      changeLampStatus();
      makingSureItIsDark = currentMillis;
    }
  }
  else {
   // makingSureItIsDark = currentMillis;
  }
}

void checkSensorLight() {                           //to be made: if debug == true short time

  lightSensor = analogRead (A0);

  if (lightSensor > 850) {
    if (currentMillis - makingSureItIsLight > 30000)//default: 600000 )
    
      // check if it really is light = set to 10 minutes.
      // for debug -- change this to X-seconds
    {
      dark = false;
      light = true;
      FastLED.clear(true);
      makingSureItIsLight = currentMillis;
    }
  }
    else {
   // makingSureItIsLight = currentMillis;
  }
}

Terraslampv9.ino (22.4 KB)

It looks to me like you need State Change Detection. You want to start a timer when there is a CHANGE from dark to light or light to dark. To do that you will need a global or static variable to hold the previous state.

See: File->Examples->02.Digital->StateChangeDetection

This sort of thing:

const bool debug = true;
void checkSensor()
{
  unsigned long currentMillis = millis();
  static boolean wasDark = false;
  static unsigned long lastLightChangeTime = 0;


  boolean isDark = analogRead (A0) <= 850;


  // Note the time when the lighting changes
  if (isDark != wasDark)
  {
    wasDark = isDark;
    lastLightChangeTime = millis();
  }


  unsigned long INTERVAL = 600000;  // Ten minutes
  if (debug)
    INTERVAL = 30000; // 30 seconds for testing


  // If there was a chnge, wait until there has been no further change for
  // 10 minutes.
  if (lastLightChangeTime != 0 &&
      currentMillis - lastLightChangeTime > INTERVAL)
  {
    // The last change in lighting was INTERVAL ago.
    dark = isDark;
    light = !isDark;
    changeLampStatus();
    lastLightChangeTime = 0; // Done with the timer until next change
  }
}