If you would like to achieve a Silver CREST Award, the suggestions below will help you to extend your robot project.
Remember to save copies of your work and/or take photographs, to use in your final report.
if-elif-else statements: Raspberry Pi 'Rock Paper Scissors' project.
variables, functions and loops: Raspberry Pi 'Introduction to Python' pathway.
Open the editor to get started.
Connect your micro:bit to your device with the USB cable and wait for a few seconds for it to register the new connection.
Click 'Send to microbit' on the editor, and follow the prompts to connect your micro:bit - the pop-ups include screenshots of what to click to make sure that it works.
If you want to continue working on a program, first check if you saved a .hex (will include any extra files, such as bot.py) or .py (just the main code).
For either file, you can click Open in the bottom right of the editor.
Alternatively:
To make it easier to practice your robot's logic at home, we are going to edit the ultrasonic distance sensor code to use the micro:bit's built-in light level sensor and temperature sensor instead, and replace the drive()
function with messages on the micro:bit's display.
An example program you can download is linked below, or you can see the syntax examples and edit your own version of the ultrasonic drive code that you worked on during the workshop.
while True: light = display.read_light_level() if light <= 10.0: display.scroll('dark') temp = temperature() if temp <= 10.0: display.scroll('cold') else: display.scroll('warm') else: display.scroll('bright')
In this example, the micro:bit checks if it is bright or dark. If it is dark, it then checks if the temperature is warm or cold.
Example program: LightTemp-Basic.py
Edit the program until you are happy with the way it works. You could use other sensors, such as the microphone or accelerometer instead (check the Reference for the syntax).
Test your code on the Emulator, changing the sensor values using the sliders, to check how it behaves with different values. Then download your code and test it in real life.
If your micro:bit is connected to your computer, you can open the terminal to get live information from the micro:bit.
Edit your code to add in a print statement, which tells the micro:bit to print some information to the terminal.
print(light)
Run your code and check that it works.
sleep()
command to slow it down?You can expand Show serial under the Emulator to check how it works, before downloading the code to your real micro:bit.
When running the code in real life, you will need to Show serial on the bottom bar to expand the terminal.
You may need to press the micro:bit's Reset button on the back to get your data.
A stream of numbers can be difficult to follow, but you can make it more legible by adding extra things into your print statement. Try:
print('Light level =', light)
You can expand it even further by adding as much information as you need, e.g.
print('Light level =', light, '; Temperature =', temp)
Try it in your own program, adding as many print statements as seem helpful.
Example program: LightTemp-Print.py
Print statements are extremely useful when testing new components, such as ultrasonic distance sensors, as you are able to make sure they are working correctly, and understand the values they give.
print('Front =', front, '; Left =', left)
Example program: RobotUltrasonics-Print.py
Once your initial debugging and calibration is complete, you will want to unplug your micro:bit from the computer, to attach it to your robot or use in some other project. This means that the terminal is no longer accessible.
You can use the micro:bit's display to help you to quickly see if there is an error, for example if one of the sensors does something unexpected!
In your light and temperature program, you could create error messages if, for example, the temperature reaches 100 degrees, which would suggest something has gone wrong with the sensor.
Example program: LightTemp-Display.py
When an ultrasonic distance sensor is not working correctly, it can give a negative number. You could add an error notification into your robot program, to show something on the display if a sensor is not connected.
if front < 0: display.show('F')
Example program: RobotUltrasonics-Display.py