7. Lesson 9 and Lesson 10

../_images/9_1.jpg

7.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.

7.2. Teaching Objectives

Let student master the cooperation and driving between Micro: bit Smart Car and controller and understand how the controller and car can communicate through radio waves.

7.3. Controller

Introduction of Micro: bit controller:

../_images/9_2.png

This controller can only be able to use with Micro: bit. The controller has multiple functions, including a joystick, 4 programmable buttons, a buzzer, and a vibration motor.

1) The joystick has a detection function of up, down, left, and right, and can also be pressed as another button.

2) The 4 buttons with different colors are red X, green Y, blue A, and yellow B. They correspond to the p13, p14, p15, and p16 pin respectively.

3) The buzzer and the vibration motor have a switch connection, they will be connected to p0 when the switch is on the left, and when the switch is on the right, each time we press the 4 programmable buttons will make the buzzer sound and vibrate.

7.4. Radio and our daily life

Radio communication is a communication method that uses electromagnetic signals instead of wires, optical fibers, and other media to transmit information across space. Nowadays, wireless communications are commonly used in smart phones, Wi-Fi, and even satellites to transmit data via radio waves.

../_images/9_3.png

The principle of micro: bit radio communication is based on radio waves. The micro: bit has an antenna on the back for data transmission between two micro: bit boards

../_images/9_4.png

7.5. Radio programming module

Download the controller expansion pack:

1) Enter the following link into the yellow box https://github.com/lzty634158/GHBit

../_images/9_5.png

2) Add GHBit to makecode

../_images/9_6.png

If you want to use the controller to control the trolley, you need to install a Micro: bit on each of the controller and the trolley and load their respective programs.

The controller and the car use radio waves for communication, so two Micro: bit need to establish a radio connection.

../_images/9_7.png ../_images/9_8.png

setting up a wireless communication module: (in the radio category)

To set the radio group, enter a number (0~255) in the yellow box

../_images/9_9.png

Set the strength of the radio (1-7)

../_images/9_10.png

(In an open area and there are not many computers nearby, the broadcast area can reach up to 70m)

radio transmit string signal

../_images/9_11.png

When string received, do{...}

(Upon receiving the string signal, the program in the building block will only start when the string signal is received)

../_images/9_12.png

7.5.1. Example 1: Two-way radio

Display numbers sent by broadcast

../_images/9_13.png ../_images/9_14.png

7.6. Program module for controller and the car

Block for controller:

../_images/9_15.png

Joystick blocks, select the joystick up, down, left, right and press actions in the yellow box.

../_images/9_16.png

The blocks for buttons, select the buttons B1~B4 in the yellow box, and select the action of pressing or releasing the button in the blue box

(Because radio waves have no directivity, different pair of cars and controller need to set up a separate set of radios to avoid interference)

7.6.1. Programming for controller:(GHBit)

(Joystick)

The red box is for the joystick, and the yellow box is for the button

../_images/9_17.png

Programming for the car:

The red box is for the joystick, the yellow box is for the button

B1: stop moving                            
B2: Display a heart shape    
B3: The car moves toward left      
B4: The car move toward right                                 

../_images/9_18.png

7.7. Exercise 1

Try adding a function to one of the buttons to make the LED lights on the expansion board lights up and emit different colors.

Hint 1: Change the program of the car, the controller program does not need to be changed

Hint 2: Use functions

7.8. Exercise 2

Try changing the answer of exercise 1, cancel the function of the "B1" button, and let it automatically pause the car

Hint: use ‘... else’

7.9. Answers

7.9.1. Exercise 1

The program for the controller refers to above

Programming for the car:

../_images/9_19.png

7.9.2. Exercise 2

Controller

../_images/9_20.png

Car

../_images/9_21.png