I have an application that can only put a distance moved on a 8 bit value. the position is sampled at a given poll rate and for obvious reasons the slower the poll rate is the more problematic or "less effective" it is. The application will sample the value and store an old value. then on the next consecutive read it subtracts, hence distance moved.
Issue one, if the last point was 250 and the distance traveled was 15, it will calculate a large value in the wrong direction.
250 - (250 + 15). which means 250 - 5 because the byte wraps. the immediate way of fixing this was to not calculate a large distance. So in this case its 245, so if the abs value > 200, we ignore that read. This works for a poll rate of 4ms. but if the poll rate is slower like 16ms, then the distance traveled could hit 200. So now we have this, 250 - (250 + 200) which means 250 - 190 making the value 60 but in the wrong direction. This will not trigger the > 200 so it moves the wrong way.
So I'm wondering if there is a convention for this sort of thing I'm unaware of. I think the if value greater than 200 is more of a hack. Maybe there is some simple way to deal with this.
A simpler more direction question would be to ask, if you have data that can move left or right represented in a integer. how do you handle the byte roll over.