previousTime is unknown for the millis() function while multitasking.

Hi,

I’m not sure how to google my question and I can’t find the solution I need online. So here’s my problem.

When using millis() to multitask, nearly every tutorial I go through use a variable called previousTime = 0.
My problem is that I do not know when my previousTime will be.

My code is to activate a system when a blockage is detected. But the detected time is unknown until it happens so I can’t set the previousTime = 0.

What I want to do is to store the time from millis() and not let it update after every loop. This is part of my code.

{

void flush_cycle(char block_confirmed) {
  unsigned long current_time = millis();
  if (block_confirmed == 1) {
    if ((current_time - clean_pump_prev) <= 20000) {
      digitalWrite(clean_pump, LOW);
      if ((current_time - solonoid_prev ) >= 2000) {
        digitalWrite(solenoid, !digitalRead(solenoid));
        solonoid_prev = current_time;
      }
    } else {
      digitalWrite(clean_pump, HIGH);
      digitalWrite(solenoid, HIGH);
    }
  }

As you can see, I cannot let my clean_pump_prev = 0, because I do not know when the block is confirmed, because if the block was confirmed at 50000 millis(), then my if statement will never run.

I am trying to store the instance millis() value into clean_pump_prev, but not let it continue to increase as time goes on. Such that my clean_pump_prev = 50000. I do not know how to let the clean_pump_value = 50000 without letting it increase with the time.

Hope my question was clear.

Not completely clear here, perhaps there is a blockage :wink:

When your event is detected: set an enableFlag, make timingMillis = millis(), turn on your control.

if (enableFlag == false && block_confirmed == 1)
{
enableFlag = true;
timingMillis = millis();
digitalWrite(pumpPin, HIGH); //turn on pump
}

if(enableFlag == true && milis() - timingMillis >= myOnTime)
{
enableFlag = false;
digitalWrite(pumpPin, LOW); //turn off pump
}

Sukaki96:
My problem is that I do not know when my previousTime will be.

What I want to do is to store the time from millis() and not let it update after every loop.

Working backwards, when would you want your stored time to update?

Generally use a state change variable to enable updating your stored time variable.
You enable that state variable when your conditions are met, your loop checks if the variable is enabled and executes the update time code. Then unable the state variable and continue with your loop.

larryd:
Not completely clear here, perhaps there is a blockage :wink:

When your event is detected: set an enableFlag, make timingMillis = millis(), turn on your control.

if (enableFlag == false && block_confirmed == 1)
{
enableFlag = true;
timingMillis = millis();
digitalWrite(pumpPin, HIGH); //turn on pump
}

if(enableFlag == true && milis() - timingMillis >= myOnTime)
{
enableFlag = false;
digitalWrite(pumpPin, LOW); //turn off pump
}

Thank you very much! This does what I wanted. Thank you a lot!

You can use the 0 to indicate that the timer has not been started yet:

void flush_cycle(char block_confirmed)
{
  unsigned long current_time = millis();
  if (block_confirmed == 1)
  {
    // If the timer has not been started, start it
    if (clean_pump_prev == 0 )
    {
      clean_pump_prev = currentTime;
      solenoid_prev = currentTime;  // Start the first solenoid cycle
    }
      
    if ((current_time - clean_pump_prev) <= 20000)
    {
      digitalWrite(clean_pump, LOW);
      if ((current_time - solonoid_prev ) >= 2000)
      {
        digitalWrite(solenoid, !digitalRead(solenoid));
        solonoid_prev = current_time;
      }
    }
    else // block cleared
    {
      clean_pump_prev = 0;
      digitalWrite(clean_pump, HIGH);
      digitalWrite(solenoid, HIGH);
    }
  }