parent
ab4d31de08
commit
e62232101b
@ -0,0 +1,40 @@ |
|||||||
|
Accelerometer Example |
||||||
|
===================== |
||||||
|
|
||||||
|
In this example we will see how to use the accelerometer of the micro:bit. The |
||||||
|
accelerometer can, for instance, be used to know which way the micro:bit is |
||||||
|
oriented. |
||||||
|
|
||||||
|
Code |
||||||
|
==== |
||||||
|
|
||||||
|
To get the acceleration value for all axes, we will just call the function |
||||||
|
`MicroBit.Accelerometer.Data`. This function returns a record with `X`, `Y` |
||||||
|
and `Z` field giving the value for each axis. |
||||||
|
|
||||||
|
|
||||||
|
```ada |
||||||
|
declare |
||||||
|
|
||||||
|
Data : MMA8653.All_Axes_Data; |
||||||
|
-- A variable to store the accelerometer data |
||||||
|
begin |
||||||
|
|
||||||
|
-- Read Accelerometer data |
||||||
|
Data := Accelerometer.Data; |
||||||
|
end; |
||||||
|
``` |
||||||
|
|
||||||
|
We can then use the value in the record to get some information about the |
||||||
|
orientation of the micro:bit. For example, if the Y value is below `-200` |
||||||
|
the micro:bit is vertical. |
||||||
|
|
||||||
|
Note that the type used to store the values of the accelerometer is declared in |
||||||
|
the package `MMA8653` (the driver), so we have to `with` and `use` this package |
||||||
|
to be have acces to the operations for this type. |
||||||
|
|
||||||
|
```ada |
||||||
|
if Data.Y < -200 then |
||||||
|
-- The micro:bit is vertical |
||||||
|
end if; |
||||||
|
``` |
||||||
@ -0,0 +1,26 @@ |
|||||||
|
with "../../../boards/MicroBit/microbit_zfp.gpr"; |
||||||
|
|
||||||
|
project Jumper is |
||||||
|
|
||||||
|
for Runtime ("ada") use Microbit_Zfp'Runtime ("Ada"); |
||||||
|
for Target use "arm-eabi"; |
||||||
|
for Main use ("main.adb"); |
||||||
|
for Languages use ("Ada"); |
||||||
|
for Source_Dirs use ("src"); |
||||||
|
for Object_Dir use "obj"; |
||||||
|
for Create_Missing_Dirs use "True"; |
||||||
|
|
||||||
|
package Compiler renames Microbit_Zfp.Compiler; |
||||||
|
|
||||||
|
package Linker is |
||||||
|
for Default_Switches ("ada") use Microbit_Zfp.Linker_Switches & ("-Wl,--print-memory-usage", "-Wl,--gc-sections"); |
||||||
|
end Linker; |
||||||
|
|
||||||
|
package Ide is |
||||||
|
for Program_Host use ":1234"; |
||||||
|
for Communication_Protocol use "remote"; |
||||||
|
for Connection_Tool use "pyocd"; |
||||||
|
end Ide; |
||||||
|
|
||||||
|
end Jumper; |
||||||
|
|
||||||
@ -0,0 +1,123 @@ |
|||||||
|
------------------------------------------------------------------------------ |
||||||
|
-- Template -- |
||||||
|
------------------------------------------------------------------------------ |
||||||
|
|
||||||
|
with LSM303; use LSM303; |
||||||
|
|
||||||
|
with MicroBit.Display; |
||||||
|
with MicroBit.Accelerometer; |
||||||
|
with MicroBit.Console; |
||||||
|
-- with MicroBit.IOs; |
||||||
|
with MicroBit.Time; |
||||||
|
with nRF.GPIO; use nRF.GPIO; |
||||||
|
|
||||||
|
use MicroBit; |
||||||
|
-- use MicroBit.IOs; |
||||||
|
|
||||||
|
|
||||||
|
procedure Main is |
||||||
|
|
||||||
|
-- PROGRAM VARIABLES |
||||||
|
Data : LSM303.All_Axes_Data; |
||||||
|
-- Value : MicroBit.IOs.Analog_Value; |
||||||
|
FreeFallCounter : Integer; |
||||||
|
FreeFallDetectionCycles : Integer; |
||||||
|
FreeFallCondition : Boolean; |
||||||
|
Threshold : Axis_Data; |
||||||
|
|
||||||
|
-- IO VARIABLES |
||||||
|
DO_1_Pt : GPIO_Point; |
||||||
|
DO_2_Pt : GPIO_Point; |
||||||
|
DO_3_Pt : GPIO_Point; |
||||||
|
DO_4_Pt : GPIO_Point; |
||||||
|
DO_Conf : GPIO_Configuration; |
||||||
|
|
||||||
|
|
||||||
|
begin |
||||||
|
|
||||||
|
-- PROGRAM STARTUP |
||||||
|
Console.Put_Line ("Jumper - starting up .."); |
||||||
|
FreeFallCondition := False; |
||||||
|
FreeFallCounter := 0; |
||||||
|
FreeFallDetectionCycles := 70; -- 70 * 5ms = 350ms |
||||||
|
Threshold := 150; -- acceleration sensor threshold |
||||||
|
|
||||||
|
-- IO CONFIGURATION |
||||||
|
DO_1_Pt := MB_P2; |
||||||
|
DO_2_Pt := MB_P8; |
||||||
|
DO_3_Pt := MB_P9; |
||||||
|
DO_4_Pt := MB_P16; |
||||||
|
DO_Conf.Mode := Mode_Out; |
||||||
|
DO_Conf.Resistors := No_Pull; |
||||||
|
DO_Conf.Input_Buffer := Input_Buffer_Connect; |
||||||
|
DO_Conf.Sense := Sense_Disabled; |
||||||
|
|
||||||
|
-- APPLY IO CONFIGURATION |
||||||
|
DO_1_Pt.Configure_IO(DO_Conf); |
||||||
|
DO_1_Pt.Clear; |
||||||
|
DO_2_Pt.Configure_IO(DO_Conf); |
||||||
|
DO_2_Pt.Clear; |
||||||
|
DO_3_Pt.Configure_IO(DO_Conf); |
||||||
|
DO_3_Pt.Clear; |
||||||
|
DO_4_Pt.Configure_IO(DO_Conf); |
||||||
|
DO_4_Pt.Clear; |
||||||
|
|
||||||
|
loop |
||||||
|
|
||||||
|
-- Read the accelerometer data |
||||||
|
Data := Accelerometer.Data; |
||||||
|
|
||||||
|
-- Print the data on the serial port |
||||||
|
Console.Put_Line ("X:" & Data.X'Img & ASCII.HT & |
||||||
|
"Y:" & Data.Y'Img & ASCII.HT & |
||||||
|
"Z:" & Data.Z'Img); |
||||||
|
|
||||||
|
-- FREEFALL DETECTION |
||||||
|
-- check wether all axis are sub-threshold => increment counter |
||||||
|
if abs(Data.X) < Threshold and |
||||||
|
abs(Data.Y) < Threshold and |
||||||
|
abs(Data.Z) < Threshold then |
||||||
|
FreeFallCounter := FreeFallCounter + 1; |
||||||
|
-- otherwise reset counter |
||||||
|
else |
||||||
|
FreeFallCounter := 0; |
||||||
|
end if; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- FREEFALL DETECTION |
||||||
|
if FreeFallCounter >= FreeFallDetectionCycles then |
||||||
|
-- set free fall condition |
||||||
|
FreeFallCondition := True; |
||||||
|
end if; |
||||||
|
|
||||||
|
-- Clear the LED matrix |
||||||
|
Display.Clear; |
||||||
|
|
||||||
|
-- Check, wether FreeFallCondition was triggered |
||||||
|
if FreeFallCondition then |
||||||
|
-- set outputs |
||||||
|
DO_1_Pt.Set; |
||||||
|
DO_2_Pt.Set; |
||||||
|
DO_3_Pt.Set; |
||||||
|
DO_4_Pt.Set; |
||||||
|
Display.Display ('X'); |
||||||
|
else |
||||||
|
Display.Display ('0'); |
||||||
|
end if; |
||||||
|
|
||||||
|
|
||||||
|
-- Read analogue pin (could be 0,1,2,3,4, or 10) |
||||||
|
-- Value := MicroBit.IOs.Analog (2); |
||||||
|
-- Console.Put_Line ("Value : " & Value'Image); |
||||||
|
|
||||||
|
-- Set output |
||||||
|
-- if Value > Analog_Value(200) then |
||||||
|
-- MicroBit.IOs.Set (12, True); |
||||||
|
-- else |
||||||
|
-- MicroBit.IOs.Set (12, False); |
||||||
|
-- end if; |
||||||
|
|
||||||
|
Time.Sleep (5); -- 200 samples / s |
||||||
|
end loop; |
||||||
|
end Main; |
||||||
Loading…
Reference in new issue