Lancaster Air Cadets

345 Sqn Microbits

Cadets learn to code!

Introduction

Microbits were designed specifically to help people learn how to code, and to engage with electronics. These are great skills to have, as once you can control a computer, you can do pretty much anything!

The tutorials for microPython (the variety of Python that runs on the Microbits) is here. This contains a series of short Tutorials, and an API Reference. The API Reference is the important bit, as this is where you can see what the commands are. For example, if you click on Buttons in the API reference, you can see the commands for how to use the buttons; if you click on Display, you can see the commands for how to draw to the display; and so on…

You don’t need to read these ahead of time (though you are welcome to do so if you like!) - it is just important that you know where to look things up when you come to need them! If you want some more ideas for what you can do with your Microbit - you can find some here.

Session 1: Images and Buttons

The basics:

To make your code be able to talk to the microbit - we need to import all of ther functionality from the the microbit library, like this:

from microbit import *

This enables us to access functions such as display (for controlling the display - such as display.scroll or display.show()), and the list of Images (for drawing on the display).

A microprocessor will run every command that you give it once - so you will almost always want an infinite loop. This is achieved with:

while True:
  # anything here happens repeatedly forever

Everything indented below the while statement will run again and again and again… forever! You can learn more about while loops here if you wish, but for now, it is fine to simply understand that while True: causes an infinite loop (we will learn about loops in more detail later on).

Lets get coding:

We started with a simple message that scrolled across the screen, then showed a picture, then went round again:

from microbit import *

# infinite loop!
while True:

    # show the small heart, and sleep for 1/4 second
    display.scroll('Look, a duck!')
    display.show(Image.DUCK)
    sleep(2000)

We then made an animated heartbeat, using a combination of display.scroll, display.show() and sleep().

from microbit import *

# infinite loop!
while True:

    # show the small heart, and sleep for 1/4 second
    display.show(Image.HEART)
    sleep(250)

    # show the small heart, and sleep for 3/4 second
    display.show(Image.HEART_SMALL)
    sleep(750)

Buttons

We then made it so that it shows a skull when the button is not pressed, and the heartbeat when the button is held down.

This relies on an if-else statement, which tells the code what to do IF something happens, and what to do if it doesn’t happen (ELSE) - in the below example, [STATEMENT] would be replaced by a bit of code that can be either True or False:

# This tests if the [STATEMENT] is true, if so, it runs the code indented underneath
if [STATEMENT]:

  # code here for if [STATEMENT] is True

# if the [STATEMENT] was false, then it runs the code underneath this instead
else:

  # code here of if [STATEMENT] is False

You can learn more about if-else statements here. Here is how we used it to control our button - see how we are replacing [STATEMENT] with button_a.is_pressed(), which is a function that returns either True (if the button is currently pressed - meaning the code indented under if will run) or False (if not - meaning that the code indented under else will run).

from microbit import *

# infinite loop!
while True:

  # only do this if the button is currently being pressed
	if button_a.is_pressed():

		# show the small heart, and sleep for 1/4 second
		display.show(Image.HEART)
		sleep(250)

		# show the small heart, and sleep for 3/4 second
		display.show(Image.HEART_SMALL)
		sleep(750)

		# if the button is not pressed
		else:

			#show a skull
			display.show(Image.SKULL)

Session 2: Sensors!

First, we made a magic 8 ball by detecting when you shake the microbit:

from microbit import *
from random import randint

# get a random number
number = random.randint(1, 2)

# infinite loop...
while True:
  
  # detect a shake
  if(accelerometer.was_gesture('shake')):

    # get a new random number (either 1, 2 or 3)
    number = randint(1, 3)

  # say yes if it was a 1
  if(number == 1):
  	display.show(Image.YES)

  # if it wasn't a 1, say no if it was a 2 
  elif(number == 2):
  	display.show(Image.NO)
    
  # if it wasn't a 1 or a 2, it must have been a 3
  else:
  	display.show(Image.SKULL)

Then we used the magnetometer (compass) to make a functioning… er… compass!

# load just the libraries that we are using
from microbit import compass, display, Image

# calibrate the compass once before you start
compass.calibrate()

# infinite loop
while True:

    # worth out which arrow I want from the list
    north = ((360 - compass.heading() + 22) % 360) // 45
    
    # get that arrow from the list and display it
    display.show(Image.ALL_ARROWS[ north ])

Session 3: Coming Soon…