IAN LANG ELECTRONICS

For this bit we are going to swap programming interfaces for a while and use the Python one. Python is a language you find everywhere. It runs on , Windows, Unix, Mac and various small computer devices. It's horrible and I don't like it all but it's the best tool for what we are about to do on this page and so I'll have to bite the bullet and use it.

 

Python is a more traditional method than we have hitherto been using in that it consists of a type-in interface and uses a series of high level mnemonics which means you're typing in a series of human readable commands that will later be turned into a hex file. On the Microbit variant there's a series of libraries and plug-ins that you can use too. Some of them are pre-programmed music pieces. Here's an example of some code:

Making More Sounds.

bbcmicrobit Go Back

from microbit import *

import microbit as m

import music

while True:

       Degrees=m.temperature() -6

       string=Degrees

       m.display.scroll(str(string))

       if button_a.is_pressed():

        music.play(music.POWER_UP)

       m.sleep(1000)

I've actually done a step in there I didn't need to do but would have had to on something like an Arduino, and we'll get to that in a moment. For now I want to point out the main reason that I don't like Python. Observe if you will the indentations shown by the solid red rectangle:

 

from microbit import *

import microbit as m

import music

while True:

       Degrees=m.temperature() -6

       string=Degrees

       m.display.scroll(str(string))

       if button_a.is_pressed():

        music.play(music.POWER_UP)

       m.sleep(1000)

 

Unlike C which, if you used a while statement, would enclose it in curly brackets { } to mark the beginning and end of the code, and unlike VB which would use the simple end while, Python uses a series of indentations to mark out what belongs where. This is annoying if you do agile coding (which is a fancy way of saying "making it up as you go along without doing all the tedious paperwork first" because it's smacks of pedantry and it takes up thought process that you could be devoting to your actual logic flow and throws you off a bit . It's also perturbing if you're used to looking for tokens like ; : ( ) {  }  and in something really complex you end up halfway over the page. It looks nicer though.

 

The practical upshot is that those indentations need to be there. If you get them wrong it sulks and throws up an error. But it doesn't do it at coding time. Oh no. It doesn't do it at compile either. No no no. It waits until you've uploaded it into the Microbit and then the microbit tells you that you've done it wrong.  DOH!!

 

Now it was at this point that I was going to delve into the code and go through it line by line to explain what stuff did. Then a professional teacher said that it would probably be better if I jotted a few notes down about the interface first. I'm following her advice and the reason I'm claiming for following her advice is that I couldn't be bothered to argue with her about it, rather than the real reason which is that she is right and consequently I am wrong. Not having that. It's the can't-be-bothered-to-argue thing. Every time.

 

The interface then. Here's a piccy:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

I

 

 

Ignore the bit on the extreme right which is frankly useless. You'll notice there is no simulator as there was in the block editor which is a bit frustrating. It's laid out as you'd expect an integrated development environment (IDE) to be and it's actually not too bad. It's got line numbers on the left  to help you see where the reported errors are, and the big black bit there is where you type in your code. You can see the above example in there. There are also eight buttons. Let's do the big ones from left to right. Firstly, My Scripts. Clicking on this takes you back to a list of programs that you've done before. Download is fairly straightforward, you click on this, it compiles and sticks the hex file in your downloads folder or wherever you specifify ready for slinging on to your Microbit. Snippets is useful as it contains a list of commonly used code with the right syntax that you click to insert. This saves you a lot of typing. Then we come to Help. Oh dear. This is where we hit the bedrock. For a help tool this thing is extremely unhelpful and frustrating. It's not that stuff isn't there, it's just that it's a pain digging it out. For instance, to turn a numeric into a string is in fact  str( variable name ). It took the best part of thirty minutes, two John Players, one and a half cuppas and a lot of swearing to find that out. Usually when I've forgotten or I'm not sure how to do something on an Arduino it's less than two minutes looking up in the reference section and if I hadn't already got a bad opinion of Python this would do nothing to endear it to me.

Negativity aside I do prefer a type-in interface to the blocks and so that's a point in favour of it.

Back to the buttons then. The four smaller ones. On the left you can see the zoom in and zoom out, they do just as you'd expect. Then there's a save, and finally one with a bug on it. Aha! de-bugging then. Nope. It gives you a list of what you've done. Ermmm............

The very last thing to look at is the title. Here I've called music powerup. When you start the project it's always given some cutesy name. Click on the cutesy name and change it, then save.

 

Now let's have a look at that there code and see what it does. In fact it does just the same as what we've been doing so far, except it uses some libraries only available in the Python script. It plays music if you press and hold button A and shows you the temperature on the LED interface. To hear the music you'll probably need to amplify. Use the hardware we did in the last chapter hooked up like this:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

On to how the code does it. It starts with the line

 

from microbit import *

 

which is a tad mysterious. The reason it's there is a big plus in the Python method. It contains lots of libraries. These libraries, as we shall see, save a lot of work. But you have to import them first. Before you do that you have to set up a ready to import. That's what the above line is doing. Below we can start our imports:

 

import microbit as m

 

The above line imports the microbit command library and defines m as the name for our microbit. Every time we now refer to m, it will mean do something from the microbit library. Next we have:

 

import music

 

The Python system has a number of pre-set musical pieces, To use them, we need to import this library too. Coming next is a most important thing:

 

while True:

 

while True: is the way of making a loop in Python. If you remember when we looked at the blocks programmer, we had a thing called a forever loop which went round and, on reaching the last block, went back to the first. That's exactly what while True: does here. This makes this line-driven and recursive. Whilst we are here I also want you to notice the spacing of the indents:

 

from microbit import *

import microbit as m

import music

while True:

       Degrees=m.temperature() -6

       string=Degrees

       m.display.scroll(str(string))

       if button_a.is_pressed():

        music.play(music.POWER_UP)

       m.sleep(1000)

 

 

 

 

pythonide microbit-transistor

Everything under while True: is indented to the same level (red) except for what happens under the if statement (blue) which is one space further along. This is how Python decides what goes where. It means you do not have to use tokens such as { } [ ] ; as you do in C or statements such as "end if" or "end sub" as you do in VB but it means your indents have to be spot on which is bad if you like to just knock out code like I do without worrying too much about structures. Still, Python does let you do more with your Microbit than anything else does and so that's the price to pay. Let's look at the first line in the while True:

 

Degrees=m.temperature() -6

 

You'll remember I said m was referring to microbit library, and here what we are saying in plain English is:"look at the temperature of the chip please and take six degrees off the value. Then assign the result to a variable called Degrees". If you read the first couple of chapters you'll recall that the temperature is inferred from the temperature of the chip rather than a dedicated temperature sensor. It seemed to be about six degrees too high so six got knocked off it. The next line is one I didn't really need but as I was experimenting at the time I chucked it in there and then forgot to change it when I finished. Still, let's look because it's interesting if you're new to coding.

 

string=Degrees

 

All I've done is created a new variable called string and I've given it the same value as the variable called Degrees. You'll recall that Degrees holds a number; the value in degrees centigrade it read from the Microbit chip. So will string. I want to output that number to the LED display on the front of the Microbit. In Blocks Editor there is a block which says "show number" and that lets you directly put a number out to the display. Here we make it a string. A string is a block of text which can be letters, numbers and punctuation marks. We make it appear on the LED display by the following:

 

m.display.scroll(str(string))

 

There's three bits to this. Let's break it down. Firstly there's m.display.scroll(str(string))      and  m.display.scroll( ) is the command to make something readable appear on the LED display. Next there's

m.display.scroll(str(string))  and str turns a variable that holds a numeric into a string. Once it's a string you can't do any more maths operations on it but you can display it. The number we want to display is of course  m.display.scroll(str(string)) which holds the value in degrees centigrade of the temperature of the chip.

 

Instead of string I could have just used Degrees here. I kind of got carried away doing it old-school when I didn't need to; it's a lesson that it's always best to review your code and remove unnecessary steps.

Over the page then we look at pressing button A and how to get it to do some music.

 

 

More >