|
| 1 | +with MMA8653; |
| 2 | +with MicroBit.I2C; |
| 3 | +with LEDs; |
| 4 | +with Ada.Real_Time; |
| 5 | + |
| 6 | +procedure Accelerometer is |
| 7 | + Acc : MMA8653.MMA8653_Accelerometer (Port => MicroBit.I2C.Controller); |
| 8 | +begin |
| 9 | + if not MicroBit.I2C.Initialized then |
| 10 | + MicroBit.I2C.Initialize (S => MicroBit.I2C.S400kbps); |
| 11 | + end if; |
| 12 | + |
| 13 | + if not Acc.Check_Device_Id then |
| 14 | + for R in LEDs.Coord'Range loop |
| 15 | + for C in LEDs.Coord'Range loop |
| 16 | + LEDs.Set_One_LED (R, C); |
| 17 | + end loop; |
| 18 | + end loop; |
| 19 | + delay until Ada.Real_Time.Time_Last; |
| 20 | + end if; |
| 21 | + |
| 22 | + Acc.Configure (MMA8653.Two_G, |
| 23 | + MMA8653.High_Resolution, |
| 24 | + MMA8653.High_Resolution); |
| 25 | + |
| 26 | + loop |
| 27 | + declare |
| 28 | + Detection_Limit : constant := 75; |
| 29 | + Data : constant MMA8653.All_Axes_Data := Acc.Read_Data; |
| 30 | + use type MMA8653.Axis_Data; |
| 31 | + use type Ada.Real_Time.Time; |
| 32 | + begin |
| 33 | + LEDs.Clear_All_LEDs; |
| 34 | + if Data.X > Detection_Limit then |
| 35 | + LEDs.Set_One_LED (3, 1); |
| 36 | + elsif Data.X < -Detection_Limit then |
| 37 | + LEDs.Set_One_LED (3, 5); |
| 38 | + elsif Data.Y > Detection_Limit then |
| 39 | + LEDs.Set_One_LED (1, 3); |
| 40 | + elsif Data.Y < -Detection_Limit then |
| 41 | + LEDs.Set_One_LED (5, 3); |
| 42 | + else |
| 43 | + LEDs.Set_One_LED (3, 3); |
| 44 | + end if; |
| 45 | + delay until Ada.Real_Time.Clock + Ada.Real_Time.Milliseconds (100); |
| 46 | + end; |
| 47 | + end loop; |
| 48 | +end Accelerometer; |
0 commit comments