« Ingredients Fail | Newest entries | Show recent e-mail in screen title bar or status bar » I wrote this code for my red alert box and tested it using a shorter timeout than 60 seconds--it worked fine with a 10-second timeout. Just today I realized that the 60s timeout wasn't working; even after several minutes without communication, the globe was still showing green. The cause of the bug is left to the reader, but a hint is available if you click "more inside".
void loop()
{
unsigned long now = millis(); // time since boot in milliseconds
if(Serial.available()) {
level = Serial.read() % 4;
last_message = now;
} else if(now - last_message > 60*1000 && level < 3) {
// if no message received after 60*1000 ms, increase alert level
last_message = now; // well, sorta
level ++;
}
if(level != oldlevel) {
set_led_color(level);
oldlevel = level;
}
delay(50);
}
Hint: sizeof(int) == 2 on avr
|
| [æ] |