2. Lesson 2

../_images/2_1.jpg

2.1. Introduction

Welcome to the Micro: bit Smart Car Tutorial!In this course, we will explore the Micro: bit and learn how to control the Micro: bit car in programming.

2.2. Teaching Objectives

Let student master how to control RGB LED on Micro: bit SMART CAR’s expansion board and it’s function and understand how to play music on buzzer by block programming.

2.3. Control of RGB light (LED) on expansion board

Choose Stemhub: bit > On-board Light one show color red

../_images/2_2.png

option in the yellow box allows you to choose which LED control

option in the red box allows you to choose the color of the LED

../_images/2_3.png

LED 1 to 4 on the expansion box corresponding to “one” to “four”.

../_images/2_4.png

setting the color and brightness,the value for the brightness starts from 0 to 255

2.3.1. Exercise 1: design a program to flash the LED on the expansion board in order.

../_images/2_5.png

Hint: Turn on each RGB light for a period,turn off the previous light before turning on the next light

2.3.2. Exercise 2: design a program to change the brightness of the LED on expansion board start from dim to bright and then bright to dim. (Breathing light effect)

Hint: Using loop and variable to dim the RGB light

../_images/2_6.png

Breathing light effect:

../_images/2_7.png

2.4. Buzzer

Buzzer on the expansion board is the same to the one on Micro: bit,they can control by block module under “Music” category. Notes that when expansion board is plugged to Micro: bit, only buzzer on expansion board will produce melody.

Music > Melody > play melody ♫ … at tempo … (bpm)

../_images/2_8.png ../_images/2_9.png

Choosing the melody in the yellow box, and choose the speed in blue box

../_images/2_10.png

Selecting tune in yellow box, and select bit in the blue box

../_images/2_11.png

Choosing the volume level in yellow box

2.4.1. Delay of the word display

Comparing two programs shown below, is there any different between them?

../_images/2_12.png ../_images/2_13.png

(1)After pressing button A,the word” Sound” will display on Micro: bit’s screen letter by letter. The middle C will be played after all the letters has been displayed.

(2)After pressing button B, the middle c will be played very fast, and then display “Sound” letter by letter. These two events will be executed almost at the same time.

Thus,Micro: bit board can move to the next step only when it finished displaying all the letters. We need to consider this huge amount of delay when we are designing a program. If we want to play the sound and display the letters at the same time, we should put the statement of displaying letter into the last step. 

2.4.2. Exercise: Play a song, design a program to play the song below by your car

1: “Twinkle Twinkle Little Star”

../_images/2_14.png

2: “Jingle Bells”

../_images/2_15.png

2.4.3. Answers

Exercise 1:

After turning on the first light, use the ‘PAUSE’ block module to set the lighting time to 100 milliseconds, and turn off the first light when the second light is turned on. And so on, turn the lights on and off one by one. The forever program makes it flash from left to right continuously.

../_images/2_16.png ../_images/2_17.png

Exercise 2: Breathing light

The whole program is separated into two parts (yellow box) (brighter、dimmer)

1. Initializing the Variable (‘value’) by setting it to 0. Also setting it as the repeating condition.

2. When ‘value’ is smaller or equal to 255:

a.	Changing the color of all the light into orange,brightness of the light will change as ‘value’ change.
b.	Stop for 0.01 second
c.	‘value’ ADD  5

3. When’ value’ larger than 255,the condition for the first yellow box will not be fulfilled.

4. Setting ‘value’ into 255, When ‘value’ is larger than 0:

a.	Changing the color of all the light into orange,brightness of the light will change as’ value’ change.
b.	stop for 0.01 second
c.	‘value’ SUBTRACT 5
d.	Program will end when’ value’ drop to 0.

../_images/2_18.png