Before you can code python for your final robotics project you need an IDE that will upload the code for you, unfortunately it's a little clunky to use VS code still. You can download and install Mu on school computers luckily! So download and install it from this link:
Download Link
Once you've run through the installer you can open it up and plug the microbit into your computer. Mu should detect it but if not select mode in the top left then select BBC Microbit in the pop up window:
At this point you can your microbit, but you need some starter code. Let's get the display to show something first, copy and paste this code into your Mu IDE:
#Add this to your editor:
from microbit import *
val1 = Image("09000:""00000:""00000:""00000:""00000:")
val2 = Image("00000:""00000:""00000:""00000:""00090:")
val3 = Image("00000:""00000:""00000:""00000:""00000:")
while True:
display.show(val1)
sleep(500)
display.show(val3)
sleep(500)
display.show(val2)
sleep(500)
display.show(val3)
sleep(500)
Now to send to code to your microbit you need to flash it. Do this by clicking the flash button while the microbit is plugged in. You will see the yellow LED indicator blink then turn solid.
To start the code click REPL on Mu IDE, then press the reset button on the microbit. (it's the black button on the back where the micro usb port is). You should see one LED blink, then another one will blink. This indicates that we have successfully uploaded code to our microbit.
If you got this far congradulations! you've programmed your microbit and are ready to move onto the next section.
In order to get your car to drive you'll first need to teach it to see. The robot's sight is actually two IR (infrared) sensors which detect saturation levels. If you take a digital reading it's either a 0 or a 1. An analogue signal will measure the level more accurately from 0 (white) to 1023(black), but can read different saturations between the two.
Line 2: imports the microbit library so that your python code can communicate with the microbit.
Line 5: a variable to save the readings from our IR sensors.
Line 11: reads the left IR sensor and saves the value in a variable.
Line 13: outputs the left analogue IR signal that was saved
Line 15: delays the loop so it checks the reading once every 0.2 seconds.
NOTE: the microbit can only cycle (loop) at max 100ms or once every 0.1 second
Exercise:
Make a second variable and have it read the analogue signal from the right IR sensor. It's connected to pin2 instead of pin1. Have it output both the left and right readings. If you complete this then you are ready to move onto the next section.
So you have your robot able to see, but not yet able to walk. To do this we need to make the motors run. To make motors run we need to download the library from here:
Download link for driver
Motor_Upper_L method:
Motor_Upper_L(1, 100) -> the first number indicates clockwise (1) or counter clockwise(0), the second determines the signal strength. The max is 255, off is 0.
Line 3: imports the Mecanum car library we need to drive the car.
Line 5: made a new instance of a mecanum car so we can access the motors.
Lines 9-12: sets the motors to forward and at power level 100. Max is 255.
Lines 15-18: sets motors to forward but turns the power level off.
This code has the car run for 1 second then stop for 0.2 second, then starts again. Keep in mind that the motors don't stop on a dime so there will be some forward momentum as the motors are set to 0. There's no breaks on this car.
The ultra sonic sensors are on a platform mounted to the servo, so that the car can turn and "look" different ways. With microbit we can set the angle the servo is looking. There's other (more accurate) ways to tell the servo to turn a set amount but we'll just focus on angle.
sets the servo to 0 degrees, but it is hard to detect where it will look, this may take some time getting used to because the code in KeyesCar prevents it from looking backwards. You may have to test starting positions to get your angles right.
Here's how you get some "vision' with the car, it will beep if the distance is too close. The sensor isn't amazing but it can accurately read about 1 foot or 30 cm from it. Anything beyond that is a little tricky.
I imported music to beep when things get too close.