Science Oxford Logo

CCC - Moon Buggies

Resources for the 'Moon Buggies' Creative Computing Club.

During this workshop, you will program robot buggies to collect 'moon rocks'.

This page recaps what we discussed as a group.

To skip to the examples and ideas, click here.


Creating your first micro:bit program

Click here to find the starter program, with the imports ready.

Open the Mu editor on your computer. If there is code on the screen, start a New program from the menu.

Mu menu, highlighting 'New'

Type these lines of code into your blank window:

from microbit import *
import radio

display.show(Image.HAPPY)

Plug your micro:bit into the computer, then click Flash:

Mu menu, highlighting 'Flash'

Some code should be running on your micro:bit!

If you get a message scrolling across the micro:bit's screen, this is an error message!

Debug your code, then click Flash again to test your changes.

Once your code is working - change it!

Instead of HAPPY, can you think of another Image the micro:bit could show on its display?


Testing your Moonbuggy

If you are in Science Oxford's workshop, your robot is named after a current astronaut, and has a number.

If you not in a Science Oxford, download MoonBuggy.py, edit the channel numnber, and flash to your robot's micro:bit.

Look at the information on your table to find out more about your astronaut, and find out what number they are - this is your radio channel number.

Add the below code to your program, between import radio and before display.show():

radio.on()
radio.config(channel=0)

Edit the channel number to be your robot's number!

Flash your code to your micro:bit, and check for errors.


Click the 'REPL' button on Mu's menu, this will open a terminal at the bottom of your screen.

Mu menu, highlighting 'Flash'

Turn your micro:bit over and click the Reset button on the back (between the USB socket and the white battery socket).

You can now type commands directly into the terminal, to interact with your micro:bit. Try this:

 >> display.show(Image.YES)

Does your micro:bit's display change?

Now try to send a message to your robot on the moon:

 >> radio.send('forward 500 500')

Did your robot move? Check for error messages in the terminal, and try again.


Practice driving your moon buggy by combining these commands:


Back to Earth

You probably noticed that your robot doesn't obey your commands immediately.

Information takes approximately 1.255 seconds to travel from Earth to the Moon, so if you were really sending commands one at a time to a robot on the Moon, then there would be a delay.

Scientists always test everything out on Earth before sending things to space, as testing is much easier this way!


For the rest of the workshop, you can turn this delay on and off, to make the robot behave as though it is on the Moon or on Earth, depending on what you are doing.

Send this command using the terminal to remove the delay:

radio.send('earth')

What do you think you could send to put the buggy back on the Moon?


Using the Grabber

Your robot has a grabbing claw on the front - this can open and close to varying amounts to help you collect moon rock samples to bring back to base camp.

 >> radio.send('grab 6')
 >> radio.send('grab 1')

The grabbers have 6 position options - 6 is fully open, 1 is fully closed.

Test it out - what options work best for the moon rocks?

Can you successfully transport moon rocks around the surface?


Type more quickly with Functions

Typing every command in one at a time is really slow.

Now that you have practiced with the moon buggies, you know which commands you use most often - you can shorten what you need to write to make this happen, by using functions.

A function is like a recipe - you teach the micro:bit a set of instructions and give these instructions a name. Then you just need to use the name, and the micro:bit knows what to do.


Example 1: shorten radio.send()


def do(message):
    radio.send(message)

The function allows you to replace radio.send with do:

 >> do('forward 500 500')
 >> radio.send('forward 500 500')

This saves you 8 letters of typing each time, which adds up to a lot when you are sending lots of commands.


How it works:


Example 2: combine instructions


def grabber1():
    radio.send('grab 5')
    sleep(10)
    radio.send('forward 500 1000')
    sleep(10)
    radio.send('grab 2')
    sleep(10)

We can now use just one command in the terminal to send three radio messages at once:

 >> grabber1()

This one instruction will open the grabber, drive a small amount forward, then close the grabber again - hopefully picking up a moon rock!


After testing out commands one at time, decide what would be useful to combine together.


What next?