DIYstoX: The Software

This is the third post about a project to build a cave surveying device. You might want to read the previous post before embarking on this one.

So far we've worked out how we can decode readings from the LDM, and how we can wire it up to a microcontroller with a magnetometer and accelerometer, then add an external battery to make it portable. In this post we'll look at the code running on that microcontroller that takes readings from the LDM, magnetometer, and accelerometer and converts them to distance, heading, and inclination respectively.

The Software

The general structure of the software on the microcontroller is illustrated by the process flow diagram on the right.

Since Bluetooth will not always be available the measurements will be stored in an array where they can be sent when possible. The taking of readings from the magnetometer and inclinometer will be triggered by receiving readings over serial from the LDM. This means that only one button, the one on the LDM, needs to be pressed to initiate readings from all of the sensors and add them to a queue for sending.


With a limited amount of memory available on the Feather it is important to ensure that our variables are assigned and used properly. To facilitate this all of our variable initialisation will be done at the start of the program, and we will only access and modify variables in the main running loop.

The downside to this is that it means we need to specify a fixed limit on the number of measurements that can be stored in the array waiting to be sent.

Code available on GitHub

I'm not going to go through the code in detail, as I find it exceptionally tedious, but you can find it on GitHub where it is reasonably well documented. It is currently still being developed - for example it is sending measurements over serial rather than Bluetooth LE , and it's not following the same communication protocol as the DistoX2. Keep an eye out for updates as I keep working on it.

What's next?

So far we've managed to get our microcontroller to take readings from the magnetometer and 3-axis accelerometer when it receives readings from the LDM.

The next step is to work out how to turn those magnetometer and accelerometer measurements into a compass heading and inclination reading, which will require some calibration. We'll explore the simple maths behind those calculations in the next post.


Previous Post Next Post