if statement with embedded millis timer?

Hey all!

I'm looking to make a thermostat warning system... I want the actual temperature to be compared to my thermostat setting, wait 5 minutes, check AGAIN, and then send my warning...

So something like:

if X = True
Then timer runs for 5 Minutes
if X = True Again,
Then Y

void warning(actualTemperature,thermostatSetting,threshold){
  if (actualValue < setValue - threshold) {
    delay(50000);
  } elif (actualValue < setValue - threshold) {
   send.Notification("your temp is too low");
  }
}

Thanks!

Z

Save the millis() value at the time that X is true. Then, each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then check the value of X again and act accordingly if X is true at that time. Note that this will NOT check whether X remains true for the wait period but you never specified that.

Why is millis() so difficult?

I don't think this is quite it... any thoughts?

void warning(actualTemperature,thermostatSetting,interval){
  unsigned long interval=1000;  // the time we need to wait

  unsigned long originalMillis = millis();

  if (actualTemperature < thermostatSetting) {
    newMillis = millis();
  }
  if ((newMillis-originalMillis >= interval) && (actualTemperature < thermostatSetting)) {
    Send.Notification;
  }
  }
}

Where are you calling the warning() function from and what are you expecting it to do ?

In the function you declare and initialise the originalMillis variable to the current value of millis(). Then, a few microseconds later you set newMillis to millis(), so the 2 variables have practically, possibly exactly the same value. Then you compare them to see whether they differ by 1000 which, frankly, they won't.

Using millis() is not difficult. Save the millis() value at the time that the start action happens. Then, each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then act accordingly and maybe save the start time for the next activity. If not, then go round loop() again, perhaps taking other actions and/or reading inputs, but don't block the free running of loop().