I'm waiting some time for a real-world event (e.g. push a button for 3 seconds) on an AVR or STM32 MCU, and I have trouble with code like:
#define PRESS_BUTTON
int waiting = 0;
int t_ms = 0; // time counter
//...
int main(void)
{
while(1)
{
waiting = t_ms + 3000; // waiting button 3 sec
while ((t_ms < waiting) && (!PRESS_BUTTON)) // infinite loop
{}
printf("out"); // not printed
waiting = t_ms = 0;
}
}
ISR( TIMER0_OVF_vect ) // timer interrupt
{
t_ms++;
}
But if I add a printf() inside the while loop, it works!
The same thing happens if I use a do...while loop either. What is causing this?
volatilekeyword. Change tovolatile int t_ms = 0;- LPsvolatile, how do you de-bounce the button? This needs to be done in either software or hardware, otherwise the program will always behave poorly. - Lundinvolatileproperly needed on stackoverflow. Good question. - Dellowarvolatile, but could also be the missing'\n'in theprintfthat could well use a buffer flush to actually print something. "It works when I add aprintf" hints to that. - tofro