You may remember that, a little over a year ago, I wrote a clock for the micro:bit (read it again here if you need a reminder). I pondered adding alarm functionality to it but, in the event, I didn’t get around to it. In keeping with that tradition, I now offer you a calculator for the micro:bit – this one has unimplemented improvements too!
I run a computer club for the kids at the local school where we teach them how to code – free from the constraints of the National Curriculum. It’s no secret that I don’t like Scratch, and I think that one of the reasons that the British Isles produced so many great coders is because they learned Basic on the ZX Spectrum and BBC computers from an early age.
If you randomly bang the keys on one of these old machines, the equivalent of randomly plopping blocks onto the screen in Scratch, Basic doesn’t do anything except spit syntax errors. Python (a wonderful learning language, as well as one which can do real work) is much the same as Basic in this regard. Scratch, on the other hand, still draws you a cat. It may even move the cat around, and it’ll certainly let you draw on the screen with its built in tools. And what kind of a lesson is that?
Being a parent (and a massive geek) I’d quite like my kids to enjoy what I enjoy (although I’m also delighted for them to find something different of their own). My eldest son, whose jokes are getting better and who tests my patience less with every passing day, likes coding, and so I’ve been teaching him outside of computer club as well as in it.
We think through the problem logically, drawing plans on the big blotter on my desk, before typing it up. And, whilst we sometimes play with the Pi, mostly we play with the Micro:bit. It’s an interesting challenge to code around the limitations of a 5 x 5 pixel display, and two buttons. This time we decided to write a calculator in JavaScript.
let calculate_total = 0 let operator = 0 let number = 0 let total = 0 basic.forever(() => { if (input.buttonIsPressed(Button.AB)) { if (operator == 0) { total = total + number } if (operator == 1) { total = total - number } if (operator == 2) { total = total * number } if (operator == 3) { total = total / number } basic.showNumber(total) } else { if (input.buttonIsPressed(Button.A)) { number += 1 calculate_total = 1 if (number < 0 || number > 9) { number = 0 } basic.showNumber(number) } if (input.buttonIsPressed(Button.B)) { if (calculate_total) { calculate_total = 0 if (operator == 0) { total = total + number } if (operator == 1) { total = total - number } if (operator == 2) { total = total * number } if (operator == 3) { total = total / number } } operator += 1 if (operator < 0 || operator > 3) { operator = 0 } if (operator == 0) { basic.showLeds(` . . # . . . . # . . # # # # # . . # . . . . # . . `) } if (operator == 1) { basic.showLeds(` . . . . . . . . . . # # # # # . . . . . . . . . . `) } if (operator == 2) { basic.showLeds(` # . . . # . # . # . . . # . . . # . # . # . . . # `) } if (operator == 3) { basic.showLeds(` . . # . . . . . . . # # # # # . . . . . . . # . . `) } } } })
There are plenty of ways that this calculator could be enhanced – at the moment it only permits single digit numbers to be entered. Perhaps this could be fixed by cycling through the numbers using a quick tap on the left button, and changing the magnitude with a long tap on the left button. In this manner ‘1’ would be entered with a single short tap, ‘2’ would be entered with two short taps (as today) and ’21’ would be entered with two short taps, a long tap, and a single short tap.
In any event, if you want to use this calculator ‘as is’, bear in mind that you can only enter the numbers 0 – 9 (although it can perform operations on numbers much larger than this if you calculate your way there!).
- Tap the left button select numbers 0 – 9 (once you’ve tapped your way to 9 it will loop back to 0 again).
- Tap the right button to select the operator (+, -, x, ÷)
- Press both buttons together to get the result.
Enjoy – and if you have any improvements please add them to the comments box.