èƵ

How to program a device that plays with random numbers

With a little basic coding, Hannah Joshua shows you how to make a truly smart messaging device that can dispense advice whenever you shake it

BBC micro:bit

New stuff you need
BBC micro:bit starter kit

UNTIL now, everything we have made has been stupid. This week, we are giving a circuit a brain.

To do this, we will use a BBC micro:bit, a tiny entry-level . You write instructions on a computer, then plug in the micro:bit and download them. When you hook the micro:bit up to your circuit, it takes care of what happens when.

Go to the , click “Let’s code” and find the “New project” section. On one side, there is a menu of blocks, all of which are programming instructions, arranged in categories. Under “Input”, find the one that says “on shake” and drag it into the workspace. This will tell the micro:bit to respond when you shake it. Now, we can use other blocks to control that response.

Under “Basic”, find “show string”, then click, drag and drop so that it snaps into the middle of “on shake”. Download it to your micro:bit and you will find that this simple program will make the words you write in this block scroll across the micro:bit’s LED screen when you shake it.

To expand on this, we can make a kind of Magic 8 Ball. On each shake, the micro:bit will show a random message from a list of possibilities. The first step is to make a random number generator. In the “Variables” list, hit “Make a Variable” and type “random number” into the box.

Grab an “on shake” as before, but this time, slot in the “set random number to 0” block from your variables list. Onto the 0 at the end of that block, slot “pick random 0 to 10”, which you’ll find in the “Math” section.

Next, we want the program to do different things for different numbers. Under “Logic”, find an “if <true> then” block. Add that to the bottom of the “on shake”, then slot the logic comparison “0 = 0” between if and then. Replace the first 0 with the “random number” variable from the list you made. Add a “show string” block in the space below the “if” line, and write a message there.

From now on, click the + sign on your “if <true> then” block a few times to add extra messages. Use the same kind of blocks in each section, but change the numbers – so, if random number = 1, type a different message. How many? If your random number generator goes from 0 to 10, you will need a message for each outcome. If you get stuck, you can look at the example below:

Make the messages as wise, or rude, or positive as you like. Maybe use it like a fortune cookie, preprogramming sage advice to receive at random.

There is plenty more you can do with your micro:bit, like make a pedometer or a timer. Engage your imagination and show us on social media.

To download a printable version of this page click here

Thanks to Imperial College Advanced Hackspace for use of their facilities


What you will need next week

If you have collected the parts for previous projects, you should have everything you need

Next in the series

1 Introduction

2 Electric candle

3 Toast notifier

4 Desktop traffic light

5 Propeller car

6 Magic eight ball

7 Theremin: Make a musical instrument you don’t have to touch

8 Sound-sensitive disco ball

9 Rubbish sweeper

10 Biscuit bot

Projects so far and a full list of required kit are at
Email: maker@newscientist.com

Topics: Electronics