3.2.6 Smart Light Following Dog


3.2.6.1 Overview

Img

Here we build a smart light following dog with a Micro:bit V2 board, a servo expansion board, a 360° LEGO servo and some LEGO parts. We program to control the dog to chase light through the Micro:bit 5×5 LED matrix.

Img


3.2.6.2 Materials Required

Img


3.2.6.3 Installation Steps

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img

Img


3.2.6.4 Code Blocks

1. if()…else if()…else…

When the condition in “if()” is satisfied, codes under “then” will execute. Or, When the condition in “else if()” is satisfied, codes under “then” will execute. Otherwise, codes under “else” will run.

Img

2. Read the light level through the 5×5 LED matrix on the Micro:bit V2 board, ranging from 0 (dim) to 255 (bright).

Img


3.2.6.5 Test Code

The threshold 10 and 150 in “if()…else if()…” can be modified according to actual conditions.

Img

⚠️ Special reminder: You can obtain the sample code through the following two methods.

Method 1: drag and drop code blocks to build the sample code

1. MakeCode programming environment

The online version of MakeCode: MakeCode editor

2. MakeCode extension library

⚠️ Special reminder: Copy and paste the link: https://github.com/keyestudio2019/pxt-creative-inventors-kit-master.git into the search box to import the library.

Img

3. Write the sample code

Img

Method 2: download the sample code file directly

1. Download the code file

Click to download the code file: 3_3_6_Smart_Light_Following_Dog

2. MakeCode programming environment

The online version of MakeCode: MakeCode editor

3. Load the sample code

Drag and drop the sample code to the MakeCode editor.

Img

Img


3.2.6.6 Test Result

Img

Wire up and power on through Micro USB cable, and connect an external power source (four AAA-1.5V batteries installed in the battery box, ensuring sufficient power).

To download the sample code using an online browser, you need to send the downloaded “.hex” file to the Micro:bit V2 board. There are two ways as follows:

① Download through WebUSB

For browsers like Google Chrome / Microsoft Edge, you need to click “Connect Device” to pair the device first. After that, click “Download” to load the sample code to the Micro:bit V2 board.

Img

② Download without WebUSB

For browsers like Safari / Firefox / Other, download and send the sample code to the Micro:bit V2 board.

Img

After downloading the sample code, unplug the Micro USB cable to the Micro:bit V2 board.

Turn the DIP switched to the RGB and ON.

Img

Shine a light source (such as your phone’s flashlight) onto the 5×5 LED matrix on the Micro:bit V2 board.

If the light level ≥ 150 detected by the Micro:bit V2 board, the light-following dog steps back with the light, these SK6812 RGB LEDs emit red light, and the 5×5 LED matrix displays Img.

If 10 < light level < 150, the light-following dog follows the light, these SK6812 RGB LEDs emit cyan light, and the 5×5 LED matrix displays Img.

If the light level ≤ 10, the light-following dog stops, and these SK6812 RGB LEDs and the 5×5 LED matrix turn off.

Img

Img