Now that we've used devices to access the Microbit Display, the next step is to look at the other sensors in the Microbit, the simplest of which is the accelerometer. Using it in Sniff is really easy:
make i2c device
make sensor mma8652 device
make accX number
make accY number
make accZ number
The microbit talks to the internal sensors using i2c (pronounced eye-squared-see). While this can be a bit intimidating, its really just like USB - a way of connecting the computer bit with some kind of peripheral. The Accelerometer plugs into the i2c, just like a mouse plugs into USB, so the first thing we need is an i2c device, so we can talk to devices using it.
Then we make a sensor. The Microbit accelerometer is a chip called an "mma8652". In theory that's what is written on the top of the chip, but its too small for me to read! There's nothing particularly special about this - its just the number of the part that they chose to use. There are other chips which do the same job, and in fact you could connect one to the microbit and have two different accelerometers. Similarly you could connect an mma8652 to an Arduino or other board. That's why Sniff calls it by its proper name, rather than just "accelerometer".
when start
.forever
..tell sensor to "read"
..say [accX]
..say [accY]
..say [accZ]
..say ""
..wait 1 secs
Having made we can now just tell it to read, and we get back the acceleration in x,y, and z!
You'll see that if you hold it still and flat, then the x and y values are small/almost zero, while the Z value is either 1 or -1. That's because of physics! The force of gravity results in an effect which is exactly the same as if you were accelerating upwards at 9.8m/s/s. Imaging being pushed back into your seat in a car (or better a plane!) as it accelerates. It feels exactly the same as if you were just lying down, and facing upwards!
That means that the most common use of an accelerometer isn't to measure acceleration, but to figure out which way is down.
If we take the x/y acceleration and just plot it we can tell if the microbit is flat:
make display microbitDisplay device
make displayX number
make displayY number
make displayColor number
make i2c device
make sensor mma8652 device
make accX number
make accY number
make accZ number
when start
.forever
..tell display to "tick"
when start
.
.forever
..tell sensor to "read"
..set displayColor to 000
..tell display to "clear"
..set displayColor to 777
..set displayX to 3
..set displayY to 3
..tell display to "move"
..tell display to "set pixel"
..change displayX by -accX*5
..change displayY by accY*5
..tell display to "draw"
..wait 0.1 secs
Here's the whole code, which uses what we learn in the last session do to drawing, and combines it with what we've learnt about the accelerometer. The only gotcha is that accX is reversed - that's just down to the way that the accelerometer is placed on the board, relative to the screen.
This actually shows a really effective use for the accelerometer as a pseudo joystick.The microbit only has two buttons which isn't enough to control a game, but using the accelerometer you can control a character in a game by tilting the board around.
No comments:
Post a Comment