#define LightThr 40 #define PlateWidth 14 int PulseCount; int Value; task main() { PulseCount = 0; start Count; SetSensor(SENSOR_2,SENSOR_LIGHT); SetSensor(SENSOR_1,SENSOR_TOUCH); GetValue(); // And now, find something interesting to do with Value !!! StopAllTasks(); } task Count() { /* Add 1 to count value at each transition of the touch sensor. Wait(5) is used to debounce switch. I originally intended to use Sensor_Mode_Pulse, but it can't keep up with the speed */ while (true) { until (SENSOR_1 == 0); Wait(5); PulseCount++; until (SENSOR_1 == 1); Wait(5); PulseCount++; } } void GetValue() { Value = 0; OnFwd(OUT_A); until (SENSOR_2 < LightThr); // Wait for first black plate until (SENSOR_2 > LightThr); // Wait for first yellow plate PulseCount = 0; until (PulseCount > PlateWidth*3/2); // Position in the middle of the first bit PulseCount = 0; repeat (14) { Value *= 2; if (SENSOR_2 > LightThr) Value++; // If yellow, bit=1 until (PulseCount > PlateWidth); // Go to next bit PulseCount = 0; } until (PulseCount > 50); // Wait until the card is out of the gears Off(OUT_A); }