Bit Level
Let's use Webduino Bit to make a level.
Webduino Bit has a gyroscope, which can be used to detect the angles of X-axis and Y-axis of the board. It has also a LED matrix, which can reflect those angles. So we can combine these two features to make a level. Let's go!
Gyroscope
Webduino Bit has an MPU-9250 chip. The MPU-9250 contains a 3-axis gyroscope which can be used to detect the angles of X-axis (roll) and Y-axis (pitch). Refer to the following picture.
Let's create a simple blockly program. Code Link
This program can detect the roll and pitch values. Its JS code is as follows.
var mpu9250;
boardReady({board: 'Bit', device: 'bit77730e', transport: 'mqtt', multi: true}, function (board) {
board.samplingInterval = 250;
mpu9250 = getMPU9250(board);
mpu9250.on(webduino.module.MPU9250Event.ANGLE_MESSAGE, function () {
document.getElementById("demo-area-01-show").innerHTML = (['row:',mpu9250.angVals[0],("<br/>"),'pitch:',mpu9250.angVals[1]].join(''));
});
});
2
3
4
5
6
7
8
9
10
LED Matrix
Webduino Bit has a 5x5 LED matrix. Let's try to create a simple blockly program to light a single LED. Code Link
Let's see its JS code.
var matrix;
boardReady({board: 'Bit', device: '1234', multi: true, transport: 'message', window: window.top.frames[0]}, function (board) {
board.samplingInterval = 250;
matrix = getMatrix(board, 4, 25);
matrix.setColor((4 - 1 + ( 2 - 1) * 5 ), '#33ff33');
});
2
3
4
5
6
7
Note that in blockly program, the range of row and column is from 1~5. While in JS code, its index is from 0~4. This is why you see the formula: ((4-1)+(2-1)5). It means the LED position of (4, 2) has index (3, 1) and position number (3+15=8).
Combined gyroscope and LED
Now we can combine the above codes of gyroscope and LED maxtrix to make a Bit level. I add a little logic to quantize the roll and pitch values to the range 1~5. The codes and result are shown in the following. Note that to see the correct result, you need to connect the Bit board to Wi-Fi first. Then you can jump into the jsFiddle and change the Device ID to yours. After that, just run the codes. That's it!
Exercise
Modify the Device ID to yours, and add some descriptions to the jsFiddle page.