Thursday, May 03, 2018

The Micro:bit - An Adorably Functional Mini Computer

Meet the Micro:bit, my newest (and cutest?) computer:

The micro:bit is tiny computer designed to teach students how to program. Apparently, every year 7 student in the UK receives one for free. While I appreciate the educational value of the micro:bit, I'm more interested in putting its compact size and easy operation to use as an experimental wearable platform. I'm also looking forward to practicing a sort of haiku style of programming, where the constraints of the micro:bit serve as a source of creativity.

I wrote my first micro:bit program yesterday, and I have to say, it was an absolute joy to do so. I wrote the code in JavaScript using a helpful editor in a web browser, and was able to run the code on the device by dragging and dropping a single file on my Windows PC. The web UI also offers a drag-and-drop block-style programming approach that should work well for individuals new to programming. The folks behind the micro:bit have gone to great lengths to reduce the effort needed to see your code run on the device itself, and as far as I can tell, they've nailed it.

While using a web browser to write simple programs may work, it's not a particularly scalable environment. For more seasoned programmers, the micro:bit team offers command line utilities to build and run code, which I expect to use going forward.

Here's the code behind my first app:


input.calibrateCompass()
let goal = 0;
let showMode = false;

input.onButtonPressed(Button.A, () => {
    goal = input.compassHeading();
})

input.onButtonPressed(Button.B, () => {
    showMode = true;
})


basic.forever(() => {
    let heading = input.compassHeading();
    let offsetRight = heading > goal ? heading - goal : (360 - goal) + heading
    let offsetLeft = goal > heading ? goal - heading : (360 - heading) + goal
    let offset = offsetRight < offsetLeft ? offsetRight : offsetLeft;
    let x = offsetRight < offsetLeft ? 4 : 0;
    let dir = offsetRight < offsetLeft ? 1 : -1;
    let mag = Math.floor(offset / 36);
    basic.clearScreen();
    if (showMode) {
        basic.showNumber(dir * offset);
        showMode = false;
        basic.pause(200)
    } else {
        if (mag == 0) {
            basic.showIcon(IconNames.Target)
        } else {
            for (let i = 0; i < mag; i++) {
                led.plot(x, 4 - i)
            }
        }
        basic.pause(200)
    }
})

The app uses the compass sensor built into every micro:bit. Pressing the A key sets a heading. As you veer off this heading, LED's on the right or left light up to encourage you to get back on course. As you veer farther off course, more LEDs are lit. Pressing the B key displays the current offset.

I can't speak highly enough about the micro:bit programming environment, API and general capabilities of the device. The limiting factor here isn't understanding low level hardware or software details, or putting up with a finicky programming environment, it's your imagination.

Up next, I'm hoping to get the on-board Bluetooth found on the micro:bit to talk to my phone. If I can pass arbitrary data over this connection, then a new world of possibilities opens up. Look out Apple Watch, I'm coming for you!

A huge thanks to my Mother-in-Law who bought me the micro:bit (and a whole collection of cool add-ons) for my Birthday!

No comments:

Post a Comment