BBC micro:bit
Gesture Detection

Introduction

In this project, we extend the functionality of our serial port connection program to allow us to read button presses and gestures on the micro:bit.

For this project, you need to have Visual Basic (any of the free versions) installed on your PC. You also need a serial port driver.

MicroPython

The first that we need is a Python program that will make our micro:bit send readings to the serial port.

This program encodes the accelerometer gestures and button presses in the first 6 bits of a byte of data. This information will be sent as characters but it will be possible for us to perform a neat conversion in Visual Basic.

from microbit import *

while True:
    bite = 0
    if accelerometer.was_gesture("up"):
        bite = bite + 1
    elif accelerometer.was_gesture("right"):
        bite = bite + 2
    elif accelerometer.was_gesture("down"):
        bite = bite + 4
    elif accelerometer.was_gesture("left"):
        bite = bite + 8
    if button_a.was_pressed():
        bite = bite + 16
    if button_b.was_pressed():
        bite = bite + 32
    print(bite)
    sleep(50)

If you can't follow how this program will work, look up how to encode numbers using 8 bit binary. By adding the place values to the variable, we are setting certain bits to equal 1 when the gesture is detected.

Visual Basic - Form Design

This code is built on top of the serial port program mentioned at the start of this section.

VB Program

The output TextBox has been resized and 6 labels have been added. I have messed around with the properties of the labels, changing autosize to false and making some formatting choices. The labels are named as follows,

  • lblA
  • lblB
  • lblUp
  • lblDown
  • lblLeft
  • lblRight

Visual Basic - Programming

The only changes to the program that are needed are in the ShowString() procedure that outputs the data received on the serial port. These changes are as follows,

Sub ShowString(ByVal myString As String)
   txtOutput.AppendText(myString)
   Dim bite As Byte
   Try
      bite = CByte(myString)
   Catch ex As Exception
      Exit Sub
   End Try
   Dim labels() As Label = {lblB, lblA, lblLeft, lblDown, lblRight, lblUp}
   For i As Integer = 5 To 0 Step -1
      If (bite And (1 << i)) <> 0 Then
         labels(5 - i).BackColor = Color.Yellow
      Else
         labels(5 - i).BackColor = Color.White
      End If
   Next
   Application.DoEvents()
End Sub

If you watch the output you get in the TextBox, you will notice that reading serial data is a bit hit and miss. The error handling code is there to make sure that our program does not get into a pickle if the data received are not quite what was expected.

Run the program and make a connection to a micro:bit running the MicroPython program from the top of this page. Pressing buttons and making gestures should result in the correct label lighting up for you.

Button presses are always registered, only one accelerometer gesture can be received at a time.

VB Program

In the screenshot, the byte that was received last was equal to 24. This meant that button A was pressed (16) and the accelerometer was tilted to the left (8).

Challenges

This page is about experimentation. It proves that the concept is workable. You can pick up accelerometer gestures on the micro:bit and send them via USB to a Visual Basic program. That is enough reassurance to make it worth embarking on a more complex Visual Basic project that makes use of the micro:bit as an input device.

If you change the Python program on the micro:bit, you can send the raw readings from the accelerometer and view them in real time in a Visual Basic program.