3. D/A conversion
It's a pitty that you may only interface RCX inputs in an analog way. Deadly for digital devices. But here's the new idea. The PIC should measure the time-delay between the impact of the IR and ultrasonic pulses. The value should be one byte large! The resistor-net combined with the opto-couplers is the easiest way (not the best) to get a D to A conversion AND the desired galvanic separation of the RCX input from the rest of the device. We first tried an eight-resistor-net, but the result was not convincing, because of the difficulty to distinguish the small raw-values from each other. The program-trick here is to send first the low-byte then the high-byte of the time-delay.
At 4MHz the PIC's internal TMR0-timer is incremented every microsecond (at 1:1 prescale)
at a prescale of 1:128 it is incremented only every 0.128 millisecond
x = c * t, where c is the sound-velocity =343 m/sec
so we get a 4.39cm resolution
a TMR0-overflow occurs after 0.128 * 256 = 32.77 msec, which corresponds to a sound-distance x = 343 * 0.03277 = 11.24 m, which is above our maximum distance of a 5 x 5m room: sqrt(2)*5=7m
Here once again the value list:
RAW = 1023 * R / (10000 + R)
The special messages:
These values may differ from one RCX and one receiver to some other due to part tolerance and probably battery level.