The other day I bought a micro:bit for my daughter. It’s a small computer with bluetooth, an accelerometer, light sensor, magnetometer and temperature sensor. It also has a couple of buttons for input and a matrix of LEDs for output. It’s become obvious that I’m way more interested in playing with it than my daughter is but I’m hopeful that after I show her what it can do, she’ll take an interest in writing some code for it herself.
My first small program takes the input from the compass and writes out the cardinal directions, N, S, E and W.
This is what the program looks like in Blocks
and here is the equivalent JavaScript code
let heading = 0 basic.forever(() => { heading = input.compassHeading() if (heading < 45 || heading > 315) { basic.showString("N") } else if (heading < 135) { basic.showString("E") } else if (heading < 225) { basic.showString("S") } else { basic.showString("W") } })
Below is an emulator running this code. Drag the compass near the top and you will see the text on the micro:bit change.