Getting Started with Offline C++ on the BBC Micro:Bit
May 21, 2016
Show all

Create a basic BBC Micro:Bit Air Mouse


We’ve been testing out the BBC Micro:Bit and C++ for a few days now trying out different APIs and recently playing around with the Micro:Bit’s accelerometer. Check out the project below where we develop a basic air mouse to control a PC.

First off, we are building this offline in the Yotta C++ environment, there is no reason why this wouldn’t work in an online editor. We’ll be using Microsoft Visual Basic .NET for a simple Serial reader to get the values of the Micro:Bit accelerometer and set the mouse to the X and Y coordinates.

The BBC Micro:Bit Code

#include "MicroBit.h"
#define PROGRAM_TICK 100
MicroBit uBit;

void setup() {

void loop() {
   int dx = uBit.accelerometer.getX();
   int dy = uBit.accelerometer.getY();
   uBit.serial.printf("%i|%i\r\n", dx, dy);

int main() {
 while(1) {

As you can see above, we create a basic loop with a delay of 100ms, every 100ms the program runs the loop() function and gets the values of the accelerometer. We then output this to the USB serial port with a delimiter being the vertical line.

That’s it for the BBC Micro:Bit code, we’re just sending the X and Y coordinates over the COM port and reading them in our VB.NET application.

The Visual Basic .NET Application

Imports System.Windows.Forms
Public Class Form1
    Dim com As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM4")
    Dim screenWidth As Integer = Screen.PrimaryScreen.Bounds.Width
    Dim screenHeight As Integer = Screen.PrimaryScreen.Bounds.Height
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        com.ReadTimeout = 100
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim strArr
            Dim data As String = com.ReadLine()
            strArr = data.Split("|")
            Dim newX = Math.Round((strArr(0) - -1024) / (1000 - -1000) * (screenWidth - 0) + 0)
            Dim newY = Math.Round((strArr(1) - -1024) / (1000 - -1000) * (screenHeight - 0) + 0)
            System.Windows.Forms.Cursor.Position = New Point(newX, newY)
        Catch ex As Exception

        End Try

    End Sub
End Class

The Timer1 interval is set to 100ms.

That’s the whole VB.NET application right there. We select a COM port to listen on (COM4) and we open a connection to it as soon as the program has launched. We split the received data into an array, we then perform some basic scaling of the values relative to the resolution of the screen. We then set the position of the mouse X and Y coordinate with the new scaled value.

There you have it! – Move the Micro:Bit around in your hand to the left or right and the cursor should follow suit, move the Micro:Bit up or down and the cursor will move up and down.

This is a very basic Air Mouse! You can adjust the scaling calculations to suit, or increase the Micro:Bit 100ms delay and the VB.net timer interval to get a better resolution when moving the cursor.

Leave a Reply

Your email address will not be published. Required fields are marked *