8
PROM: Primary Robotics On Micro:bits Adrian Oldknow [email protected] March 2018 Section 2: Using buttons, gestures and leds In the last section we didn’t use a real micro:bit at all. So now’s the time to plug in a real one and explore some of things it can do. There is useful introduction on the Micro:bit Education Foundation site. There is quite a bit of technical jargon around this picture. So far we have met the 2 buttons and the 25 LED display on the front. You may have tried programming the micro:bit to respond to one or more of the `gestures’ like `shake’. These are detected by a device called an `accelerometer’ which is one of the sensors on the back. In order to create a program in the form which a micro:bit’s processor can understand we have to convert it into a code known as `hex’. That is done using the `Download’ button in MakeCode’s toolbar. We want to save the hex file.

  · Web viewrobot! (This was the Czech ... for a forced labourer!) Let’s now create a useful device – an electronic dice. (Actually `dice’ is a plural word, a single one should

Embed Size (px)

Citation preview

PROM: Primary Robotics On Micro:bits Adrian Oldknow [email protected] March 2018

Section 2: Using buttons, gestures and leds

In the last section we didn’t use a real micro:bit at all. So now’s the time to plug in a real one and explore some of things it can do. There is useful introduction on the Micro:bit Education Foundation site.

There is quite a bit of technical jargon around this picture. So far we have met the 2 buttons and the 25 LED display on the front. You may have tried programming the micro:bit to respond to one or more of the `gestures’ like `shake’. These are detected by a device called an `accelerometer’ which is one of the sensors on the back. In order to create a program in the form which a micro:bit’s processor can understand we have to convert it into a code known as `hex’. That is done using the `Download’ button in MakeCode’s toolbar. We want to save the hex file.

So we have created the hex file called `microbit-Buttons-and-icons.hex’ in the `Downloads’ folder on the computer’s hard-drive. Now we just have attached the micro:bit to the one of the computer’s USB ports using the cable supplied. The big end plugs into the computer, and the little end into the micro:bits `Micro USB Connector’ on the back.

You need to wait a few seconds until the computer recognises the micro:bit as an external memory device, usually called `MICROBIT (D:)’. From the `Downloads’ folder, right-click on the hex file you want to send to the connected micro:bit. Find `Send to’ on the menu and then select `MICROBIT (D:)’. A window will pop up to show you how the download to the micro:bit is progressing, and the yellow LED on the back next to the micro USB connector will flash.

When this stops flashing and shows a steady light then the micro:bit is powered up and ready to go. Now you can check out that the correct things happened when either or both of the buttons is pressed. When you disconnect the micro:bit from the USB cable the LED will go out and the micro:bit has no power. Attach a battery box with two AAA batteries to the battery connector on the back. Now you should find that the program runs all on its own. The technical term for an electronic device which can run its own stored program is called an `autonomous’ device. Another term for this is a robot! (This was the Czech name for a forced labourer!) Let’s now create a useful device – an electronic dice. (Actually `dice’ is a plural word, a single one should be called a `die’.)

We need to be able to pick a random number between 1 and 6 and display it. We also need to be able to generate such a random number whenever we perform a certain action like pressing a button or shaking the device. We will need to use one of the purple `Math’ blocks in the toolbox.

Here is a sample program to get the feel of what we can do with some of the other blocks from the `Basic’ tools. `Forever’ just keeps on repeating the two commands for ever.

`Show number’ does just that. `pause (ms)’ waits the given number of milliseconds before moving to the next command. A pause of 5000 milliseconds means a wait of 5 seconds. But we actually need a number between 1 and 6, not one between 0 and 5. So we’ll introduce the idea of a `variable’ which is a very important tool in computing.

We will set a variable called `die’ to the result of adding 1 to the random number between 0 and 5.

So here is a first go at making an automatic die roller that generates a number every 2 seconds so long as the micro:bit is powered up. Can you adapt it by replacing the `forever’ by an `on’ block so that new numbers are generated when an action is made? Don’t forget to give your program a name.

But a real die has spots, not numbers! Can you see how the following program works?

Add more `if’ statements to produce suitable patterns for scores of 5, 4, 3, 2 and 1. Then you can remove the `show number’ block!

Some games require you to roll a six in order to start. How many rolls do you expect this take? We can carry out an experiment to find out.

First we’ll see a useful idea to help keep your programs tidy. It’s called a `function’. You need to use `Advanced’ from the toolbox to find the `functions’ tools. You can then create a function called `Roll a six’ or any name you like.

This function uses a `while’ loop to keep rolling a die until a 6 comes up. The number of rolls is stored in the variable called `rolls’. We can use a very simple program block to call this function and then to use the variable it produces. Each time you press the `A’ button you will carry out the experiment. The results can vary widely, from just 1 (if you are lucky) to quite a large number.

So let’s adapt this to find the average number of rolls of a set of several experiments. The variable `number’ is used to set the number of rolls in the experiment. The variable `total’ will add up the values of `rolls’ after each experiment. The program uses a counted `repeat’ loop. After each experiment the number of `rolls’ is shown, and then the program waits for 2 seconds. Finally it shows the grand total of the number of rolls over all 10 of our experiments.

In order to find the average number we need to divide the value of `total’ by the value of `number’. This can be done by pressing button `B’.

So here is a sample of the whole experiment. The numbers of rolls were: 7, 8, 8, 1, 3, 9, 7, 11, 8, 1, which total to 63. Pressing button `B’ shows the average as 6. Hang on! 63 divided by 10 is 6.3, so we need to understand something about the way the micro:bit does arithmetic, at least when using the `MakeCode’ editor. In order to work faster and to use less memory, the micro:bit just working with whole number, called `integers’. So dividing 10 into 63 goes 6 times and leaves a remainder of 3. In the `Math’, `… More’ toolbox there is a `remainder’ block.

So below there is a sleeker version of our complete program. Now the `A’ block just runs quickly to show the `total’, and the `B’ block shows the average and the remainder.

Now, how about making an automatic device to play `scissors, paper, stone’?

What other random ideas can you come up with?

Some games, like chess, are played against the clock. Can we turn the micro:bit into a stop-watch? We can use button `A’ to set it running, and button `B’ to stop it and show the elapsed time. We have already found that the micro:bit works with whole numbers, which explains why the Basic `pause’ block works in milliseconds, so we don’t have to use any decimals. This program uses a variable called `time’ which will count the number of seconds elapsed. The loop is a `tested’ loop which checks to see if the button `B’ is being held down. Notice the difference between `on button’ at the start, and `button’ in the third line. Both are in the `Input’ toolbox. `not’ is in the `Logic’ toolbox. The four Basic blue blocks flash the clock every 500 milliseconds, to each pass of the loop takes 1000 milliseconds, or 1 second. When the `B’ button is held down, the flashing stops and the elapsed time in seconds is displayed.

Can you turn this into an egg-timer, which flashes of buzzes after say 3½ minutes (or however you like your egg cooked).

How about making a reaction timer? The program randomly chooses to display an `A’ or `B’ character. You must press the appropriate button. The program displays the time between its display and your response.