Having difficulty with subtration of unsigned long

The "unsigned byte or unsigned int" part is interesting. It saves a few instruction cycles in copying the interval for the compare and the increment.

If you know your interval is in the sub-unsigned byte or sub-unsigned int range, you can save several more instructions by casting the difference term, since the compiler only copies enough of the source data to do the cast-ed arithmetic.

The Wokwi simulation linked below uses a few different schemes. And the F1-"View Compiled Assembly Code" output is cool. I wish the Arduino IDE had that feature.

1 Like