crullier:
KenF,
Thanks for the explanation. So in my original proposal, the sketch would be waiting for the Millis variable to reach a value and during this time my sketch would be sitting still as in DELAY.
Btw, all of you guys' earlier post helped and I was able to refine my sketch.
You are getting there but not yet.
You -can- watch the clock for later time but some day it will pass 12 and start again.
That will work as long as you never pass the clock limit. Arduino millis() limit is just over 49.7 days. But micros() limit is just over 90 minutes!
What we do is to find the difference between two times up to the clock limit apart.
This lets us run many timed processes off one clock and not care how long the sketch is running.
millis() and micros() return unsigned long variables for time and because they are unsigned, if you use unsigned longs as well then simply subtracting the earlier time from the later time you will always get the positive difference between the two, same as if I look at a 12 hour clock, from 11 to 2 is 3 hours.
unsigned long startMillis, elapsedMillis;
..... later
startMillis = millis();
...... later on
elapsedMillis = millis() - startMillis; // elapsedMillis now holds how much later on.
We also trigger events using if ()
if ( millis() - startMillis >= waitMillis )
{
// do what should be done when time is up, which might include resetting startMillis
}
****** as far as while loop in your case
Arduino has setup() for things that run once and then loop() which goes round and round, every time a bit later than last. Every time, your code may do something so you don't need to do everything in a single pass through loop().
In fact, if your loop() has more than one thing to do like check 2 different things, having one thing that sometimes or always takes long will hold up the other and may make it late checking. :o(
Don't forget that besides time, a common thing to check is the state of a pin. If your loop() is always fast you won't need an interrupt to do that in timely fashion either. Interrupt costs cycles to run too!
You also may have if (Serial.available()) in your loop but serial is slow compared to Arduino so I usually put that last, especially if a human is behind it. They don't notice 100ms most of the time.
For smooth multiple task coding, keep what anything does short. Sometimes this will mean running a task in stages, what is called process states. One variable keeps the state value and when the overall task runs it only does what the current state should. This gives you a very powerful yet simple tool, look around for tutorials on finite state machines to get started.