Simple Code does work different

I have the following code that works unexpected, it returns true always, even if called directly after start_tme was set millis(). platform arduino, processoresp32-s3 devkitc-1

/**
 * @brief checks if a start + wait time greater than millis(), start time was set
 * from millis() some time before
 * @param start_time 
 * @param wait_time
 * @returns TRUE if condition met, FALSE if not
 */
bool my_timer(unsigned long start_time, unsigned long wait_time)
{
    bool erg;
    erg = false;

    Serial.print("millis ");Serial.println(millis());
    Serial.print("start_time ");Serial.println(start_time);
    Serial.print("wait ");Serial.println(wait_time);
    if ((millis()) > (start_time + wait_time));
    {
        erg = true;
    }
    return erg;
} // END my_timer

first I had the following Version.

bool my_timer(unsigned long start_time, unsigned long wait_time)
{
    bool erg;
    erg = false;
    if (millis() > start_time + wait_time)
    {
        erg = true;
    }
    return erg;
} // END my_timer

it worked sometimes and call from an other part in the code, the same returns true even if time not over. wait_time is alwasy 30000

This by the way is exactly what labelled as “the buggy one” in the “Comparing durations” for not handling the millis rollover:

https://arduino.stackexchange.com/a/12588/30175

You are calling millis() twice, with printing in between, so the serial output is not representative of what the output of the function will be.

Try to capture all variables, including the return value, once, and print them out,

in this application overflow is not a theme as it is shure the system is restartet at least once a day. On the other hand I still do unterstand why the first version returned true while the wait time is not reached. I need two none blocking timers so I used millis(), So I have one variable and a constant all unsigned long, for each timer, a start time, that is set with millis() the it is started and the constant with the wait time.
the 50 years I am programming I never had a function doing different things by entering the same values

Oh my god!! It’s right there! The semicolon after the if!

That shouldn’t be there, it’s an empty statement. That’s the same as saying

bool my_timer(unsigned long start_time, unsigned long wait_time)
{
    bool erg;
    erg = false;

    Serial.print("millis ");Serial.println(millis());
    Serial.print("start_time ");Serial.println(start_time);
    Serial.print("wait ");Serial.println(wait_time);
    if ((millis()) > (start_time + wait_time))
       ; // execute empty statement if condition is true
    // scoped block, always executed
    {
        erg = true;
    }
    return erg;
} // END my_timer
1 Like

Thank you so much, I was blind.

Best Regards

Rainer