杏吧原创

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 鈥淟et鈥檚 code鈥 and find the 鈥淣ew project鈥 section. On one side, there is a menu of blocks, all of which are programming instructions, arranged in categories. Under 鈥淚nput鈥, find the one that says 鈥渙n 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 鈥淏asic鈥, find 鈥渟how string鈥, then click, drag and drop so that it snaps into the middle of 鈥渙n 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鈥檚 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 鈥淰ariables鈥 list, hit 鈥淢ake a Variable鈥 and type 鈥渞andom number鈥 into the box.

Grab an 鈥渙n shake鈥 as before, but this time, slot in the 鈥渟et random number to 0鈥 block from your variables list. Onto the 0 at the end of that block, slot 鈥減ick random 0 to 10鈥, which you鈥檒l find in the 鈥淢ath鈥 section.

Next, we want the program to do different things for different numbers. Under 鈥淟ogic鈥, find an 鈥渋f <true> then鈥 block. Add that to the bottom of the 鈥渙n shake鈥, then slot the logic comparison 鈥0 = 0鈥 between if and then. Replace the first 0 with the 鈥渞andom number鈥 variable from the list you made. Add a 鈥渟how string鈥 block in the space below the 鈥渋f鈥 line, and write a message there.

From now on, click the + sign on your 鈥渋f <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鈥檛 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