Use this spirit level to quickly and easily display the tilt of any object attached!
Created by Kaitlyn from Raffles Institution.
Learn to read tilt with micro:bit's built-in accelerometer.
Learn to work with micro:bit's 5x5 LED Display!
1 x BBC micro:bit
1 x Micro USB cable
2 x AA Batteries
1 x Double AA Battery Pack
Pre Coding: Connect your Micro:Bit
Before we begin writing the code, we need to decide what we want to achieve with the program and in what order should each component run.
For the electric spirit level, the steps that we will take in the code for each loop are:
We start by defining variables needed as shown. A breakdown of a few variables are:
As the 5x5 LED matrix can only display so much information, the actual tilt values will not be useful for display.
Instead, a function tiltExtent() takes the parameter num, which refers to the tilt value from the accelerometer, and converts these tilt values (num) to tilt levels from 0 to 4.
0 indicates no tilt in the given direction and 4 indicates very large tilt, while -1 is returned when there is an error.
Here, tiltBoundary and tiltSensitivity are used as the boundary values between tilt levels.
The two functions checkRoll() and checkPitch() write the tilt levels obtained from tiltExtent() into tiltList for the roll (left-right) and the pitch (forward-backward) axes respectively.
Before using the tilt values, we calibrate them using a zeroed value for both pitch (zeroPitch) and roll (zeroRoll) obtained from a calibration function written later.
As the accelerometer readings are negative for both left and forward tilt, we need to use the Math.abs() function to obtain the modulus of the negative value to be given to the tiltExtent() function as a parameter for these two directions.
Having obtained the tilt levels in tiltList we can now write the led plotting functions for the different cases that can arise, namely
Using the plotting functions from the three cases in step 4, we can now plot the actual LED matrix for the different possible combinations of tilt levels. As the three functions in step 4 do not discriminate with direction, we need to adjust the coordinate values passed to the LED matrix to plot the LEDs in the right directions.
Step 6: Write Calibration Functions
Having completed the bulk of the code, we now add in the calibTilt() and the resetTilt() functions.
calibTilt() allows users to tare the tilt to zero at the micro:bit's current position
resetTilt() resets the calibration of the board to its original state.
We add a simple function checkState() to check whether the tilt levels have changed from a previous iteration.
If there is no change in tilt levels from a previous iteration i.e. stateChange == 0, we can directly move on to the next iteration and skip the plotting of the LED matrix, reducing computation needed.
Now we can finally place all the necessary functions into the micro:bit's infinite loop to run it repeatedly.
Firstly, we set button A and B on the micro:bit to the calibTilt() and resetTilt() functions respectively using input.onButtonPressed(), and plot a tick on the LED matrix when calibration is completed.
Next run the necessary functions according to our code flow in Step 0 and check for a state change (meaning that there has a change in the tilt of micro:bit since the last iteration).
If there is a change in tilt levels i.e. stateChange == 1, the code will update prevState to the new tilt levels and set stateChange back to 0 for the next iteration, and plot the updated tilt levels on the LED matrix using PlotResult().
Flash the completed code to your micro:bit .
Attach your micro:bit and the battery pack securely to any object and it is ready for use!
Have fun with your electric spirit level! And while you're at it, why not try to extend the capabilities of the tilt sensor or even turn it into a game?
This article is from TINKERCADEMY.