I probably should admit this - I was almost to the point of procrastinating to put up this re-cap tutorial article because I was feeling quite tired today. In fact, this entire week has felt like a roller coaster ride for me. I won't repeat something that I've already talked about in my last article (you can read it here).
Alright, let's just get straight into the re-cap shall we?
Getting Started with MicroPython & BBC Micro:bit
For the purpose of this online stem course, I have made a few assumptions about you. You are either someone...
With some experience of using the Micro:bit product
With some programming experience on other products like Raspberry Pi, Arduino and so on.
If you are really new to Micro:bit, I would recommend that you refer to my other article on #BlocksWithMicrobit. That should be a really good starting point for anyone that is totally new to BBC Micro:bit.
mu-editor: First Look
In the introductory session, I have explained with a simple overview of what each of the toolbar options are for, as well as what each part of the editor is. During the Session 01 live broadcast however, I realized that the pop-up screens are not exactly part of the main editor window and thus some of you will notice that the Mode Selection screen for example, didn't appear. So I want to apologise for that again. Next time I will just share my entire monitor screen.
But let's start by clicking on the "Mode" toolbar option.
Once you have that done, you should be given a list of options that you can work with. They include:
Adafruit CircuitPython
BBC micro:bit
Pygame Zero
Python 3
I won't be going into what each of those modes are for, but I am thinking of creating a separate lab series/online course that will dive deeper into Programming with MicroPython. That will be pretty much a beginner - intermediate level course by itself. Plus I will also be attempting to speak to various brands like Adafruit, Pycom and MicroPython themselves to try and secure a range of their products that can be used for that course. For now, that is really just an idea. I will release more details about it when it becomes more certain.
Moving on, make sure that you have the "BBC micro:bit" option selected.
If for some reasons, you do not see the "untitled" program file tab just above the editor area, just click on the New toolbar option and a tab will be added for you.
Programming MicroPython for the BBC micro:bit (Part One)
Okay, time to talk about some programming. The very first thing that we are going to do, is to type the import library statement. I'll explain more about it in a moment.
So, what is going on here?
The first part of the statement "from microbit" tells the program that we want to reference some capabilities and functionalities from within the microbit library. Think of the microbit library like the parent container for all of the different default toolbox capabilities within MakeCode.
The next part of the statement that says "import *" means we want to include every functionality that is available within the microbit library itself.
Next, like the "forever" loop block within the MakeCode editor, we need a way to tell the micro:bit to keep running our code. To do that, we have to enter the following code:
Once you are done with that, let's just continue first with the rest of the programming code.
Enter "display.", and be sure to include the "." character as well. You will notice a list of possible values that will be appearing beside it.
What mu-editor is doing here is basically letting you know what are the options that are available. In the programming world, we call it the "intellisense". Basically, it senses that you have entered the "display" functionality and it knows that you are going to likely need to use one of the following available capabilities.
Let's continue by typing "scroll(". Now, you will notice another thing that is happening. Because I have magnified my font, so everything seemed to exploded in size as well. LOL. But it should look fine on your screen.
What this little pop-up screen is doing is that its telling you what are the different values that are going to be needed for this particular function to work.
So, we continue by entering the following values:
What are the values:
string - represents the string value that you want the LED display to scroll
delay=? - represents the amount of time to pause in between the scrolling (the default value here is 150, but you can change it to any amount of time you want. It's in milliseconds)
wait=True - the display will not scroll continuously
loop=False - display the value only once.
Flashing to the Micro:bit
Before we flash our program to the micro:bit board, let's save the program file. Make it a good habit to always save your work. So that in any situation, if the application crashes or if you laptop suffers a malfunction of sorts, your effort and work is saved. Trust me, someday, you will be thanking me for giving you this advice.
Save as
Enter the name that you want to save the program as. Always remember to enter the ".py" file extension behind the filename. This will tell your computer to recognise the file as a Python programming language file.
Now we can flash the program onto our micro:bit.
One of things that you will find very useful about the mu-editor is that when you have your micro:bit connected to your laptop or PC, it automatically recognises that the board is connected and it will automatically send the program to the micro:bit board for you. How awesome is that!
Programming MicroPython for the BBC micro:bit (Part Two)
Next up, we are going to be working with Input events. (Please pardon my mouse cursor in the screenshot. Just ignore it.)
For the program to recognise if you have pressed a particular button, we have to make use of the if...elif... statements.
Notice that the if conditional statement here checks if button A has been pressed before asking the program to do something. So, when the program runs to the point where it hits the colon ":" character, it recognises that the next section of programming code is something that it has to run.
We will enter a similar display.scroll statement like the one we have seen before earlier in this tutorial.
And then what about Button B?
We do that by adding another conditional statement below but this time instead of using "if", we will use the keyword "elif", which is short of "else if".
Important Note: When working with any programming code, especially Python, you have to respect the spaces for each block (section of code). We call them "indentations". The python interpreter (compiler) need those spaces in order to differentiate between each section of code.
Alright, finally we have a complete program. Go ahead and save it and flash the program onto your micro:bit.
Final Look
And with that, we are done with this tutorial! Definitely a lot shorter than the Basics tutorial that I have published yesterday. But that's only because I think you already know some of the basic concepts of working with the BBC micro:bit, or you already have done some programming prior to making the effort to following this online stem course.
If you like to re-watch the live session from this recent Sunday, I have posted the video here:
You will notice that there are some points that might have been touched in the live video session that isn't covered in this tutorial, so don't worry about it. The slight differences usually aren't that crucial.
The main purpose of publishing these lesson re-cap tutorials is so that you can take away with you the most important key points for each session.
Okay, this is awesome! You are awesome! Take note that we will be having another round of sessions this weekend, same timing on Sunday at 9 pm SGT and 2 pm BST.
Be sure to subscribe to our YouTube channel so that you can be notified when we go live. Official YouTube Channel: https://www.youtube.com/channel/UC9Vn5b7Ey-saBP-2U83nhdw
If you like this tutorial and you find it helpful, please help to share this article with your friends, and also, sign up as a member to start liking and leaving comments on our content.
Comments