Close
Simone Seagle

Simone Seagle

Independent Web and Educational Software Developer

Read More

Search

Prev Next
Fall Ball Updates
Blog

Fall Ball Updates

April 25, 2016 Simone Seagle

I've gotten my Fall-Ball project to the point where it's plotting the data in real time!

For some time now I've had the KL46Z successfully broadcasting its accelerometer data out using a Bluetooth dongle. The code to do that was pretty simple, and it also shows how the data was formatted.

int main()
{
    init();
    while (true) {
        if (timer.read() > UPDATETIME) {

            // Read the acceleration on all three axes
            float accData[3];
            accelerometer.getAccAllAxis(accData);

            bluetooth.printf("x:%f y:%f z:%f\n\r", accData[0], accData[1], accData[2]);

            // Just show the x acceleration on the LCD screen for debugging purposes
            sprintf (lcdData,"%4.2f", accData[0]);  

            LCDMessNoDwell(lcdData);

            timer.reset();
        }
    }
}

I used Node.js for the server so I could read the Bluetooth serial port. There's a sweet little serial library that you can install with npm that works really nicely. My first implementation of the program used Python, which was kind of a pain because there's no timer built in AND the python serial library doesn't have events. I wanted this program to be event-driven so that the end-user would have a better experience.

The code to parse the data from the serial port is also fairly simple:

// When the serial port gets data, parse it and send it out to the clients as an object with properties for x, y, and z
fallBallPort.on('data', function(data) {

    var x_acc, y_acc, z_acc;
    var acc = data.split(' ');

    if (acc.length != 3) return;
    x_acc = acc[0].substring(3, acc[0].length);
    y_acc = acc[1].substring(2, acc[1].length);
    z_acc = acc[2].substring(2, acc[2].length);

    console.log("x:"+x_acc+", y:"+y_acc+", z:"+z_acc);
    io.sockets.emit('data', {x:x_acc, y:y_acc, z:z_acc});
});

I use the Node.js socket.io library to send data out to all the clients. Right now I'm running in on my computer, but shortly I will be installing all this on a Raspberry Pi.