2

I was looking at the blink without delay sketch and I was trying to modify it so the LED would start blinking after 1000 ms. I changed the previousMillis variable to 1000, but the program didn't change. In fact, it doesn't seem to change for any value of previousMillis. I thought that this would change the if statement:

if (currentMillis - previousMillis >= interval)

because 2000ms would have to pass for currentMillis - previousMillis to be equal to 1000ms (interval). What am I missing?

3
  • If you initialize previousMillis to 1000 then, initially, currentMillis - previousMillis is −1000. But you are dealing with unsigned numbers, which cannot be negative, so the subtraction wraps around and you get 2^32−1000 = 4294966296. – Edgar Bonet Oct 1 '16 at 20:51
  • Thanks, I removed the unsigned from both variables and it works now. However, is there any downside to this? And any better solutions? – tootin' putin Oct 1 '16 at 21:15
  • is there any downside to [removing unsigned]?” Yes. millis() will eventually overflow. Unsigned overflow is well behaved, whereas signed overflow is undefined behavior in C and C++. “any better solutions?” You can set an initial interval and change it later. – Edgar Bonet Oct 2 '16 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.