Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

The push buttons to control an action

As previously mentioned in this chapter, the MicroBit


is controlled by 2 push buttons.

Push button A.

Push Button B.

In your daily life, name objects you use that are


controlled by push buttons:
The lift - the TV remote control.

A project to understand more

Look at the script built on the right and then answer


the following questions:

What happens the moment you run the script?

The word “Hello” is dislpayed.

If no buttons are pressed, the leds will show the


shape of: a heart

If the button A is pressed, the leds will show the


shape of: the letter A

If the button B is pressed, the leds will show the


shape of: the letter B

Project

Launch the software MakeCode and build the script


that allows to:

Reproduce a musical note if the button A is pressed.

Display “1” if the button B is pressed.


Right Click | Grade 6

Display “2” if no buttons are pressed.

9
Name the variable then click on

3 new blocks are created:

The variable name.

The block is used to initialize


the value of a variable.

The block is used to change


the value of the variable.

A project to understand more

Look at the script built on the right and then answer


the following questions:

What is the name of the variable used in the script?


count

Once the script is launched, the variable is


initialized to the value 0

What are the numbers displayed on the MicroBit’s


screen? 0 1 2 3 4 5 6 7 8 9

What happens when the variable reaches the value


10? the value of the variable will be reseted to 0
Project

Launch the software MakeCode and build the script


that allows you to:

Create two variables a and b.

Display the values of the variable a from 3 to 6.

Display the values of the variable b from 7 to 2.

Watch the tutorial on the digital platform


Right Click | Grade 6

1- Access your account on www.zero1.education


2- Open the Digital Resources tab to watch the tutorial.

11
Chapter 1: MakeCode for MicroBit | Lesson 5

Objectives 1- Creating a project with two variables

A project with two variables

In this project, we will create a game that consists of


the following:

Number of players: 2.

dice
Write on two cards the values 1 and 2.

Reverse the two cards in order to start the game.

Each player will pick a card and press the push


button of the Microbit a number of times equal to
The winner is!!
the number picked. (Player 1 use the button A and
player 2 use the button B)
The game will end when one of the two players
reaches the value 10. As a result, a message will
be displayed announcing the winner.

How many variables should we use?

Let us answer the following questions in order to


determine how many variables we should use in the
project.

How many users can play the game? 2

Can the name of a variable change? YES NO

Can the content of a variable change? YES NO

Can 2 values be in the same variable? YES NO

As a conclusion to be memorized!

A variable can’t hold more than one value at the same


time; that’s why, a variable should be created for each
player.
a b
The variable has a unique name and can change its
content any moment in the script.
Let us create a variable named “a” for the first
player.
And a variable named “b” for the second player.

12
Ready to start?

As a first step, let us proceed in creating the two


variables “a” and “b”.

The two variables are created.

In the block “on start”, insert the two blocks as


shown.

In order to initialize the two variables a and b, set


their values to 0

A script for each button

Two scripts should be built for each player in order to


control the game. Look at the script built for button A.

The blocks inside the “on button A pressed” block


will be executed once:
The button A is pressed

The value 1 should be added in the block


“change a by” in order to increase the value of the
variable “a” by 1.

The block “if” is added in order to:


Display the name of the winner

What value should I type in the “if” block?


10

What happens when the variable “a” reaches


the value 10? The letter A is displayed on the
MicroBit’s screen announcingthe name of the
winner.
Project
Now that you built the script for the button A, it is
time to program the button B and to start playing the
game.
Watch the tutorial on the digital platform
Right Click | Grade 6

1- Access your account on www.zero1.education


2- Open the Digital Resources tab to watch the tutorial.

13
the blocks to control a MicroBit‘s led

In order to control the leds of the MicroBit, follow the


steps below:

Click on the category “Led”.

Discover the different blocks to control the leds.

The block will illuminate the led


(0,0)

The block will make the led


(0,0) blink.

The block will turn off the


led (0,0)

It is time to try

Try to build the Script shown on the right and


describe what happens once it is executed.

The leds of the first row will illuminate in sequence


delayed of one second between a led and another.

Project

Now that you built the script to illuminate the first


column of the leds, it is time to create the script to
illuminate the first and last rows of the leds.
Watch the tutorial on the digital platform
Right Click | Grade 6

1- Access your account on www.zero1.education


2- Open the Digital Resources tab to watch the tutorial.

15
Chapter 1: MakeCode for MicroBit | Lesson 7

Objectives 1- Integrating a variable to control the MicroBit’s leds

Quick review

From the previous lesson, we learned that each led of (0,0)


the MicroBit’s screen has a unique address.
(0,2)
In this lesson, we will learn how to control each led
using a variable and the loop block. (1,3)

Write the address (x,y) of each led of the screen


(3,2)
shown on the right.
(4,3) (1,4)

In order to make the leds turn on and off without referring to the manual method (used in the
previous lesson), a variable should be declared, increased or decreased according to the loop block.

Flashing leds... The project

In order to create a flashing led project, follow the


steps below:

Create a variable named “led”.

From the category Loops, insert the block “for


index from 0 to 4”.

From the category Led, insert the block “plot x - y”.

Replace the block with the block

Insert the block in the “x” field as shown.

Insert the block “pause” to slow down the blinking


effect.

As a conclusion!

When the Script runs:

The for loop repeats the instructions 5 times.

Each turn of the loop, a led will illuminate.

16
Completing the project

Add another loop as shown in the script in order to


turn off the leds in the same order they were turned
on.
The second loop will include the blocks to turn off
the leds of the first row.

Illuminating 25 leds

Now it is time to illuminate the 25 leds of the MicroBit


screen.
The previous Script illuminates the leds of the first
columns (address of the column 0).
Now it is the time to learn how to create an embedded
loop in order to illuminate the different columns of the
MicroBit’s screen.

Your time to try

Create two variables: ledx and ledy then insert in


the block “on start” the two blocks “set ledx to 0”
and “set ledy to 0” as shown.

The two blocks “set ledx to 0” and “set ledy to 0”


are used to initialise the value of the 2 variables
to 0

Examine how the two loop blocks are built and how
the two variables are used in order to assure the
functioning of the Script.

Watch the tutorial on the digital platform


Right Click | Grade 6

1- Access your account on www.zero1.education


2- Open the Digital Resources tab to watch the tutorial.

17

You might also like