Linux Format

Decision Shaker

Leave all of the important decisions in life to your computer friend when build your very own decision engine using a micro:bit.

-

The micro:bit controller is an easyto‑use board that was designed for children taking their first steps with physical computing. In this tutorial we shall use micro:bit to create a gesture-controlled ‘Decision Shaker’, where we ask micro:bit a question, give it a shake and see the answer appear across the LED matrix screen.

We start the project by opening our web browser and visiting https://makecode. microbit.org: this will load the block-based language we shall use to code the project. The screen is split into three: the first column is a virtual micro:bit to simulate our code; the second column shows the palettes of blocks that we can select to write the code; and the final column, also the largest, is the coding area where blocks are placed.

In the coding area, we can already see some blocks. To remove them from the area, drag them to the palette of blocks in column two, and release them. They will disappear. Now let’s go to the Input palette and drag ‘on shake’ to the coding area. This will be the trigger to start our project.

We now need to create a variable that will store the possible responses from our decision shaker. Click on Variables, click Make A Variable and call it “responses”. Now drag ‘set item to 0’ and place it inside ‘on shake’. Change ‘item’ to ‘responses’.

We will now create an array to store the possible responses to our questions. Arrays can be found in the Advanced palette. We need the lower ‘create array with’ block: drag this and connect it to ‘set responses’ so that it fits on top of the ‘0’. You will see that ‘create array with’ has a cog: this means we can edit the number of items in the array. Click on the cog and a new menu appears. Now we need to drag ‘value’ and place it in the top-most slot inside the array immediatel­y to the right: do this until there are five values in the array. To close the menu, click on the cog to return to coding. Now in the empty slots of our array, add a number of responses to questions. (See right for some examples.)

With that completed, go to the Basic palette and drag the ‘show string’ block to place it inside ‘on shake’, underneath the array we have just created. (Look for the small arrow to turn yellow before you release.) This block will show a string of text on the microbit’s 5 x 5 LED matrix. Now we need a way to select the random response to display; so from Arrays, drag the ‘list get value at’ block and place it over the ‘hello’ in ‘show string’. Remember to change ‘list’ to ‘responses’.

Now our code can only choose the first item in our ‘responses’ array, so we need to

“We shall use micro:bit to create a gesture-controlled ‘Decision Shaker’...”

add an element of randomness to the project. In the “Math” palette drag ‘pick random 0 to 4’ and place it over the ‘0’ of ‘get value at’. This will now pick a random number from 0 to 4 each time the code is triggered. Our array has five responses but we select between 0 and 4: this is because the block langue we are using starts counting from 0, so our first item in the array is at position 0, and the fifth is at position 4.

Our code is complete: now plug in your micro:bit, and it should mount as a USB flash device. Click on the Download button in the bottom-left to copy the code to your computer, then drag and drop the downloaded file to your micro:bit, where it will be flashed. You will see an orange LED flash on the micro:bit. Once this has gone off, the code is ready. Ask your micro:bit a question, then give it a shake to reveal the answer!

You can power the micro:bit controller from the official battery pack, or use a USB powerbank to use the project when you’re not at your computer.

 ??  ?? Here we can see all of the code for this project. There isn’t much you need to create, but even this covers variables, arrays and event-driven programmin­g.
Here we can see all of the code for this project. There isn’t much you need to create, but even this covers variables, arrays and event-driven programmin­g.
 ??  ?? Creating an array in your web browser can be tricky: just remember to add new values to the top of the array, not further down.
Creating an array in your web browser can be tricky: just remember to add new values to the top of the array, not further down.

Newspapers in English

Newspapers from Australia