← Micro:bit

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 เครื่องมือพัฒนาโปรแกรม

ToolWhere to find itNotes
MakeCodemakecode.microbit.orgBlocks and JavaScript, in your browser. We use this.
Python Editorpython.microbit.orgPython in your browser. Mu is an installed editor.
Phone or tablet appSearch “Micro:bit” in your app storeSends code over Bluetooth. The book found the app unreliable in 2018, so test it first.

Next: get ready →

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.

  1. Open makecode.microbit.org in Chrome or Edge.
  2. Start a project. Under Advanced, click Extensions. The book calls this “Add Package”.
  3. Choose neopixel, for programmable RGB LEDs, and wait for it to load.
  4. Bookmark the page. After this, the bookmark opens MakeCode even without the internet.

🧪 Lab 3.2 · Connect the Micro:bit

  1. Use a micro USB data cable. Some cables only charge. The yellow LED on the back lights.
  2. Wait a moment. A drive called MICROBIT appears, like a USB flash drive.

Next: your first program →

3.2 Your first program ตัวอย่างการพัฒนาโค้ด

  1. Write the code.
  2. Test it in the simulator.
  3. Download the .hex file.
  4. Copy it to the MICROBIT drive.
  5. Watch it run.

🧪 Lab 3.3 · A beating heart

  1. Click New Project. Every project starts with two blocks: on start runs once at the start, and forever repeats without end.
  2. We do not need on start, so drag it back to the menu on the left and drop it on the bin.
  3. From Basic, put show icon inside forever and pick the big heart.
  4. Add pause (ms) and set it to 100, which is 0.1 seconds.
  5. Add another show icon and pick the small heart.
  6. Name the project Start and click Download.
  7. Copy the .hex file to the MICROBIT drive.
  8. The yellow LED flashes while the file copies. When it stops, your code runs.

💻 Beating heart (project “Start”)

forever
show icon
pause (ms) 100
show icon
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.

Next: text and letters →

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

forever
show string Hello!MicroBit
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

  1. In on start, add set variable to and name the variable letters.
  2. From Advanced › Arrays, drop in an array of text.
  3. Click + on the array until there are 26 slots, then type A to Z.
  4. In forever, add for index from 0 to 25.
  5. Inside the loop, add show string with letters get value at index, then pause (ms) 1000.

💻 Example 2: letters A to Z (corrected)

on start
set letters to array of A, B, C … Z
forever
for index from 0 to 25
show string letters get value at index
pause (ms) 1000
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.

Next: the buttons →

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.

PressWhat happens
AThe heart beats 4 times, then a tick appears
BA star appears
A and B togetherA line appears across the middle

💻 Example 3: ON Button

on button A pressed
repeat 4 times
show leds
pause (ms) 100
show leds
show icon
on button B pressed
show leds
on button A+B pressed
show leds
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(`
		. . . . .
		. . . . .
		# # # # #
		. . . . .
		. . . . .
		`)
})

Next: review →

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

  1. Change the text in Example 1 to your own name.
  2. Change the pause in the heart program to 500, then 0. What changes?
  3. Use show leds to draw your own picture for button B.
  4. 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.

1. When do the blocks inside on start run?
2. When do the blocks inside forever run?
3. pause (ms) 1000 waits for…
4. How do you delete a block?
5. On the computer, a connected Micro:bit appears as…
6. The yellow LED on the back is flashing. What is happening?
7. A list has 26 letters. In a loop “for index from 0 to N”, what should N be?
8. In Example 3, what makes the code run when you press button B?