Lesson 3 – Selection and Inputs

Learning Outcomes

Objectives:

Must

Store input values from the BBC Micro:bit (Buttons, sensors etc…)

Should

Use if statements to create branching programs based on input values.

Could

Create a thermometer program that gives a warning  based on certain values…

Keywords

Words to learn for this lesson are:

Sensors, Thermometer, Input, Selection, Nested If… If/else

Starter

So far we’ve only looked at lights on the microbit.  It does so very very much more….

Very quickly, make a list of the INPUT devices on the microbit.  What PHYSICAL, real world input does each one react to? Make a table like this…

Input device Triggered by…
Buttons Activated by pressing the button.
Thermometer
Compass…

Main

Now, we have to write code that responds to a CHANGE in the state of the sensor/input device.  In the case of a button, that is when it is pressed.  Try this:

from microbit import *


while True:
    if button_a.is_pressed():
        display.show(Image.PACMAN)

How does this work?
Why do we use “if” here?
The following code uses if/else:

from microbit import *

while True:
    if button_a.is_pressed():
        display.show(Image.PACMAN)
        sleep(200)
    else:
        display.show(Image.GHOST)

Now we have branching code – code which responds to different input. You press the button, something happens. You don’t, you get something else…

So, how about if/else?
Have a look at this code…

 

while True:
    if button_a.is_pressed():
        display.show(Image.PACMAN)
    elif button_b.is_pressed():
        music.play(music.NYAN)
    elif accelerometer.was_gesture('shake'):
        display.show(Image.GHOST)
    elif pin1.is_touched():
        display.show(Image.SURPRISED)

# Connect a speaker to pins 0 and GND to hear the music

# try these images: HEART, MEH, RABBIT, COW, DUCK, SWORD, GIRAFFE
# TARGET, HAPPY, HOUSE, TORTOISE, UMBRELLA, SNAKE, SKULL, BUTTERFLY

# try these musics: ENTERTAINER, PRELUDE, ODE, BIRTHDAY, PYTHON, WEDDING

Challenge 1) Here you’ll hack some headphones into the microbit:

You’ll need some headphones, and two crocodile clips from your teacher, if available.

When you run the code, you’ll see there are reactions to a shake, also to touching pin 1 (the metal part…)

Challenge:

Can you add code to the program so that it will react to these events as well:

microbit.accelerometer.is_gesture(name)  where name is any of:

"up""down""left""right""face up""face down""freefall""3g""6g""8g""shake"

Tough challenge!:

How about making a happy face appear when button a and button b are pressed together?
(Hint: there are two ways of doing this.  Try to check button a.  If it is pressed, check button b.

 

Make sure all that code has annotated screenshots!

 

Plenary

Finally, we’re going to write some code for the built in thermometer:

Before we start, you need to consider one thing:

The thermometer on the micro:bit is embedded in one of the chips – and chips get warm when you power them up. Consequently, it doesn’t measure room temperature all that well. The chip that is used to measure temperature can be found on the left hand side of the back of the micro:bit:

This code will show the temperature:

from microbit import *

while True:
   temp = temperature()
   display.scroll(str(temp) + 'C')
   sleep(500)

You may wish to use the battery pack, so you can use the micro:bit away from the computer.
Challenges:
Using a mixture of the code in this lesson:
1)Write a program that tells us if the temperature is too cold, too hot, or just right.
2)Write a program that tells us if it is hotter than before or colder (clue: think < and >)
3)Write a program that displays half the pixels on the screen when the microbit starts. One row disappears for every degree the temperature drops, and one row is added for every extra degree…
 

 

Homework