0
\$\begingroup\$

I do not know why the simple code below does not work. It may have to do with how unsigned long behaves? Code simply determines if elapsed time has exceeded Snooze_Time which is 10000 by comparing the current millis() to the start value of millis().

unsigned long   Start_Time;
unsigned long   Time_Elapsed; 
unsigned long   Snooze_Time;

void setup() 
{
Snooze_Time = 10000;   //Time to start beep
Start_Time = millis();

Serial.begin(250000);
}

void loop() {

    Time_Elapsed = millis() - Start_Time;

      if (Time_Elapsed >> Snooze_Time)
      {
          Serial.println("ELAPSED >> SNOOZE");
          Serial.print("Time_Elapsed:  ");
          Serial.println(Time_Elapsed);
          Serial.print("Snooze_Time:  ");
          Serial.println(Snooze_Time);
          Serial.println();
      }

      if(Time_Elapsed << Snooze_Time)
      {
        Serial.println("ELAPSED << SNOOZE");
        Serial.print("Time_Elapsed:  ");
        Serial.println(Time_Elapsed);
        Serial.print("Snooze_Time:  ");
        Serial.println(Snooze_Time);
        Serial.println();
      }
  }

The early output from the Serial.prints is this, which appears correct.

enter image description here

Then once Time_Elapsed becomes greater than 65533, far greater than 10000, for no apparent reason both if statements pass for Time_Elapsed << 10000 AND Time_Elapsed >> 10000.

If fact, 65539 passes both if conditions

enter image description here

Any help is appreciated.

\$\endgroup\$
2
  • \$\begingroup\$ What Chris says. || Note that unsigned long is 32 bits. | 2^16 = 65536. So you appear to be getting a 16 bit roll over. For reasons which Chris notes. \$\endgroup\$
    – Russell McMahon
    Commented May 3, 2020 at 21:11
  • \$\begingroup\$ your program ignores Time_Elapsed == Snooze_Time ... is that a desired behavior? \$\endgroup\$
    – jsotola
    Commented May 4, 2020 at 3:09

1 Answer 1

5
\$\begingroup\$
if (Time_Elapsed >> Snooze_Time)

In C-style languages, >> and << are bit-shift operators (C++ which Arduino technically is adds some excessively clever overloading for additional uses, but that's not relevant here)

The comparison operators are <, >, etc.

On traditional platform's Arduino's millis() overflows after around 50 days, though there are some 3rd party platforms where it happens much, much faster. The tie-in to 65536 would be related to the size of an int on typical ATmega compilers, to the extant that your program changes there it only a curious result of the fact that you accidentally used an entirely inappropriate operator for your test.

There are existing questions on Arduino SE which discuss how to perform overflow safe timing.

\$\endgroup\$
1
  • \$\begingroup\$ Ahh.... the world makes sense again! Thank you! \$\endgroup\$
    – BlueSky
    Commented May 3, 2020 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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