Consult the API for the various built-in images, and make your bot display one of some animal of your choice.
Section 6.1 Lab: Introduction to microbit
In this we will get our first test of programming the microbit controllers.
Subsection 6.1.1 Basic programs in micro:bit
-
Follow the "Get started" tutorial. It will teach you how to start a new program file, and how to upload the file to the micro:bit to run it. You will also learn your first three commands:
display.show,sleepanddisplay.scroll. -
Follow the Writing micro:bit programs tutorial. This teaches you a number of more advanced techniques such as using the two buttons on the micro:bit, but is also a refresher of basic Python stuff.
Practice 6.1.1.
Practice 6.1.2.
Read in the API about how to specify a custom image, then make an image of your choice. I suggest some version of the HC Panther.
Practice 6.1.3.
The
Image object also provides a list of images, for example the list of all arrows or the list of all "clocks". Call the display.show function by providing it a list like that and see it loop through all the different symbols. Look through the API above to find how you can control the delay between images, to make them take a second between each image.
Practice 6.1.4.
Use this approach of a list of images to create the animation of a person that enters from the right of the screen and leaves on the right. You can do this manually if you like, or you may use an LLM/AI to help you create this. An approach to doing that could be: Ask it to generate an animation of 5x5 grid frames each being either "filled" or "empty"; then ask it to transform these frames by replacing the empty spots with 0 and the filled spots with 1, and concatenating all the lines of each frame separated by a colon, then putting each frame in a string. Finally ask it to wrap each string in a call to
Image. Then store this list in a variable and use it with display.show.
Practice 6.1.5.
Use the
display.scroll command together with a Python for loop to create a small program that will go through all the numbers from 10 down to 1 and for each number scroll it through the screen then wait for half a second before the next number. But make sure the scroll of each number happens faster than normal (look for the documentation for scroll to learn how to control this), then afterwards it shows some big explosion or star like image, for a second, before starting again.
Practice 6.1.6.
Use the serial monitor you learned about in the lab in order to modify the above command so that instead of starting at 10 it asks the user to input the number.
Subsection 6.1.2 Using the accelerometer to emulate dice-rolling
For this part weβll learn a bit about the micro:bitβs accelerometer capabilities. The accelerometer is in general the circuit that detects movement.
-
Follow the "dice" Dice tutorial for a very basic mechanism for generating random dice.
-
Follow the Graphical Dice tutorial for a very a variation that shows the dice by image instead of number.
Practice 6.1.8.
Modify the graphical-dice example so that the various die images are stored in a Python list and an entry is selected directly based on the random number. In your final version there should no longer be any
if and elif constructs.
Practice 6.1.9.
Building on the previous example, modify the graphical dice example further as follows: When the shake is detected, the code generates a list of 10 random die rolls. Then it proceeds to show each of the rolls in quick succession (about 4 within a second) before settling on the final number. The idea is to simulate a bit the behavior of rolling the die and how things change until it settles.
Practice 6.1.10.
This is a bigger program bringing things together. Here is the behavior you need to produce:
-
When "idle" the robot shows a "beating heart", with the heartbeat animation alternating between a smaller heart and a larger heart.
-
If shaken the bot enters an "active" state where it starts producing and showing random die numbers, graphically.
-
If while in the active state the B button is pressed then the bot settles on one random number and only shows that. You could say it has entered a "stable" state.
-
If at any point the A button is pressed then the bot reverts to the idle state.
-
If the B button is pressed during the idle state then the last rolled number is shown for 1 second before the heartbeat resumes.
You will want to use a global "state" variable to represent the three different states that the bot may be in. Use 1, 2 and 3 to represent the three different states, but give them all-caps names that reflect what they represent, for example
ST_IDLE = 1.Bonus: Look into the
audio or music modules and add a suitable sound to indicate the end of the roll.
To complete this lab, show your instructor this final program in action.
