I have written the following code in Python to run a microbit when attached to an RC car, I’m looking for anyways to optimise this code to navigate the track faster.
left = 0
right = 0
difference = 0
def on_forever():
global left, right, difference
left = Kitronik_Move_Motor.read_sensor(Kitronik_Move_Motor.LfSensor.LEFT)
right = Kitronik_Move_Motor.read_sensor(Kitronik_Move_Motor.LfSensor.RIGHT)
difference = left - right
if difference > 10:
if left > right:
Kitronik_Move_Motor.motor_off(Kitronik_Move_Motor.Motors.MOTOR_RIGHT)
Kitronik_Move_Motor.motor_on(Kitronik_Move_Motor.Motors.MOTOR_LEFT,
Kitronik_Move_Motor.MotorDirection.FORWARD,
30)
else:
Kitronik_Move_Motor.motor_off(Kitronik_Move_Motor.Motors.MOTOR_LEFT)
Kitronik_Move_Motor.motor_on(Kitronik_Move_Motor.Motors.MOTOR_RIGHT,
Kitronik_Move_Motor.MotorDirection.FORWARD,
30)
else:
Kitronik_Move_Motor.move(Kitronik_Move_Motor.DriveDirections.FORWARD, 30)
basic.forever(on_forever)
As is setup in the code the “track” is a black line drawn onto a large sheet of paper, and using the sensors on the bottom it calculates the difference between the two sides of the car to readjust course, but it still moves very slowly and I was hoping there was a way to improve the code to run it faster.
I’ve attempted to simply just increase the speed but that results in the RC Car flying off the track.
Andrew Hogan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1