IAN LANG ELECTRONICS
I really, really like hard work. I could watch it all day. One thing I did spend a few minutes watching a while ago was two men doing a traffic survey. One had a stopwatch and the other a countup clocker. Basically, the man with the stopwatch shouted "Go" and the man with the countup clocker pressed a button on it for every car that went past him. About five minutes later the man with the stopwatch shouted "Stop" and the man with the countup clocker pressed another button and shouted out a figure, which the man with the stopwatch wrote down on a piece of paper on a clipboard he had which I'd forgotten to mention until now. Then they reset their gadgets, crossed over and did exactly the same. Then they crossed back over again and did another one. They were still doing it when I came back the other way an hour later.
So let's make the BBC Microbit work like the countup clocker. When we press button A, we want to count up by 1, and when we press B we want to see what the count is. For a reset - there's a reset button on the back, between the USB and battery connectors, so we don't have to worry about that. Spiffing, and here's a script for your consideration:
Now there isn't a lot to this but it does do exactly what was described above. Not too stylishly, but it does it nonetheless.
If you read through the introduction preceding this article, you'll see that we used if statements and a forever loop to make things work. That's a good starting point as if statements are the most important logic tool you can have, but it's a long way round. I'm going to bang on for a couple of sentences about something called event driven code versus something else called line driven code. Chin up, it may be utterly tedious but it's relevant and you will find it useful.
Line driven code goes through a cycle. It does whatever it says to do in a loop and then goes back to the top. You can turn line driven into event driven using if statements but it's a long way round.
Event driven does not go round a loop. The device just polls for an input and then, if it gets an input that matches something it knows, it does the code associated to that event. And that's what we've done here. The big purple bits that say on button A and on button B pressed are event handlers and what's within the boundaries are the bits of code that will run when the event happens.
So before we delve into the code and what it does, let's have a look at how to build it out from the toolbox.
Step 1 cometh forth in haste:
Trot along to the input tab of your toolbox and drag out a handler that says "on Button A pressed" on it and drop it into your workspace. Whilst you're in there drag out another one exactly the same. You'll end up with something like the below:
We need to set one of them to be the event handler for button B and so hover your mouse over the big A on the bottom one and left click to get the below:
And then click on B to get the handler to change to saying "on Button B pressed".
Piece o' cake. So now we have a pair of event handlers, one for each button. They can handle any button-pressed related issues, but they can't do anything about it yet because there's no code blocks in the bondaries. On to step 2.
The first thing we need to do in step 2 is to go in and drag out a variable as below. When you drag them out they always have a default name of "item". Ignore that for now, we'll change it in a mo.
Drag out the above mentioned command block and click it in as in the figure on the right:
Next we are going to rename it. So hover your mouse over the bit that says "item" and left click to get the pop-up as seen on the figure on the right. Once you've done that click on the second one down, Rename Variable......, and you get a dialogue box that looks like the below:
At which juncture you simply type into the text field there whatever you want the variable to be called. As you can see, I'm naming it Countup. The variable changes its name in the command block and as you will see in a moment or two it gets added to a table of variables present in this sketch. But we still haven't told it what to set the variable to. Here comes some maths.
Dib into the maths tab and get one of these things on the right that say 0 + 0 and click it in like the figure below.
0 + 0 is of course rather pointless because rather than counting up it will remain obdurately at zero and furthermore won't change our variable. Let's sort that out.
Hover your mouse over the zero of the block as in the figure at left then click. The zero will highlight and you can overtype it. Type in a 1 and press the enter key.
Back to the variables tab and you'll see that Countup has been added to the table of variables present in this script as I was wittering on about earlier. So drag it out and click it in like the figure below:
Mathematicians would probably call this sort of thing "incrementing by the arithmetic identity" but everybody else just says "adding one on". What we are doing here is adding one on to our count when the button gets pressed.
Before we do the other event handler let's take a moment here to bang on a bit about what a variable is. Essentially it's a temporary storage place, and as the name suggests what's stored in it can change. There are three main types you will encounter. The first is a numeric; it stores numbers and that's what our variable Countup here does. The second is a string; it stores blocks of text. The third is a boolean, sometimes known as a checkflag, which stores either 0 or 1. They're used for checking whether a condition is true (1) or false (0).
We are at the bottom of the page and so click on the button below and we'll crack on and finish the other event handler, then set to poshing our little device up a bit.