{"id":522,"date":"2016-09-03T19:33:01","date_gmt":"2016-09-03T18:33:01","guid":{"rendered":"http:\/\/www.45rpmsoftware.com\/wordpress\/?p=522"},"modified":"2016-09-03T19:33:01","modified_gmt":"2016-09-03T18:33:01","slug":"a-clock-for-your-microbit","status":"publish","type":"post","link":"https:\/\/www.45rpmsoftware.com\/blog\/?p=522","title":{"rendered":"A Clock For Your micro:bit"},"content":{"rendered":"<p>I was very dismissive of the BBC\u00a0micro:bit when I first read about it. \u00a0A sub-RaspberryPi computer which cost more than the RaspberryPi Zero? \u00a0Really? \u00a0And if the measure of a computer&#8217;s power is what you can achieve with it, the BBC micro:bit doesn&#8217;t even stack up very well against its venerable forebear &#8211; the 1981 vintage BBC Model B. \u00a0After all, with the Model B you could word process, do spreadsheets, communicate, play Elite or Exile and more besides. \u00a0With the micro:bit, more memory and more powerful processor notwithstanding, you can&#8217;t do anything like as much.\u00a0 \u00a0A bit pointless, surely?<!--more--><\/p>\n<p>Now that I have one, and I&#8217;ve played with one, it turns out that it&#8217;s apparent lack of power is exactly the point. \u00a0There&#8217;s no need to ensure that your program plays nicely with the other applications that might be running &#8211; your software is guaranteed to be the only software on the system. \u00a0Operating system updates? No need to worry about that either. \u00a0This is pure, stripped down, simple computing. \u00a0And with only 5&#215;5 pixels (with 10 shades of red, including &#8216;off&#8217;), there&#8217;s no need to worry about whether you&#8217;re a great graphic artist. \u00a0No one is a great graphic artist on this thing! \u00a0It&#8217;s just you, your programming acumen, and your imagination. \u00a0So what do you want to do?<\/p>\n<p>The first thing I wanted to do was write a compass app. \u00a0But that really is trivial. \u00a0The next thing was a tamagotchi. \u00a0But that was trivial too (although I have some ideas for a more advanced version &#8211; watch this space!) \u00a0So my first, moderately complex, program was a digital clock.<\/p>\n<p>The face buttons on the Raspberry Pi are used to set the time &#8211; left button advances the hours, and right advances the minutes. \u00a0But 5&#215;5 pixels are too few to show the time accurately &#8211; aren&#8217;t they? \u00a0Well yes. \u00a0That&#8217;s true. \u00a0As a representation in clock form, or using decimal digits. \u00a0But binary? \u00a0There are definitely enough pixels for that. \u00a0We can even fit seconds in!<\/p>\n<p><a href=\"http:\/\/www.45rpmsoftware.com\/wordpress\/wp-content\/uploads\/2016\/09\/IMG_1941.jpg\" rel=\"attachment wp-att-524\"><img loading=\"lazy\" class=\"aligncenter wp-image-524 size-full\" src=\"http:\/\/www.45rpmsoftware.com\/wordpress\/wp-content\/uploads\/2016\/09\/IMG_1941-e1472927567720.jpg\" alt=\"IMG_1941\" width=\"1122\" height=\"999\" srcset=\"https:\/\/www.45rpmsoftware.com\/blog\/wp-content\/uploads\/2016\/09\/IMG_1941-e1472927567720.jpg 1122w, https:\/\/www.45rpmsoftware.com\/blog\/wp-content\/uploads\/2016\/09\/IMG_1941-e1472927567720-300x267.jpg 300w, https:\/\/www.45rpmsoftware.com\/blog\/wp-content\/uploads\/2016\/09\/IMG_1941-e1472927567720-768x684.jpg 768w, https:\/\/www.45rpmsoftware.com\/blog\/wp-content\/uploads\/2016\/09\/IMG_1941-e1472927567720-1024x912.jpg 1024w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/p>\n<p>It&#8217;s 7:01.24PM! \u00a0Of course, 5 pixels only gets us to 31. \u00a0Not enough. \u00a0There are a few ways around this, but I chose to implement an &#8216;add 30&#8217; bit &#8211; it keeps telling the time simple (and for users who aren&#8217;t confident in binary then they&#8217;ll at least know which half of the clock the second or minute hand would be in).<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<pre>\u2022oooo\r\nooooo\r\noo\u2022oo\r\nooooo\r\noo\u2022oo<\/pre>\n<p>The &#8216;lit&#8217; bit on the top line indicates that the time is something &#8216;PM&#8217;. \u00a0The &#8216;lit&#8217; 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).\u00a0\u00a0The &#8216;lit&#8217; bit on the bottom\u00a0line indicates that 30 needs to added to the number of seconds\u00a0shown on the fourth\u00a0line (which will show seconds\u00a00-29 in binary).<\/p>\n<hr \/>\n<pre>oooo\u2022\r\nooo\u2022o\r\nooooo\r\noooo\u2022\r\noo\u2022oo<\/pre>\n<p>In this example, the micro:bit is displaying 1:02.31AM<\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>So you&#8217;d like to have a bash at this yourself? \u00a0Feel free &#8211; here&#8217;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.<\/p>\n<pre>from microbit import *\r\n\r\ndef makeBinary(intValue,padding):\r\n    number = intValue\r\n    returnValue = \"\"\r\n    brightness = 7 #value 0 to 8\r\n    while number &gt;0:\r\n        bit = int(number % 2)\r\n        if bit &gt; 0:\r\n            bit = brightness\r\n        quotient = int(number \/ 2)\r\n        returnValue = str(bit)+returnValue\r\n        number = quotient\r\n    for i in range(len(returnValue),padding):\r\n        returnValue = \"0\"+returnValue\r\n    return returnValue\r\n\r\ntimeAdvance = 0\r\nminuteAdvance = 0\r\nhourAdvance = 0\r\nsecondCounter = 0\r\nwhile True:\r\n    if button_a.was_pressed():\r\n        #advance hours\r\n        hourAdvance = hourAdvance + 1\r\n        if hourAdvance &gt; 23:\r\n            hourAdvance = 0\r\n        timeAdvance = (hourAdvance*60*60*1000)+(minuteAdvance*60*1000)\r\n    elif button_b.was_pressed():\r\n       #advance minutes\r\n       minuteAdvance = minuteAdvance + 1\r\n       if minuteAdvance &gt; 59:\r\n           minuteAdvance = 0\r\n       timeAdvance = (hourAdvance*60*60*1000)+(minuteAdvance*60*1000)\r\n    else:\r\n        #calculate and display time\r\n        if (running_time()-secondCounter) &gt; 1000:\r\n            secondCounter = running_time()\r\n            seconds = (running_time()\/1000)%60\r\n            minutes = ((running_time()+timeAdvance)\/1000\/60)%60\r\n            hours = ((running_time()+timeAdvance)\/1000\/60\/60)%24\r\n            pmString = \"0\"\r\n            addthirtyMString = \"00000\"\r\n            addthirtySString = \"00000\"\r\n            if hours&gt;12:\r\n                pmString = \"7\"\r\n                hours = hours - 12\r\n            if minutes&gt;29:\r\n                addthirtyMString = \"00700\"\r\n                minutes = minutes - 30\r\n            if seconds&gt;29:\r\n                addthirtySString = \"00700\"\r\n                seconds = seconds - 30\r\n            hourString = makeBinary(hours,4)\r\n            minuteString = makeBinary(minutes,5)\r\n            secondString = makeBinary(seconds,5)\r\n            time = Image(pmString+hourString+\":\"+minuteString+\":\"+addthirtyMString+\":\"+secondString+\":\"+addthirtySString)\r\n            display.show(time)<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was very dismissive of the BBC\u00a0micro:bit when I first read about it. \u00a0A sub-RaspberryPi computer which cost more than the RaspberryPi Zero? \u00a0Really? \u00a0And if the measure of a computer&#8217;s power is what you can achieve with it, the BBC micro:bit doesn&#8217;t even stack up very well against its venerable forebear &#8211; the 1981 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.45rpmsoftware.com\/blog\/?p=522\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;A Clock For Your micro:bit&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/522"}],"collection":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=522"}],"version-history":[{"count":0,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/522\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}