# variables for state var state[4] = [0,0,0,0] var new_state[4] = [0,0,0,0] # stop timer 0 timer.period[0] = 0 # reset outputs call sound.system(-1) call leds.top(0,0,0) call leds.bottom.left(0,0,0) call leds.bottom.right(0,0,0) call leds.circle(0,0,0,0,0,0,0,0) # subroutine to display the current state sub display_state call leds.circle(0,state[1]*32,0,state[3]*32,0,state[2]*32,0,state[0]*32) onevent prox when prox.ground.delta[0] <= 400 and prox.ground.delta[1] <= 400 do if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then new_state[0] = 0 new_state[1] = 1 new_state[2] = 0 new_state[3] = 0 call leds.top(32,0,0) call leds.bottom.left(0,32,0) call leds.bottom.right(0,32,0) emit pair_run 0 end end when prox.horizontal[3] >= 2000 do if state[0] == 0 and state[1] == 1 and state[2] == 0 and state[3] == 0 then new_state[0] = 0 new_state[1] = 0 new_state[2] = 0 new_state[3] = 1 call leds.top(0,32,0) emit pair_run 1 end end when prox.horizontal[0] >= 4000 and prox.horizontal[4] >= 4000 do if state[0] == 1 and state[1] == 0 and state[2] == 0 and state[3] == 0 then motor.left.target = 0 motor.right.target = 0 new_state[0] = 0 new_state[1] = 0 new_state[2] = 0 new_state[3] = 0 emit pair_run 5 end end call math.copy(state, new_state) callsub display_state onevent buttons if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 1 then motor.left.target = -250 motor.right.target = -250 timer.period[0] = 1000 new_state[0] = 0 new_state[1] = 1 new_state[2] = 0 new_state[3] = 1 emit pair_run 2 end if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then call leds.top(32,0,0) call leds.bottom.left(32,0,0) call leds.bottom.right(32,0,0) emit pair_run 6 end call math.copy(state, new_state) callsub display_state onevent timer0 timer.period[0] = 0 if state[0] == 0 and state[1] == 1 and state[2] == 0 and state[3] == 1 then motor.left.target = 0 motor.right.target = 0 new_state[0] = 0 new_state[1] = 0 new_state[2] = 1 new_state[3] = 0 timer.period[0] = 4000 emit pair_run 3 end if state[0] == 0 and state[1] == 0 and state[2] == 1 and state[3] == 0 then motor.left.target = 100 motor.right.target = 100 new_state[0] = 1 new_state[1] = 0 new_state[2] = 0 new_state[3] = 0 emit pair_run 4 end call math.copy(state, new_state) callsub display_state