Using break and millis()

Sorry for another naive question
What is the problem with the following:
digitalWrite(relay, LOW); //signal ON
timestart = millis();
while (digitalRead(RGPR)) { //wait for ON indicator from repeater
if (millis()-timestart > maxtimeon) {
lcd.print( "Time to ON timeout!!");
logFile.println("Time to ON timeout!!");
fail = true;
break;
}
}
I am waiting for a digital input to go low but timing out if it hasn't happened within a certain time, in this case 3 seconds. It appears that "timestart" is not being updated. It aborts after a few cycles,i.e., first time through RGPR goes low in 1.1 seconds, second time through in 1.0 seconds, but third time through in 1.1 seconds and it aborts. It seems to be not timing each individual pass but accumulating the time on consecutive passes????

Missing code tags

And indentation. And code.

Why does timestart need to be updated ?

OK so I am new to the forum. I just cut and paste a fragment of code to describe my problem. How should I pose the question then?

Post your code.
Post a description of how your code behaves and how this falls short of your expectations.

This is just a fragment of my answer.

  digitalWrite(relay, LOW); //signal ON
  timestart = millis();
  boolean looping = false;
  if (digitalRead(RGPR) == HIGH)looping = true;
  while (looping ) { //wait for ON indicator from repeater
    if (millis() - timestart > maxtimeon) {
      lcd.print( "Time to ON timeout!!");
      logFile.println("Time to ON timeout!!");
      fail = true;
      looping = false;
    }
  }

You mean "timestart " does what it should to do. If not this will be aborted immediately.

I changed the code slightly and it now works but I don't understand why.

timestart = millis();
      while (digitalRead(RGPR)) {  //wait for ON indicator from repeater
        timeon = millis() - timestart;
        if (timeon > maxtimeon) {
          lcd.print( "Time to ON timeout!!");
          logFile.println("Time to ON timeout!!");
          fail = true;
          break;
        }
      }

Please see, read, and really understand reply #5

Check out my tutorial on How to write Timers and Delays in Arduino which show how to stop/start/restart timers.
Also look at Multi-tasking in Arduino once you start using timers

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.