0

As the title says i'm having a problem with an if clause evaluating incorrectly in one particular function

uint16_t    getSleepTime()
{
    volatile uint8_t flag = EEPROM.read(3);

    uint8_t high    = EEPROM.read(0);
    uint8_t low     = EEPROM.read(1);

    if(flag != 0xaa);
    {
        Serial.println(flag);
        Serial.println("ERROR: SLEEP TIME NOT DEFINED, CANNOT CONTINUE.");
        while(true)
        {
            digitalWrite(PIN_LED, true);
            delay(250);
            digitalWrite(PIN_LED, false);
            delay(250);
            wdt_reset();
        }
    }

    return word(high, low);
}

the result is that the if function evaluates as true always my first thought was, ok the eeprom wasn't written properly, hence i added the println, the returned value was 170 == 0xaa then i thought well maybe the flag variable is optimized out, hence the volatile and then, well maybe its a type comparison issue so i also defined a const uint8_t flagv with value 0xaa same results in every case

13:21:00.991 -> 170
13:21:00.991 -> ERROR: SLEEP TIME NOT DEFINED, CANNOT CONTINUE.
and the corresponding blink

the thing is that being such a trivial code i'm out of ideas right now, as anyone here experienced something like this?

1
  • when debugging, put Serial.println(flag); before the if statement – jsotola Feb 12 '20 at 16:13
2

Do not put a ; after an if:

if(flag != 0xaa);

is the same as

if(flag != 0xaa)
{
}

Meaning that the code after the if statement is always executed.

2
  • ouch, dumb mistake – diegogmx Feb 12 '20 at 10:34
  • No problem, however, I also have to vote for closing, since it's not related to Arduino. – Michel Keijzers Feb 12 '20 at 10:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.