Forward and Backward

Have you ever thought about what direction a car’s wheels have to turn to propel it forward?  The wheels turn opposite directions on opposite sides of the car.  Likewise, to make the cyber:bot go forward, its left wheel has to turn counterclockwise, while its right wheel turns clockwise.

Remember that a script can use the servo_speed function to control the speed and direction of each servo.  Then, it can use the sleep function to keep the servos running for certain amounts of time before choosing new speeds and directions.  Here’s an example that will make the cyber:bot roll forward for about three seconds, and then stop.

Example script: forward_three_seconds

  • Make sure the cyber:bot board’s power switch is set to 1 and the battery pack is plugged into the cyber:bot board.
  • Enter, save, and flash the script forward_three_seconds to the micro:bit.
  • Disconnect the programing cable and put the cyber:bot on the floor.
  • Move the switch to position 2 and press the reset button on the micro:bit. The cyber:bot should drive forward for three seconds.
# forward_three_seconds

from cyberbot import *

bot(18).servo_speed(75)             # Full speed forward
sleep(3000)                         # Wait three seconds
bot(18).servo_speed(None)           #Stop

Your Turn – Traveling Backwards

Now, let's try making  the cyber:bot travel backwards for three seconds.

  • Save a copy of the previous example and then rename the script  backward_three_seconds.
  • Change the servo_speed arguments so the cyber:bot will travel backwards for three seconds.
  • Save and flash it to the micro:bit.
# backward_three_seconds
from cyberbot import *
bot(18).servo_speed(-75)            # Full speed backwards
bot(18).servo_speed(None)           # Stop