IAN LANG ELECTRONICS

The next bit is a piece of cake. Possibly a Victoria sponge or a nice Madeira. There isn't any marzipan on it though. It's back to the Basic tab in the toolbox to drag out a "show number" box. It always has a default of 2 for no apparent reason so drag it out:

bbcmicrobit Go Back

An Easy Script - Countup Clocker.

Previous Page shownumber

click it in:

bnumber

The number we actually want it to show is the number stored in the variable countup. So back to the variables tab and find one of these:

countupblock

And click it like this:

thatsit

We're ready to run. On your options bar click run:

 

 

 

 

 

 

 

and click button B on the simulator on the right hand side. If all has gone well it will do this:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Now click button A a known number of times (I suggest five) and then button B again. It should show you the number of times you pressed A. Click A again a known number of times (I suggest another 5) and then B. Now it will tell you how many times you pressed A this time PLUS the number you pressed last time. Hey presto, a countup clocker. Compile it in just the same way we did in the introduction and send it up to your Microbit. Once transferred, test it - A should count on one when you press, B should show you how many presses were made in total and reset at the back should set the count back to zero.

 

 

That works. It does what it says on the can. It isn't terribly posh though and we'd probably get marked down in an exam if we left it like this. So It's time to posh it up a bit. First of all we need a visual indicator to show a button's been pressed. So from the Basic tab in your tools box, add the following three in:

options sim0 poshup1

An explanation is now beholden and here cometh one forth. The first bit, show leds, allows us to switch on as many or as few of the LEDs on the front panel as we like. You can see how they're arranged in a red square on the inside and every time button A gets pressed a red square lights up to let us know that the button's been pressed succesfully. Next, we wait for 20 milliseconds and  then clear the LED panel down so that it's blank again. Hey presto, a blinky.

 

Relying on the reset button on the back is a bit primitive as it gives us no option to reconsider if the button's pressed accidentally. So we need to press A and B at the same time to reset the count and then A for confirm and B for cancel. We need another event handler. It's going to get a bit crowded so let's collapse the existing blocks like this:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

and stick in our new event handler from the inputs tab which is the following:

poshup2 poshup3

In the following we are going to use some logic loops and some if statements. We're also going to use a boolean variable.  Here's the chap:

aandb

He's going to lock out the add-to and show functions whilst we are in reset mode.this will mean we will have a bit of work to do on the other two event handlers afterwards. To get the variable, click in the variables tab in the toolbox. Drag out set item to and change the name. It doesn't actually matter what you call it,

mine's called resQuery because it's RESet QUERYing. It's a good practice to give your variables meaningful names. What does matter is what you set it to. Go to the maths tab and drag out one of these:

they're right at the bottom. Click it into the variable you've just placed (in my case resQuery) and

hover your mouse over the zero and click. When it highlights, overtype to say 1 as above.

 

Our next block is going to be a loop. Here's where we're going and what we're going to fetch:

zero whiledo stickitin

Whilst on the right you can see where it needs to be stuck; notice it's INSIDE the event handler and that true block has been detached. A while...do loop can be made to perform a series of actions, or hang the entire run of the program, whilst a series of arguments is either true or false. You can specifiy the arguments that will break out of or continue the loop and that's what we are going to do next.

logic tab

So now we trot along to the Logic tab and we drag out one of these:

That chap's called an or block and his job is to provide two ways for  the

loop to continue. If neither way is happening, the run will break out of the loop, but if either way is happening, the loop will continue. We need to stick him in the while block like so:

or orcomplete

Now we need to go back to the Logic tab and drag out two of these and drop them in the workspace:

comparator

The block above is known as a comparator and if one side of the equals sign agrees with the other, then it returns a result of true. If not, it returns a result of false. Don't worry if you don't know what I'm talking about because when we've constructed the loop I'm going to go through what it's doing.

The two comparators fit into the or block like this:

comparatorin

Now we need to replace those four zeroes with the arguments we want to put. Over the page we look at them.

Next