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 number of transitions from zero to one or from one to zero in a given time interval yields the speed of the disk. Here are the transitions of the preceding example stream:
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 ...
The getTransitions
method yields the number of transitions that
have been observed since the device was constructed. In the previous code
example, we would expect device.getTransitions
to return
2
.