On 3 March 2015 at 07:59, James Newton wrote: > "Subtracting is NOT adding plus a bit." > > (one of my very favorite double entendres) > > Somewhat related. Met again recently. This is bread & butter stuff to people who play with wrap around buffers but less than intuitive to some others. Imagine a timer that increments with a finite word length and a future time T_then and current time T_now, or a rotary buffer with Add_in and Add_out (Add_in > Add_out when buffer not empty). Then usually Time to go to reach T_then is (T_then-T_now) [depending on implementation a correction of 1 may be needed]. Similarly buffer length =3D Add_in - Add_out When the buffer wraps or the future time is > number accommodated by word length used the buffer "wraps". All very standard. How to deal with the fact that the "high" value address or time is now less than the "low" value can lead to some devious calculations. They are unnecessary. If unsigned integers are used then the calculation (T_then - T_now) always produces the correct result if the overflow / carry is ignored. This is because the subtraction 'acts as if' the high order bit above the current word length is present. May not be intuitive but some playing shows how it works. Some will wonder why I labour this obvious (to some) point. The Arduino system has a millisecond timer millis() which wraps around after a long period (weeks). (Also a microsecond micros() timer with typically a 4 uS granularity). These allow time delays and time management to be handled dynamically without programmed delays. For millis() timer wrap around is not an issue. It's more so for micros() but the microsecond timer is less used. For those who wish systems to run for periods longer than the wrap around period and to work correctly when millis() is used for timing, timer wraparound must be correctly handled. This is the subject of much web discussions, solutions with many lines of code and much confusion. This includes discourses by people who are obviously competent programmers and not reasonable dismissed as just Arduino-ites by Arduino haters. Yet all that is required is an unsigned subtraction of the two values. Even amongst the informed, some very basic stuff is being missed. Russell --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .