millis() and while loop

Good day

I need some advice on using the mills function with while loops. I would like to execute some code section for a specified amount of time, eventually serving as time wasting instead of the delay() function.

I noticed that the timer gets stuck in the while loop, and stops counting. The timer value stays zero in the while loop, and therefor the while loop never meets the exit criteria.

Any advice would be highly appreciated.

Thanks

unsigned long time;

void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Time: ");
time = millis();

while(time<1000)
{
Serial.println(time); //prints time since program started
delay(10); // wait a second so as not to send massive amounts of data
}
}

There's nothing inside your while loop that changes the variable called time.

So once you get inside the while loop, you're stuck there for ever.

I would like to execute some code section for a specified amount of time, eventually serving as time wasting instead of the delay() function.

Sounds pointless. Why? What are you really trying to do?

the typical loop you are looking for is BlinkWithoutDelay.

there is a system time millis() that counts in microseconds.

you can note that time

then = millis() ;
duration = millis() - then ;

but, that would just refresh each loop. and you want to create a delay anyway.

lets use a value.

int period = 1000 ; put before setup.
int then, duration ;

in loop() put this

duration = millis() - then ;

if (duration >= period )
{
// do that thing you want to do ;
then = millis(); // starts the timer all over again
}

just change the period to whatever you need.
as a note, this is only for very short timing periods.
for longer periods, you would want to use unsigned long and not int

blink without delay has many-many variations. this is just a simple one.

duration = millis() - then ;
if (duration >= period ) {
// do that thing you want to do
then = millis(); // starts the timer all over again
count++ ; // increments a counter each period
}

if (count >= 60) {
// read temperature
count = 0 ;
}

this sequence does a thing each second ( whatever your period is)
then increments count
and after 60 seconds, it would read the temperature.

same stuff to increment count.... and add count_HR before setup

if (count >= 60) {
// read soil moisture
count = 0 ; // reset
count_HR++ ;
}

if ( count_HR == 24) {
// send daily temperature to web data logger
count_HR = 0 ; // reset
}

And note - you do not use "while" loops in any program that requires multi-tasking - doing more than one thing at a time. And "delay()" is itself, a while loop, so you cannot use that.

All this said, the problem with your program is simply that it is disorganised and you have not thought it out properly. :grinning:

Please read the instructions for posting here.

Paul__B:
you do not use "while" loops in any program that requires multi-tasking - doing more than one thing at a time.

That's not really true, although I understand Paul__B's reason for recommending that as a rule for beginners. You can use while loops, as long as you know they will execute quickly and not hog the CPU. For example, scanning an array for a particular value, or repeating a calculation for a small but unknown number of repetitions. But a while loop with a delay inside is a no-no, as are while loops which repeat until millis() reaches a value that is more than a handful of milliseconds later than it started.

PaulRB:
For example, scanning an array for a particular value, or repeating a calculation for a small but unknown number of repetitions.

I perceive an oxymoron or two there. :grinning: