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.
Hardware Setup
- Set the cyber:bot board's power (PWR) switch to Position 0.
- Make sure the battery holder is loaded with 5 AA batteries.
- Make sure the battery holder's barrel plug is firmly plugged into the cyber:bot board's barrel jack.
- Connect your micro:bit module to your computer with a USB cable.
Example Script: forward_three_seconds
- In a Google Chrome or Microsoft Edge browser, go to python.microbit.org to open the micro:bit Python Editor.
- Make sure the cyberbot.py module is added to the Project Files.
(See Add modules to your micro:bit). - Set the project's name to forward_three_seconds, enter the script below, and then click Save.
(See Save & Edit Scripts and Flash Scripts with Python Editor.) - Click Send to micro:bit.
(See Flash Scripts with Python Editor.)
# forward_three_seconds 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)
- Set the power (PWR) switch to position 2.
- Verify that the cyber:bot rolls forward for three seconds.
- Set the PWR switch to 0.
Your Turn – Traveling Backwards
Now, let's try making the cyber:bot travel backwards for three seconds.
- Change the project name from forward_three_seconds to backward_three_seconds.
- Change the servo_speed arguments so the cyber:bot will travel backwards for three seconds.
- Save the modified script and flash it with the Send to micro:bit button.
# backward_three_seconds 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)
- Set the PWR switch to 2.
- Verify that the cyber:bot rolls backwards for 3 seconds.
- Set the PWR switch to 0.