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().

So origninalMillis is the time right now and newMillis is the time recorded last time the temperature was below the setpoint. Those names are confusing, but let’s go with them.

So newMillis will always be smaller (or the same) as originalMillis. So does this line make sense? Or have we got something backwards here?

if ((newMillis-originalMillis >= interval) && (actualTemperature < thermostatSetting)) {

Also, you don’t need the part after the &&. You just checked the actualTemperature a few lines ago and if it was lower than the setpoint then you set newMillis to the same thing as originalMillis. So if that second part were ever false, then the first part would have to be false as well.

Instead of originalMillis and newMillis, perhaps this would be easier to read through if they were named something like millisRightNow and millisLastTimeTempWasLow. That might make it a little easier to keep up with which one was which.