var prox2[5] var max var i var t var speed_diff var speed var speed_l sub get_max_and_speed_diff prox2[0] = prox.horizontal[0] prox2[1] = prox.horizontal[1] prox2[2] = prox.horizontal[2] prox2[3] = prox.horizontal[3] prox2[4] = prox.horizontal[4] max = 0 for i in 0:4 do if prox2[i] > max then max = prox2[i] t = i - 2 end end speed_diff = t * (speed / 2) sub get_speed_l if max > 3500 then speed_l = (3500 - max) / 2 end if max > 4000 then speed_l = -speed end if max < 3000 then speed_l = 300 - (max - 1000) / 7 end if max < 2000 then speed_l = speed end if speed_l > speed then speed_l = speed end if speed_l < -speed then speed_l = -speed end onevent buttons when button.forward == 1 do speed = speed + 50 if speed > 500 then speed = 500 end end when button.backward == 1 do speed = speed - 50 if speed < -500 then speed = -500 end end when button.center == 1 do speed = 0 motor.left.target = 0 motor.right.target = 0 end onevent prox callsub get_max_and_speed_diff callsub get_speed_l if max < 500 then motor.left.target = 0 motor.right.target = 0 else motor.left.target = speed_l + speed_diff motor.right.target = speed_l - speed_diff end get_max_and_speed_diff prox 0 0 0 prox.horizontal[0] prox 0 1 0 prox.horizontal[1] prox 0 2 0 prox.horizontal[2] prox 0 3 0 prox.horizontal[3] prox 0 4 0 prox.horizontal[4] max 0 0 i 0 4 TRUE GT 0 prox 0 i 0 max max 0 prox 0 i t 0 - 1 i 1 2 speed_diff 0 * 1 t 1 / 1 speed 1 2 buttons TRUE button.forward speed 0 + 1 speed 1 50 TRUE GT 0 speed 0 500 speed 0 500 TRUE button.backward speed 0 - 1 speed 1 50 TRUE LT 0 speed 0 -500 speed 0 -500 TRUE button.center speed 0 motor.left.target 0 motor.right.target 0 prox TRUE LT 0 max 0 500 motor.left.target 0 motor.right.target 0 motor.left.target 0 + 1 speed_l 1 speed_diff motor.right.target 0 - 1 speed_l 1 speed_diff get_speed_l TRUE GT 0 max 0 3500 speed_l 0 / 1 - 1 3500 1 max 1 2 TRUE GT 0 max 0 4000 speed_l 0 - speed TRUE LT 0 max 0 3000 speed_l 0 - 1 300 1 / 1 - 1 max 1 1000 1 7 TRUE LT 0 max 0 2000 speed_l 0 speed TRUE GT 0 speed_l 0 speed speed_l 0 speed TRUE LT 0 speed_l 0 - speed speed_l 0 - speed prox 5