0% found this document useful (0 votes)
9 views9 pages

A2 Worksheet - Input

This document provides a worksheet for Year 9 students on physical computing using the micro:bit. It includes programming tasks that involve using the accelerometer, light sensor, and buttons to create interactive applications. Students are guided through coding exercises that demonstrate how to read sensor inputs and control the micro:bit's display based on user interactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

A2 Worksheet - Input

This document provides a worksheet for Year 9 students on physical computing using the micro:bit. It includes programming tasks that involve using the accelerometer, light sensor, and buttons to create interactive applications. Students are guided through coding exercises that demonstrate how to read sensor inputs and control the micro:bit's display based on user interactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Year 9 – Physical computing Activity worksheet

Lesson 2 – Bare bones

Input
Example .
The program below displays a ‘happy face' whenever the accelerometer detects that
the micro:bit is lying face up.

1 from microbit import *


2 while True:
3 if accelerometer.is_gesture("face up"):
4 display.show(Image.HAPPY)
5 else:
6 display.clear()

You need to flash a program to your micro:bit, to see it run.

Notes
● The code is nested in a while loop with a True condition, which means it will be
repeated forever. This is a common pattern.

Page 1 Last updated: 01-06-21


Year 9 – Physical computing Activity worksheet
Lesson 2 – Bare bones

● In line 3, accelerometer is an object that represents the micro:bit’s movement


sensor and is_gesture is a method that corresponds to one of the possible actions
you can perform on the display (in this case, checking for a specific gesture).
method
object method
arguments

accelerometer . is_gesture ("face up")

You can find a reference with all the available accelerometer methods at: microbit-
micropython.readthedocs.io/en/latest/accelerometer.html
The documentation also includes tutorials on detecting movement and gestures:
microbit-micropython.readthedocs.io/en/latest/tutorials/movement.html
microbit-micropython.readthedocs.io/en/latest/tutorials/gestures.html

Task 1 . Light sensor


The 5⨉5 LED matrix can also be used as an input device. The read_light_level
method of the display senses the amount of light falling on the display.

display.read_light_level() Returns an integer between 0


and 255 representing the
light level falling on the
display. Larger means more
light.

Source: https://microbit-micropython.readthedocs.io/en/latest/display.html

Step 1
Copy the program below in your development environment.

1 from microbit import *


2 while True:
3 light = display.read_light_level()
4 display.scroll(light)

Page 2 Last updated: 01-06-21


Year 9 – Physical computing Activity worksheet
Lesson 2 – Bare bones

5 sleep(100)

Flash the program to your micro:bit, to see it run. You will see successive light level
values scrolling through the micro:bit’s display.

Step 2
Remove the lines that display the light values, as you no longer need them. Replace
them with the statements below, that blink a square on the display.

- display.scroll(light)
- sleep(100)

+ delay = .
+ display.show(Image.SQUARE)
+ sleep(delay)
+ display.clear()
+ sleep(delay)

Fill in the incomplete statement so that the delay between successive blinks is 3 times
the light value obtained from the sensor.
Question: When do you expect to see fast blinking? Will it be when there is little light or a
lot of light? Explain your answer.

little, because the light can't be bright if it's blinking fast

Step 3
Flash the program to your micro:bit, to see it run. Place your hand over the display and
start moving it closer and further away to investigate what happens. Try covering the
micro:bit or shining a light at it.

from microbit import *


Page 3 Last updated: 01-06-21
Year 9 – Physical computing Activity worksheet
Lesson 2 – Bare bones

x = 0
while True:
if button_b.was_pressed():
x = x + 1
display.show(x)\

Task 2 . Buttons
Step 1
Copy the program below in your development environment.

1 from microbit import *


2 x = 0
3 while True:
4 if button_b.was_pressed():
5 x = x + 1
6 display.show(x)

Question: Can you briefly describe what you expect this program to do?

whenever the button B is pressed , it add one number to x and display it

Step 2
Flash the program to your micro:bit, to see it run. Press button B on the micro:bit a few
times to investigate what happens.

Note: If is_pressed had been used instead of was_presed, then the multiple button presses
would be detected over and over again, making the program difficult to use.

Page 4 Last updated: 01-06-21


Year 9 – Physical computing Activity worksheet
Lesson 2 – Bare bones

Question: What is the initial value of the x variable? In which line of the program is that
initial value assigned to x?

0,
line 2

Question: How is the value of the x variable modified when button B is pressed? Which
line of the program modifies the value of x?

+1, line 4 and 5

Step 3
Extend your program so that the value of x is decremented (reduced by 1) when
button A is pressed.

Step 4
Instead of displaying the value of x, modify your program so that it lights up the pixel at
column x, in the middle row of the 5⨉5 LED display.

- display.show(x)

+ display.clear()
+ display.set_pixel(x, , 9)

You will need to fill in the y-coordinate of the middle row. If you are wondering about the
purpose of the clear method, investigate what happens when you don’t include it.

⚠ If the value of the x-coordinate in set_pixel is below 0 or above 4, you will see
an error message scrolling on the display.

Step 5
Extend your program to perform the appropriate checks so that the value of x is never
reduced below 0 or increased above 4.

Page 5 Last updated: 01-06-21


Year 9 – Physical computing Activity worksheet
Lesson 2 – Bare bones

You also have the option to:

● reset x to 0 whenever it is increased above 4 and


● reset x to 4 whenever it is reduced below 0

Task 3 . Tilt
Step 1
Replace the conditions in the if statements that check if the dot on the display should
move left or right. Instead of checking if a button was pressed, your program will check
if the user performed the ‘gesture’ of tilting the micro:bit to the left or to the right.

- if button_a.was_pressed():

+ if accelerator.was_gesture( ):

- if button_b.was_pressed():

+ if accelerator.was_gesture( ):

Fill in the incomplete statements with the appropriate gesture. You will find the list of
recognised gestures in the documentation:
microbit-micropython.readthedocs.io/en/latest/accelerometer.html

Flash the program to your micro:bit, to see it run and check if it works correctly.

Note: You need to tilt the micro:bit in the appropriate direction at around 45° for these gestures
to be detected.

Note: If is_gesture had been used instead of was_gesture, then the same gesture would
be detected over and over again, making the program difficult to use. However, in order to
detect a second tilt in the same direction, you will need to restore the micro:bit to a horizontal
position and then tilt again.

Step 2
Extend the program so that the dot on the display can move across rows as well, by
tilting the micro:bit forwards and backwards. You will need to introduce a y variable to
keep track of the dot’s current row.
Page 6 Last updated: 01-06-21
Year 9 – Physical computing Activity worksheet
Lesson 2 – Bare bones

Step 3 [optional]
If you remove the statement that clears the display between successive dot movements,
you will create a trail of dots. You will be able to draw images on the display by tilting!

- display.clear(x)

Explorer task . Binary


Any button can be regarded as a binary digit. When the button is pressed, it corresponds
to 1, otherwise it corresponds to 0.
The two buttons on the micro:bit can represent a 2-bit binary number. The table below
contains the four possible binary numbers that can be represented, along with the
equivalent decimal number.

Button A Button B

place value place value decimal


2 1 number

0 0 0

0 1 1

1 0 2

1 1 3

Page 7 Last updated: 01-06-21


Year 9 – Physical computing Activity worksheet
Lesson 2 – Bare bones

Copy the code below into your development environment and complete it so that the
decimal value displayed is equivalent to the 2-bit binary number represented by the
micro:bit’s buttons.

1 from microbit import *


2 while True:
3 decimal = 0
4 if button_a.is_pressed():
5 .
6 if button_b.is_pressed():
7 .
8 display.show(decimal)

Explorer task . Plotter (Mu editor only)


Copy the program below in your development environment. This is identical to the code
from Task 1.

1 from microbit import *


2 while True:
3 light = display.read_light_level()
4 display.scroll(light)
5 sleep(100)

Replace line 4. Instead of displaying the value of light on the 5⨉5 LED matrix, your
program will print that value.

- display.scroll(light)

+ print((light,))

Page 8 Last updated: 01-06-21


Year 9 – Physical computing Activity worksheet
Lesson 2 – Bare bones

⚠ Flash the program to your micro:bit. You will see nothing displayed, but the
program is running and obtaining values from the light sensor.

Activate the plotter tool. The values of the light variable that are being printed will be
displayed on a rolling graph. This is an excellent way to visually inspect numerical sensor
inputs.

Resources are updated regularly — the latest version is available at: ncce.io/tcc.

This resource is licensed under the Open Government Licence, version 3. For more
information on this licence, see ncce.io/ogl.

Page 9 Last updated: 01-06-21

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy