Speedometers often use a spinning disk that has alternating black and white strips such as this one:
A light sensor measures the color, receiving a stream of zeroes (black) and ones (white), such as
0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 ...
Each sensor measurement is communicated by a separate call to the add
method:
SpinningDiskDevice device = new SpinningDiskDevice(); device.add(0); device.add(0); device.add(0); device.add(0); device.add(1); device.add(1); device.add(1); device.add(1); device.add(1); device.add(1); device.add(0); ...
The length of each segment is an indication of the speed: the slower the speed, the longer the segment. In your implementation, measure the length of the current segment. You need to reset the length when the inputs flip from ones to zeroes or zeroes to ones.