The Emergent Properties of Meat
Blog
About me
Bigger projects: emc2 & AXIS
Friends' pages:
Alex
Bill
Chris
Jon
Michael


Tag cloud
Photostream

« 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

Entry first conceived on 13 March 2009, 1:33 UTC, last modified on 19 March 2009, 16:14 UTC



Powered by the Emergent Properties of Meat. Copyright © 2004-2010 Jeff Epler
[æ]