While loop not working correctly

uptimer=3000;
I have the following code:
while(uptimer){
//dont do anything
Serial.print("uptimer: ");

}

ISR(TIMER2_OVF_vect){

if(uptimer){
uptimer--;
}
}
I am using a radio and I don't need the "Serial.print" in the while loop. BUT, if I don't put it in there the loop gets hung up. I've even tried putting in a bogus variable that it can just add to and it still gets hung up unless I put in the Serial.print....

this uptimer is a timer using TIMER2_OVF_vect. I'm trying to not use the delay command and I can't see using millis because I don't want it to execute the code after the while until the timer has counted down. Any ideas what I doing wrong?

Your code is incomplete.

Please remember to use code tags when posting code

Please read the advice on how to post queries on this forum.

Without seeing all the code it is difficult to work out the problem - but at a guess you've missed volatile.

Please post your entire code. Why can't you use millis()? Do you need sub-millisecond resolution?

countrypaul:
Without seeing all the code it is difficult to work out the problem - but at a guess you've missed volatile.

Also, since uptimer is a multi-byte variable interrupts need to be disabled before reading it or modifying it in loop().

Still not sure why millis() can't be used...

@countrypaul, volatile was the trick. I had the uptimer variable declared as unsigned long. I changed to volatile long and it works. Thanks!

Why not “volatile unsigned long”?

mscientist33:
@countrypaul, volatile was the trick. I had the uptimer variable declared as unsigned long. I changed to volatile long and it works. Thanks!

You still need to disable interrupts while reading or modifying uptimer in loop(). Otherwise you risk reading or writing uptimer while the interrupt is writing it. This will show up as a weird value intermittently.

Okay, I'm only reading in that while loop. The timer is set before I get there so I still need to disable interrupts?

The timer value is 3000, is there a reason I should use volatile unsigned long?

I'm having to do some reading as to what volatile actually does.

mscientist33:
Okay, I'm only reading in that while loop. The timer is set before I get there so I still need to disable interrupts?

The timer value is 3000, is there a reason I should use volatile unsigned long?

I'm having to do some reading as to what volatile actually does.

Yes you must use volatile and you must disable interrupts before reading or writing a multi-byte variable that is shared with an interrupt. This is because the Arduino processor is an 8-bit processor (at least the AVR is) and can't write 32 bits in one cycle. There are other potential problems too. If your while loop doesn't read the value faster than the interrupt cycle you may miss the value being at 0 and not exit the loop after 3000 decrements.

Interrupts are tricky. Why don't you use millis()?

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