Chapter 3 · Book pp. 25–44
Coding with MakeCode
การพัฒนาโปรแกรมสำหรับ Micro:bit
Coding a Micro:bit is suitable from about age 10. You will drag blocks together, send them to the board, and see them run. The book uses the word code (โค้ด) rather than “program”, and so will we.
Three ways to code เครื่องมือพัฒนาโปรแกรม
| Tool | Where to find it | Notes |
|---|---|---|
| MakeCode | makecode.microbit.org | Blocks and JavaScript, in your browser. We use this. |
| Python Editor | python.microbit.org | Python in your browser. Mu is an installed editor. |
| Phone or tablet app | Search “Micro:bit” in your app store | Sends code over Bluetooth. The book found the app unreliable in 2018, so test it first. |
3.1 Get ready การเตรียมการ
🧪 Lab 3.1 · Prepare MakeCode for offline use
A busy school network can be slow, and this lets MakeCode keep working. You need the internet once.
- Open makecode.microbit.org in Chrome or Edge.
- Start a project. Under Advanced, click Extensions. The book calls this “Add Package”.
- Choose neopixel, for programmable RGB LEDs, and wait for it to load.
- Bookmark the page. After this, the bookmark opens MakeCode even without the internet.
🧪 Lab 3.2 · Connect the Micro:bit
- Use a micro USB data cable. Some cables only charge. The yellow LED on the back lights.
- Wait a moment. A drive called MICROBIT appears, like a USB flash drive.
3.2 Your first program ตัวอย่างการพัฒนาโค้ด
- Write the code.
- Test it in the simulator.
- Download the .hex file.
- Copy it to the MICROBIT drive.
- Watch it run.
🧪 Lab 3.3 · A beating heart
- Click New Project. Every project starts with two blocks: on start runs once at the start, and forever repeats without end.
- We do not need on start, so drag it back to the menu on the left and drop it on the bin.
- From Basic, put show icon inside forever and pick the big heart.
- Add pause (ms) and set it to 100, which is 0.1 seconds.
- Add another show icon and pick the small heart.
- Name the project Start and click Download.
- Copy the .hex file to the MICROBIT drive.
- The yellow LED flashes while the file copies. When it stops, your code runs.
💻 Beating heart (project “Start”)
See it in JavaScript
basic.forever(function () {
basic.showIcon(IconNames.Heart)
basic.pause(100)
basic.showIcon(IconNames.SmallHeart)
}) ✏️ Book correction
The book says the heart beats every 0.1 seconds. It is much slower, because show icon also holds each picture for 600 ms. Press Run and count the beats yourself.
Direct download
In Chrome or Edge on a computer, you can skip copying. Click the … next to Download, choose Connect device, and MakeCode sends code straight to the board.
Open a project again
A .hex file made by MakeCode also holds your blocks. On the MakeCode home page click Import, choose Import File, and pick the file.
3.3 Examples 1 and 2 ตัวอย่างทดสอบการทำงาน
Example 1: scrolling text
show string scrolls text across the display, one letter at a time.
💻 Example 1: scrolling text
See it in JavaScript
basic.forever(function () {
basic.showString("Hello!MicroBit")
}) Example 2: letters A to Z
This uses an array (อาร์เรย์), a numbered list that starts at 0. A for loop counts through it, and each letter stays for 1 second. A single letter does not scroll, so this looks like changing flash cards.
🧪 Lab 3.4 · Build the array
- In on start, add set variable to and name the variable letters.
- From Advanced › Arrays, drop in an array of text.
- Click + on the array until there are 26 slots, then type A to Z.
- In forever, add for index from 0 to 25.
- Inside the loop, add show string with letters get value at index, then pause (ms) 1000.
💻 Example 2: letters A to Z (corrected)
See it in JavaScript
let letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
basic.forever(function () {
for (let index = 0; index <= 25; index++) {
basic.showString(letters[index])
basic.pause(1000)
}
}) ✏️ Book correction
The book’s list leaves out the letter E, and its loop runs from 0 to 26. That is 27 turns, with only 25 letters to show, so it runs off the end of the list. We have added E and stopped the loop at 25. The variable names are clearer here too.
Example 3: ON Button
The display stays dark until you press something. on button pressed is an event block: it waits, and runs only when that button is pressed.
| Press | What happens |
|---|---|
| A | The heart beats 4 times, then a tick appears |
| B | A star appears |
| A and B together | A line appears across the middle |
💻 Example 3: ON Button
See it in JavaScript
input.onButtonPressed(Button.A, function () {
for (let i = 0; i < 4; i++) {
basic.showLeds(`
. # . # .
# # # # #
# # # # #
. # # # .
. . # . .
`)
basic.pause(100)
basic.showLeds(`
. . . . .
. # . # .
. # # # .
. . # . .
. . . . .
`)
}
basic.showIcon(IconNames.Yes)
})
input.onButtonPressed(Button.B, function () {
basic.showLeds(`
. . # . .
# . # . #
. # # # .
. # . # .
# . . . #
`)
})
input.onButtonPressed(Button.AB, function () {
basic.showLeds(`
. . . . .
. . . . .
# # # # #
. . . . .
. . . . .
`)
}) Chapter summary
- on start runs once, and forever repeats.
- Blocks live in groups such as Basic, Input, Loops and Logic.
- Download gives a .hex file. Copy it to the MICROBIT drive to run it.
- Lists count from 0, and for loops include the end number.
- Event blocks such as on button pressed wait for something to happen.
🛠️ Activity · Make it yours
- Change the text in Example 1 to your own name.
- Change the pause in the heart program to 500, then 0. What changes?
- Use show leds to draw your own picture for button B.
- Challenge: build a dice with on shake and pick random 1 to 6.
Check yourself
The book has no end-of-chapter questions, so these are my own, based on its pages.