var factor var product var interval var running var speed var state var result var i var l[8] var p1 var p0 var counter var digit var code var steer var status #onevent startup factor = 15 interval = 5 running = 0 call sd.open(1000, status) if status==0 then call sd.read(product, status) call sd.open(-1, status) else product = 733 end sub display for i in 0:7 do if i == result then l[i] = 32 else l[i] = 0 end end call leds.circle(l[0],l[1],l[2],l[3],l[4],l[5],l[6],l[7]) sub calibrate if p0 < 512 then counter = counter + 1 result = counter % 8 else product = (product * (counter + interval / 2)) / (CALIBLEN * CALIBINTERVAL) call sd.open(1000, status) if status == 0 then call sd.write(product, status) call sd.open(-1,status) end running = 0 motor.left.target = 0 motor.right.target = 0 end sub read counter = counter + 1 if counter % interval == 0 then if digit < WORDLEN then if p0 > 512 then code = code + (1 << digit) end digit = digit + 1 else state = S_WAIT_SYNC result = code end end sub drive steer = (factor * (p1 - 512)) / 50 motor.left.target = speed + steer motor.right.target = speed - steer sub sync if p0 < 512 then state = S_READING counter = -interval / 2 digit = -1 code = 0 end onevent buttons when button.forward == 1 do running = R_RUNNING speed = product / interval state = S_WAIT_SYNC result = -1 callsub display end when button.center == 1 do running = R_STOP motor.left.target = 0 motor.right.target = 0 end when button.backward == 1 do running = R_CALIBRATING speed = product / CALIBINTERVAL state = S_WAIT_SYNC result = -1 callsub display end if running == R_STOP then when button.left == 1 do if interval < 7 then interval = interval + 2 end end when button.right == 1 do if interval > 3 then interval = interval - 2 end end result = 2 - (interval - 3) / 2 callsub display end onevent prox if running != R_STOP then p1 = prox.ground.delta[1] callsub drive p0 = prox.ground.delta[0] if state == S_WAIT_SYNC then callsub sync else if running == R_RUNNING then callsub read if state == S_WAIT_SYNC then callsub display end else callsub calibrate callsub display end end end