MakeCode sample projects for Bitty Data Logger

micro:bit code

Bitty Data Logger can capture and chart accelerometer, magnetometer, temperature data and data from connected, external sensors. Your device (e.g. a micro:bit) will need the right code for Bitty Data Logger to work properly.

Code examples for you to learn from and adapt to your needs are available in the Bitty Data Logger code repository.

Find the right sample for your needs, create a MakeCode project, switch to JavaScript view and simply paste the code from our repository, into your MakeCode project.

Let's review a couple of examples:

AUTO capture mode and accelerometer data

            let connected = 0
            bluetooth.onBluetoothConnected(function () {
                connected = 1
                basic.showString("C")
            })
            bluetooth.onBluetoothDisconnected(function () {
                connected = 0
                basic.showString("D")
            })
            bluetooth.startAccelerometerService()            
        

When you create your project, you must remember to add the Bluetooth extension from within the Advanced menu on the left. For ease of use, it is also recommended that you go into your project settings and switch off pairing (unless you know you wish to have security, in which case select from one of the other two pairing options).

This is very simple code, as you can see. We have two events handlers, one to respond to a Bluetooth connection being accepted from anotyher device, like a smartphone (onBluetoothConnected) and one for when the connection is lost (onBluetoothDisconnected).

The only other significant code is the line bluetooth.startAccelerometerService() which includes the Bluetooth accelerometer service. This is a code block which takes care of sampling from the accelerometer and transmitting the readings over Bluetooth, all automatically.

USER Code capture mode and general data

            let connected = 0
            bluetooth.onBluetoothConnected(function () {
                connected = 1
                starttime = input.runningTime()
            })
            bluetooth.onBluetoothDisconnected(function () {
                connected = 0
            })
            let message_type = 0
            let timestamp = 0
            let starttime = 0
            let value1 = 0
            let value2 = 0
            let value3 = 0
            let value4 = 0
            
            bluetooth.startUartService()
            let message = pins.createBuffer(11);
            
            function transmitData() {
                timestamp = input.runningTime() - starttime
                message.setNumber(NumberFormat.Int8LE, 0, message_type);
                message.setNumber(NumberFormat.Int32BE, 1, timestamp);
                message.setNumber(NumberFormat.Int16BE, 5, value1);
                message.setNumber(NumberFormat.Int16BE, 7, value2);
                message.setNumber(NumberFormat.Int16BE, 9, value3);
                bluetooth.uartWriteBuffer(message)
            }
            basic.forever(function () {
                if (connected == 1) {
                    message_type = 1
                    // random numbers used solely for demonstration purposes. Acquire your data here as required.
                    value1 = Math.randomRange(0, 1023)
                    transmitData()
                    message_type = 2
                    // random numbers used solely for demonstration purposes. Acquire your data here as required.
                    value1 = Math.randomRange(0, 1023)
                    transmitData()
                    message_type = 3
                    // random numbers used solely for demonstration purposes. Acquire your data here as required.
                    value1 = Math.randomRange(0, 1023)
                    transmitData()
                    message_type = 4
                    // random numbers used solely for demonstration purposes. Acquire your data here as required.
                    value1 = Math.randomRange(0, 1023)
                    transmitData()
                }
                basic.pause(1000)
            })            
        

bluetooth.startUartService() creates the Bluetooth UART service. This service allows data to be sent as messages to the connected device and is the way 'general data' is sent to Bitty Data Logger since the release of version 4.0.

let message = pins.createBuffer(11) creates a 'buffer'. This is an area of memory which can contain (in this case), 11 bytes of data. We use the buffer to formulate messages in the required format before transmitting them over Bluetooth.

In the forever block, we loop... forever and in this case we generate general data item values as random numbers. You would probably acquire those values from somewhere else, perhaps reading from an analog pin to which a sensor has been connected. The type of data to be transitted to Bitty Data Logger is identified by a message type value. This is then set and a variable corresponding to the general data item # (1 - 4) has the value stored in it. We then call a function called transmitData() which completes the formulation of the message in the buffer and transmits it using the Bluetooth UART service.