Index>BBC Micro:bit>BBC Micro:bit Starter Kit>Lesson 9 Use Buttons to Switch Patterns
No related product information!

Lesson 9 Use Buttons to Switch Patterns

2496

  In this lesson, we will carry out an interesting experiment to use the button A and B on the Micro:bit to switch patterns.

9.1 Components to be prepared

image.png

9.2 LED display

The front LED display of Micro:bit consists of 25 5x5 lattice LEDs.

25 LEDs arranged in a 5x5 grid make up the display for showing pictures, words and numbers.

image.png


9.3 Circuit

Connect micro:bit and PC with a Micro USB cable.

image.png


9.4 MakeCode programming

  We will use an online MakeCode Editor to complete the experiment in this lesson, as shown below.

9.4.1 Start programming

(1) Log in to the website

1. You need to enter the URL in the address bar of Google Browser:

https://makecode.microbit.org/

2. After the website is successfully opened, the interface as shown below will appear:

image.png



(2) Import a project

1. In the HOME interface, click the "Import" button to import the external ".hex" file:

image.png


In the pop-up dialog box, select the "Import File", as shown in the following figure:

image.png



Click the "Choose File"

image.png



Find the code file for this lesson:

BBC _Microbit_Kit\Code\Lesson_9\BlockCode

Select the file in ".hex" format and click the Open:


image.png

2. Notice whether the file has been loaded into the following window, and then click the "Go ahead!" button, as shown in the following figure:


image.png

3. Open the file successfully, as shown in the following figure:

image.png


9.4.2 Run the program

1. After the program is written, connect micro:bit and PC with a Micro USB cable.

2. After micro:bit is connected to the computer, you need to first "Pair device". Click the image.png button on the right of image.png in the lower left corner, and then click the image.png option, as shown in the following figure:

image.png


Then click image.png in the lower right corner

image.png


Then the following dialog box will pop up, select image.png, and then click image.png

image.png


After the device is successfully paired, the image.png button changes to image.png

image.png


3. Start to download the program to Micro:bit, and click the image.png button. Generally, the program will be downloaded directly to the Micro:bit. After the download is completed, your Micro:bit will restart and run the program just downloaded.Observe the LED screen of the Micro:bit. If you press the A button on the Micro:bit, you will see a pattern “√”; If you press the B button on the Micro:bit, you will see a pattern “X”. If you press the A and B buttons at the same time, you will see a third pattern, as shown in the following figure:

image.png


[Note]:

1.If Micro:bit doesn't respond after clicking the image.png, you need to click the image.png button on the right of the image.png, and then click the image.png , and observe the situation of the Micro:bit again, as shown in the following figure:

image.png


    2.When you press the button A or button B, if the pattern on the Micro:bit screen has no change, it may be due to the unsuccessful download of your program onto the Micro:bit. So you can download the program onto the Micro:bit again to resolve this problem. If the pattern on the screen still has no change, you need to restart the Micro:bit and then download the program for the third time. 


 If you have problems, please send us an email: support@adeept.com


9.4.3 Learn the code program

In the program, we use the following instruction blocks, which are explained as follows:


Block

Function

image.png 

 This is an instruction block controlled by buttons. You can press the corresponding button such as button A or button B or press the button A and the button B at the same time to implement the instruction block. You can put the instruction blocks needed to run into this button instruction block.


9.5 Python programming

9.5.1 Run the program

1.Connect micro:bit and PC with a Micro USB cable.

2. Open the Mu Editor installed on the computer, and click the button [Load] in the upper left corner to open the source code program of this lesson:

image.png 


Find the code file for this lesson:

BBC _Microbit_Kit\Code\Lesson_9\PythonCode

Select the file in ".py" format and click the Open:

image.png 


3. Click the [Flash] button to download the program to Micro:bit, as shown in the following figure:

image.png 


4.After the program is downloaded, the pattern will be displayed on the LED screen of the Micro:bit.

image.png

 

【Note】:

After you click the [Flash] button, if there is no change on the LED screen of Micro:bit, you need to restart the Micro:bit, and then click the [Flash] button again.

    If you have problems, please send us an email: support@adeept.com


9.5.2 Learn the code program


The source codes are as follows:


1

2

3

4

5

6

7

8

from microbit import *

while True:

    if button_a.is_pressed() and button_b.is_pressed():

        display.show(Image.HEART)

    elif button_a.is_pressed():

        display.show(Image.YES)

    elif button_b.is_pressed():

        display.show(Image.NO)


    (1)
The method of the button_a.is_pressed() can judge whether the button A on the Micro:bit has been pressed. If the button A is pressed, the pattern “√” will be displayed on the LED screen. 


5

6

elif button_a.is_pressed():

    display.show(Image.YES)


(2)The method of the button_b.is_pressed() can judge whether the button B on the Micro:bit has been pressed. If the button is pressed, the pattern “X” will be displayed on the LED screen: 


7

8

elif button_b.is_pressed():

    display.show(Image.NO)


    (3)The method of the button_a.is_pressed() and button_b.is_pressed() can judge whether the button A and button B have been pressed at the same time. If the button A and button B are pressed at the same time, the pattern in the “HEART” shape will be displayed on the LED screen:


3

4

if button_a.is_pressed() and button_b.is_pressed():

    display.show(Image.HEART)


[The way to display other patterns]

In the code program, we use the “Image” library. More graphic schemes and methods of use about the “Image” library are available in the link below:

https://microbit-micropython.readthedocs.io/en/v1.0.1/image.html#attributes