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.
- 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 forward_three_seconds.py 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.py from cyberbot import * bot(18).servo_speed(75) # Full speed forward bot(19).servo_speed(-75) sleep(3000) # Wait three seconds bot(18).servo_speed(None) #Stop bot(19).servo_speed(None)
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 name it backwards_three_seconds.py.
- Change the servo_speed arguments so the cyber:bot will travel backwards for three seconds.
- Save and flash it to the micro:bit.
# backwards_three_seconds.py from cyberbot import * bot(18).servo_speed(-75) # Full speed backwards bot(19).servo_speed(75) sleep(3000) bot(18).servo_speed(None) # Stop bot(19).servo_speed(None)