Hi! I've gotten stuck on this piece of code. I tried to add a screenshot of it (Idk if it worked, I haven't used this forum before).
Basically, in my loop:
if (counter < 5) { /* do something */}
The counter increments correctly to 4, but then switches to -32768 and keeps adding one. Therefore the if statement keeps executing even after 5 loops (Because negative number is less than 5).
I have tried changing the limit from 5 to a variety of different numbers and the behavior is the same every time.
Does anyone know why this behavior is happening and how I can get it to behave as intended?
Post the whole code. There is information in the missing part. Read the forum guidelines to see how to properly post code. Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
The Serial.println can only happen when counter is less than 5. Once counter is 5 or greater nothing will print, but the counter still increments, until the counter (an int data type) rolls over at 32767 to -32768. Then -32768 is less than 5 so it starts printing again adding one each iteration (-32767, -32766, ...) until it gets to (+) 5 and stop printing again.