Story 1: Drive and stopfunctions
The same in Python
def forward():
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO1,
kitronik_simple_servo.ServoDirection.CCW,
50)
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO2,
kitronik_simple_servo.ServoDirection.CW,
50)
def stop_car():
kitronik_simple_servo.servo_stop(kitronik_simple_servo.ServoChoice.SERVO1)
kitronik_simple_servo.servo_stop(kitronik_simple_servo.ServoChoice.SERVO2)
def on_button_pressed_a():
basic.show_arrow(ArrowNames.NORTH)
forward()
basic.pause(2000)
stop_car()
basic.clear_screen()
input.on_button_pressed(Button.A, on_button_pressed_a)
In MakeCode, switch from Blocks to Python at the top of the editor to type it yourself.
Two functions keep the program short: forward makes both wheels roll forwards, stop_car stops them.
Servo 1 turns counter-clockwise and servo 2 clockwise, because the two servos face opposite ways.
Your turn: a function backward on button B: swap both directions.
Story 2: Spin aroundboth wheels the same way
The same in Python
def forward():
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO1,
kitronik_simple_servo.ServoDirection.CCW,
50)
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO2,
kitronik_simple_servo.ServoDirection.CW,
50)
def stop_car():
kitronik_simple_servo.servo_stop(kitronik_simple_servo.ServoChoice.SERVO1)
kitronik_simple_servo.servo_stop(kitronik_simple_servo.ServoChoice.SERVO2)
def spin_right():
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO1,
kitronik_simple_servo.ServoDirection.CCW,
50)
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO2,
kitronik_simple_servo.ServoDirection.CCW,
50)
def on_button_pressed_a():
basic.show_arrow(ArrowNames.NORTH)
forward()
basic.pause(2000)
stop_car()
basic.clear_screen()
input.on_button_pressed(Button.A, on_button_pressed_a)
def on_button_pressed_b():
basic.show_arrow(ArrowNames.EAST)
spin_right()
basic.pause(1000)
stop_car()
basic.clear_screen()
input.on_button_pressed(Button.B, on_button_pressed_b)
In MakeCode, switch from Blocks to Python at the top of the editor to type it yourself.
Both servos turn the same direction: one wheel rolls forward, the other backward, and the car spins on the spot.
Your turn: a function spin_left. How long must the pause be for exactly a quarter turn?
Story 3: Drive a squarea loop
The same in Python
def forward():
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO1,
kitronik_simple_servo.ServoDirection.CCW,
50)
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO2,
kitronik_simple_servo.ServoDirection.CW,
50)
def stop_car():
kitronik_simple_servo.servo_stop(kitronik_simple_servo.ServoChoice.SERVO1)
kitronik_simple_servo.servo_stop(kitronik_simple_servo.ServoChoice.SERVO2)
def spin_right():
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO1,
kitronik_simple_servo.ServoDirection.CCW,
50)
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO2,
kitronik_simple_servo.ServoDirection.CCW,
50)
def on_button_pressed_ab():
for index in range(4):
forward()
basic.pause(1500)
spin_right()
basic.pause(700)
stop_car()
input.on_button_pressed(Button.AB, on_button_pressed_ab)
In MakeCode, switch from Blocks to Python at the top of the editor to type it yourself.
Press A and B together. The loop repeats a side and a quarter turn four times. Change 700 until every corner is a
right angle: each floor and each set of batteries is a little different.
Your turn: a triangle: three sides, and each turn one third of a full turn.
Story 4: Steer with a remoteradio
The car
The same in Python
def forward():
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO1,
kitronik_simple_servo.ServoDirection.CCW,
50)
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO2,
kitronik_simple_servo.ServoDirection.CW,
50)
def stop_car():
kitronik_simple_servo.servo_stop(kitronik_simple_servo.ServoChoice.SERVO1)
kitronik_simple_servo.servo_stop(kitronik_simple_servo.ServoChoice.SERVO2)
def spin_right():
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO1,
kitronik_simple_servo.ServoDirection.CCW,
50)
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO2,
kitronik_simple_servo.ServoDirection.CCW,
50)
def spin_left():
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO1,
kitronik_simple_servo.ServoDirection.CW,
50)
kitronik_simple_servo.servo_run_percentage(kitronik_simple_servo.ServoChoice.SERVO2,
kitronik_simple_servo.ServoDirection.CW,
50)
radio.set_group(7)
def on_received_number(receivedNumber):
if receivedNumber == 1:
forward()
elif receivedNumber == 2:
spin_right()
elif receivedNumber == 3:
spin_left()
else:
stop_car()
radio.on_received_number(on_received_number)
The remote: a second Micro:bit
The same in Python
radio.set_group(7)
def on_forever():
if input.rotation(Rotation.PITCH) < -30:
radio.send_number(1)
basic.show_arrow(ArrowNames.NORTH)
elif input.rotation(Rotation.ROLL) > 30:
radio.send_number(2)
basic.show_arrow(ArrowNames.EAST)
elif input.rotation(Rotation.ROLL) < -30:
radio.send_number(3)
basic.show_arrow(ArrowNames.WEST)
else:
radio.send_number(0)
basic.clear_screen()
basic.pause(100)
basic.forever(on_forever)
Tilt the remote forward: it sends 1 and the car drives. Tilt right or left: 2 or 3, the car spins. Flat: 0, the
car stops. Both use radio group 7: in a class, every pair picks its own group number.
Your turn: tilt back to drive backwards (send 4).
Really stuck? Try first: you learn the most by building it yourself. If it still does not work, your
teacher can give you a code for a ready-made program of your story.
Type the code in the lock at the top right of the page.