I was very dismissive of the BBC micro:bit when I first read about it. A sub-RaspberryPi computer which cost more than the RaspberryPi Zero? Really? And if the measure of a computer’s power is what you can achieve with it, the BBC micro:bit doesn’t even stack up very well against its venerable forebear – the 1981 vintage BBC Model B. After all, with the Model B you could word process, do spreadsheets, communicate, play Elite or Exile and more besides. With the micro:bit, more memory and more powerful processor notwithstanding, you can’t do anything like as much. A bit pointless, surely?
Now that I have one, and I’ve played with one, it turns out that it’s apparent lack of power is exactly the point. There’s no need to ensure that your program plays nicely with the other applications that might be running – your software is guaranteed to be the only software on the system. Operating system updates? No need to worry about that either. This is pure, stripped down, simple computing. And with only 5×5 pixels (with 10 shades of red, including ‘off’), there’s no need to worry about whether you’re a great graphic artist. No one is a great graphic artist on this thing! It’s just you, your programming acumen, and your imagination. So what do you want to do?
The first thing I wanted to do was write a compass app. But that really is trivial. The next thing was a tamagotchi. But that was trivial too (although I have some ideas for a more advanced version – watch this space!) So my first, moderately complex, program was a digital clock.
The face buttons on the Raspberry Pi are used to set the time – left button advances the hours, and right advances the minutes. But 5×5 pixels are too few to show the time accurately – aren’t they? Well yes. That’s true. As a representation in clock form, or using decimal digits. But binary? There are definitely enough pixels for that. We can even fit seconds in!
It’s 7:01.24PM! Of course, 5 pixels only gets us to 31. Not enough. There are a few ways around this, but I chose to implement an ‘add 30’ bit – it keeps telling the time simple (and for users who aren’t confident in binary then they’ll at least know which half of the clock the second or minute hand would be in).
•oooo ooooo oo•oo ooooo oo•oo
The ‘lit’ bit on the top line indicates that the time is something ‘PM’. The ‘lit’ bit on the middle line indicates that 30 needs to added to the number of minutes shown on the second line (which will show minutes 0-29 in binary). The ‘lit’ bit on the bottom line indicates that 30 needs to added to the number of seconds shown on the fourth line (which will show seconds 0-29 in binary).
oooo• ooo•o ooooo oooo• oo•oo
In this example, the micro:bit is displaying 1:02.31AM
So you’d like to have a bash at this yourself? Feel free – here’s the code, in Python (natch). If you make any improvements to it, please let me know what they are so that I can share with others.
from microbit import * def makeBinary(intValue,padding): number = intValue returnValue = "" brightness = 7 #value 0 to 8 while number >0: bit = int(number % 2) if bit > 0: bit = brightness quotient = int(number / 2) returnValue = str(bit)+returnValue number = quotient for i in range(len(returnValue),padding): returnValue = "0"+returnValue return returnValue timeAdvance = 0 minuteAdvance = 0 hourAdvance = 0 secondCounter = 0 while True: if button_a.was_pressed(): #advance hours hourAdvance = hourAdvance + 1 if hourAdvance > 23: hourAdvance = 0 timeAdvance = (hourAdvance*60*60*1000)+(minuteAdvance*60*1000) elif button_b.was_pressed(): #advance minutes minuteAdvance = minuteAdvance + 1 if minuteAdvance > 59: minuteAdvance = 0 timeAdvance = (hourAdvance*60*60*1000)+(minuteAdvance*60*1000) else: #calculate and display time if (running_time()-secondCounter) > 1000: secondCounter = running_time() seconds = (running_time()/1000)%60 minutes = ((running_time()+timeAdvance)/1000/60)%60 hours = ((running_time()+timeAdvance)/1000/60/60)%24 pmString = "0" addthirtyMString = "00000" addthirtySString = "00000" if hours>12: pmString = "7" hours = hours - 12 if minutes>29: addthirtyMString = "00700" minutes = minutes - 30 if seconds>29: addthirtySString = "00700" seconds = seconds - 30 hourString = makeBinary(hours,4) minuteString = makeBinary(minutes,5) secondString = makeBinary(seconds,5) time = Image(pmString+hourString+":"+minuteString+":"+addthirtyMString+":"+secondString+":"+addthirtySString) display.show(time)
I liked this but thought it was confusing having to add decimal 30’s to binary. Better to stick to either decimal OR binary.
My alteration to your code was simple – instead of adding a 30s indicator, add a binary 32 indicator. So I used the bottom row of 5 LED’s for the binary 1, 2, 4, 8, 16 from right to left and then the left-most second row as binary 32 LED. Likewise for the minutes using the 3rd and 4th rows. And so the final top 5th row can represent the hours in 24hr mode using binary 1,2,4,8 and 16. Of course, 32 isn’t needed. So the code changed required were :-
# Add your Python code here. E.g.
from microbit import *
def makeBinary(intValue,padding):
number = intValue
returnValue = “”
brightness = 8 #value 0 to 8
while number >0:
bit = int(number % 2)
if bit > 0:
bit = brightness
quotient = int(number / 2)
returnValue = str(bit)+returnValue
number = quotient
for i in range(len(returnValue),padding):
returnValue = “0”+returnValue
return returnValue
timeAdvance = 0
minuteAdvance = 0
hourAdvance = 0
secondCounter = 0
while True:
if button_a.was_pressed():
#advance hours
hourAdvance = hourAdvance + 1
if hourAdvance > 23:
hourAdvance = 0
timeAdvance = (hourAdvance*60*60*1000)+(minuteAdvance*60*1000)
elif button_b.was_pressed():
#advance minutes
minuteAdvance = minuteAdvance + 1
if minuteAdvance > 59:
minuteAdvance = 0
timeAdvance = (hourAdvance*60*60*1000)+(minuteAdvance*60*1000)
else:
#calculate and display time
if (running_time()-secondCounter) > 1000:
secondCounter = running_time()
seconds = (running_time()/1000)%60
minutes = ((running_time()+timeAdvance)/1000/60)%60
hours = ((running_time()+timeAdvance)/1000/60/60)%24
BinarythirtytwoMString = “00000”
BinarythirtytwoSString = “00000”
if minutes>32:
BinarythirtytwoMString = “80000”
minutes = minutes – 32
if seconds>32:
BinarythirtytwoSString = “80000”
seconds = seconds – 32
hourString = makeBinary(hours,5)
minuteString = makeBinary(minutes,5)
secondString = makeBinary(seconds,5)
time = Image(hourString+”:”+BinarythirtytwoMString+”:”+minuteString+”:”+BinarythirtytwoSString+”:”+secondString)
display.show(time)