This is a memo of writing a micro: bit program in VS Code and Python. The feature of micro: bit is that it can be created in a visual programming environment, and it is doubtful if anyone wants to write it in Python. The usage environment is like this.
The explanation is based on the assumption that VS Code already contains Python extensions.
A tool for writing Python programs to the micro: bit.
$ pip install uflash
It seems that the VS Code extension that will be introduced later uses the "uflash" command, so make sure that it can be used properly.
$ uflash --version
uflash 1.3.0
It's OK if the version comes out.
Put the extension corresponding to micro: bit in VS Code. Search for "micro: bit" in the extension search field and install the extension below. After installing, let's restart VS Code for the time being. This completes the construction of the environment.
Create a new directory in a suitable location and create a Python file in it. Here, the directory name is "microbit" and the Python file name is "main.py". Drag and drop the created directory to VS Code and open the Python file on VS Code. If the extension is installed properly, the button "Build current file to Micro: Bit" will appear in the upper right.
Now let's write the code for micro: bit.
from microbit import *
while True:
if button_a.is_pressed():
display.show(Image.HAPPY)
else:
display.show(Image.ANGRY)
Even so far, I can write to micro: bit at least, but the red line appears in the function for micro: bit on VS Code and the completion does not work yet. To make the completion work, open the VS Code command palette (Cmd + Shift + P) and execute the following command.
Fetch micro:bit modules
Immediately after executing the command, the red line does not disappear yet, but once the source file is saved, it will be reflected and the red line will disappear.
When you connect the micro: bit to your PC and press the "Build current file to Micro: Bit" button, the orange LED on the micro: bit will start blinking, and when it stops blinking, writing is complete. After the writing is completed, if you press or release the A button on the main unit, the facial expression displayed on the 5x5 LED matrix will change.
The micro: bit has a built-in LED/button/sensor as a single unit, and wireless communication is possible if multiple units are prepared, so it seems that you can make interesting things depending on your ingenuity. I had a hands-on experience at a university festival, and I had the impression that even elementary school students who had no programming experience could enjoy using it using the visual programming environment MakeCode. Even if you use Python, the Japanese documentation is solid, so it's relatively easy to get started. Considering that programming materials for children cost more than 10,000 yen, micro: bit, which supports both visual development environment and text development environment as well as being able to buy the main body for around 3000 yen, seems to be a powerful option. ..
Recommended Posts