Chapter 8 of KitiBot-Microbit

From Waveshare Wiki
Jump to: navigation, search

Bluetooth Control

nRF51822 is the main control chip of micro:bit, this is a BLE chip, which features Bluetooth function for micrro:bit. We could use our phone to control KitiBot by Bluetooth.

Bluetooth connection


To communicate KitiBot with phone, you need to install a APP first. (The APP could only support Android phone)

Except the APP, you also need to add bluetooth package to makecode.

Add bluetooth package.gif

Add Bluetooth package, it will prompts that the package is conflict with radio, neopixel and so on. You can detect those package and add it. After adding, a Bluetooth class can be used in blocks area.

Now, we can being our first Bluetooth code

Bluetooth code MK.gif

Open Bluetooth accelerometer service, button service, led service, temperature service at begin. If there is Bluetooth device, a character "C" will be displayed, "D" is displayed if there is not Bluetooth connected.

We download this code to micro:bit and access with our phone, try to control it.

Holding A and B buttons, press reset button, LED matrix wil display "PAIRING MODE!" and enter pairing mode.

Pairing mode.png

Open Bluetooth service of your phone, search BBC micro:bit and connect it. (The strings in the end are different from different micro:bit)

Search bluetooth on phone.png

【Note】 Every time you restart micro:bit you need to pair again, and before re-pairing, you need to delete the paired micro:bit devices on your phone

Paired decies on phone.png

"√" will be displayed in LED matrix if pairing successfully.

Open the APP we installed before, choose "FIND FAIRED BBC MICRO:BIT(S)" to search the paired micro:bit

App search microbit device.jpeg

App search microbit device-2.jpeg

Note that the micro:bit could only be connected when "BBC micro:bit(BONDED)" is showed in app instead of "BBC micro:bit[tozug](BONDED)", otherwise, it will failed. If you find your connect micro:bit failed, please search again after "√" icon disappears.

App search microbit device-3.jpeg

If Bluetooth connecting successfully, control icons will be showed in app and character "C" displayed on LED matrix of micro:bit.

App search microbit device-4.jpeg

Now we could control micro:bit with the app. Open Accelerometer, roll your micro:bit, then you can find the micro:bit icon on app will roll as well.

App accelerometer.jpeg

Open Buttons, press A or B button on your micro:bit, you can find that the corresponding icons on app will light green, and show "Pressed", if you long press buttons, led will light red and show "Long Pressed" at the same time.

Short press button.jpeg Long press button.jpeg

Open LEDs, edit the LED matrix and click SET DISPLAY, the led matrix on micro:bit will light on as what you set on app. If you click SEND TEXT, it will display the string you send.

LEDs matrix setting app.png

Open temperature, the app will display current temperature measured by micro:bit

Temperature on app.jpeg

Open Device Information, current device information of micro:bit will be showed

Device information on app.jpeg

【Note】The first code we made just opens some services, those services which don't be opened cannot be used in app.

Bluetooth connecting-2


We have learned how to connect micro:bit via Bluetooth, but the way is a little complex. Here we try to simplify it by modifying codes.

Bluetooth code modify.gif

Click Settings-> project settings. Choose No Pairing Required:Anyone can connect via Bluetooth. With this setting, pairing isn't required anymore

No Pairing setting.png

Download the code to micro:bit. And set the app, click settings.

Setting app.png Setting app-2.jpeg

Uncheck Filter the checkbox of "unpaired micro:bit from scan results?"

After setting, you can reach micro:bit devices, and click "BBC micro:bit[tozug]" to connect.

App search microbit device-5.jpeg

【Note】If you didn't delete paired devices as we said before, the device listed in app is "BBC micro:bit[tozug](BNONDED)", you need to delete paired devices and research

App search microbit device-6.jpeg

Control KitiBot via Bluetooth


There is an icon looks like game joystick in app, we can use it to control KitiBot

Game joystick icon on app.png

Connect micro:bit, and click the joystick icon, the control page is as below:

Joystick control page on app.jpeg

Click setting on right top of app to enter setting page.

If states of buttons change, signals will be sent to micro:bit to toggle Gamepad events. Different event values stand of different button events.

Buttons event sent.jpeg

We use default setting. add an event handle code for Gamepad events. The event source is MES_DPAD_CONTROLLER_ID, event value is MICROBIT_EVT_ANY, different key value relate to different button event. Then we could control KitiBot by detect key values.

Bluetooth code event.gif

Bluetooth code event-2.png

Download the code to micro:bit and connect it to Bluetooth. You can control the robot to move by joystick in APP. Press to move and release to stop.

Control servo


At last section, we have tried to control robot via Bluetooth, here we add servo control to make robot shake.

Bluetooth code shake.gif

Bluetooth code shake-2.png

Control RGB LED


We add RGB LED control for flickering effect

Bluetooth code LED.gif

400px-Bluetooth control flowled2.png

Same like the last code, it use a variable temp for color.

400px-Bluetooth control flowled3.png

Set hue to 180 on start. Hue can be set in range 0~360 for different color.

400px-Bluetooth control flowled4.png

If temp is not equal to 0, it means that button is pressed and it will change hue value and show it.

Note: range of hue is 0~360, it will add 360 if hue is less than 0 and minus 360 if it is bigger than 36, to let the hue keeps in 0~360

400px-Bluetooth control flowled5.png

Related Pages