// Clock Reader, auto-synchronized // Next bit encoding depends on previous bit color // State encoding : // Present state Encoding Next State // 0 Yellow = 0 2 // 0 Grey = 1 1 // 1 Black = 0 0 // 1 Yellow = 1 2 // 2 Black = 0 0 // 2 Grey = 1 1 // Threshold between black and grey #define BG 42 // Threshold between grey and yellow #define GJ 51 #define Hysteresis 2 #define Center 3 #define Light SENSOR_2 #define CardPresent SENSOR_1 int State; int Value; task main() { SetSensor(Light,SENSOR_LIGHT); SetSensor(CardPresent,SENSOR_TOUCH); while (true) { until (CardPresent==1); OnFwd(OUT_C); GetValue(); } } void GetValue() { Value = 0; State = 0; until (Light < BG - Hysteresis); // First plate is black Wait(Center); repeat (15) { Value *= 2; if (State == 0) { until (Light > BG + Hysteresis); Wait(Center); if (Light > GJ) { State = 2; } else { Value += 1; State = 1; } } else if (State == 1) { until ((Light < BG - Hysteresis)|| (Light > GJ + Hysteresis)); Wait(Center); if (Light < BG) { State = 0; } else { Value += 1; State = 2; } } else if (State == 2) { until (Light < GJ - Hysteresis); Wait(Center); if (Light < BG) { State = 0; } else { Value += 1; State = 1; } } } Wait(50); Float(OUT_C); }