I have a sketch that makes use of the millisecond clock as a timer (rather than using the delay statement)
my coding was . . .
myTimer = millis() + 60000; //wait one minute
while (myTimer > millis()); // nothing to doi but wait
The sketch acted as though the while statement was not there (there was no delay)
After looking for the problem, i finally changed the while statement as follows . . .
while(myTimer > millis()){};
and it worked.
Should the conpiler have told me of my error?
Should the conpiler have told me of my error?
No. The compiler warns of syntax errors, not logic errors.
Is 'myTimer' an unsigned long, or an int? Edit - yes, the computer can't read your mind.
pcbbc
4
This...
while (myTimer > millis());
Produces exactly the same code as this...
while (myTimer > millis()) { };
So that wasn't your problem.
Most likely, as aarg says, you have mistakenly defined myTimer as int.
- Times should be unsigned long type.
- Ideally you should always subtract times, never add them...
startTime = millis();
while (millis() - startTime < 60000);
If you add times you will get a glitch after 49.7 days.
This code
while(myTimer > millis()){};
does exactly the same thing as delay() so why not use delay() ?
If you want non-blocking timing then have a look at how millis() is used to manage timing without blocking in Several Things at a Time.
And see Using millis() for timing. A beginners guide if you need more explanation.
...R