|
9 | 9 | */
|
10 | 10 | #include "CurieIMU.h"
|
11 | 11 |
|
12 |
| -boolean blinkState = false; // state of the LED |
13 |
| -unsigned long loopTime = 0; // get the time since program started |
14 |
| -unsigned long interruptsTime = 0; // get the time when zero motion event is detected |
| 12 | +boolean ledState = false; // state of the LED |
15 | 13 | void setup() {
|
16 | 14 | Serial.begin(9600);
|
17 | 15 | while(!Serial); // wait for the serial port to open
|
18 | 16 |
|
19 | 17 | /* Initialise the IMU */
|
20 |
| - blinkState = CurieIMU.begin(); |
| 18 | + CurieIMU.begin(); |
21 | 19 | CurieIMU.attachInterrupt(eventCallback);
|
22 | 20 |
|
23 | 21 | /* Enable Zero Motion Detection */
|
24 |
| - CurieIMU.setDetectionThreshold(CURIE_IMU_ZERO_MOTION, 1500); // 1.5g=1500mg |
25 |
| - CurieIMU.setDetectionDuration(CURIE_IMU_ZERO_MOTION, 25); // 25s |
| 22 | + CurieIMU.setDetectionThreshold(CURIE_IMU_ZERO_MOTION, 50); // 50mg |
| 23 | + CurieIMU.setDetectionDuration(CURIE_IMU_ZERO_MOTION, 2); // 2s |
26 | 24 | CurieIMU.interrupts(CURIE_IMU_ZERO_MOTION);
|
27 | 25 |
|
| 26 | + /* Enable Motion Detection */ |
| 27 | + CurieIMU.setDetectionThreshold(CURIE_IMU_MOTION, 20); // 20mg |
| 28 | + CurieIMU.setDetectionDuration(CURIE_IMU_MOTION, 10); // trigger times of consecutive slope data points |
| 29 | + CurieIMU.interrupts(CURIE_IMU_MOTION); |
| 30 | + |
28 | 31 | Serial.println("IMU initialisation complete, waiting for events...");
|
29 | 32 | }
|
30 | 33 |
|
31 | 34 | void loop() {
|
32 |
| - //if zero motion is detected in 1500ms, LED will be turned up |
33 |
| - loopTime = millis(); |
34 |
| - if(abs(loopTime -interruptsTime) < 1500) |
35 |
| - blinkState = true; |
36 |
| - else |
37 |
| - blinkState = false; |
38 |
| - digitalWrite(13, blinkState); |
| 35 | + // if zero motion is detected, LED will be turned up. |
| 36 | + digitalWrite(13, ledState); |
39 | 37 | }
|
40 | 38 |
|
41 | 39 | static void eventCallback(void){
|
42 | 40 | if (CurieIMU.getInterruptStatus(CURIE_IMU_ZERO_MOTION)) {
|
43 |
| - interruptsTime = millis(); |
| 41 | + ledState = true; |
44 | 42 | Serial.println("zero motion detected...");
|
45 | 43 | }
|
| 44 | + if (CurieIMU.getInterruptStatus(CURIE_IMU_MOTION)) { |
| 45 | + ledState = false; |
| 46 | + if (CurieIMU.motionDetected(X_AXIS, POSITIVE)) |
| 47 | + Serial.println("Negative motion detected on X-axis"); |
| 48 | + if (CurieIMU.motionDetected(X_AXIS, NEGATIVE)) |
| 49 | + Serial.println("Positive motion detected on X-axis"); |
| 50 | + if (CurieIMU.motionDetected(Y_AXIS, POSITIVE)) |
| 51 | + Serial.println("Negative motion detected on Y-axis"); |
| 52 | + if (CurieIMU.motionDetected(Y_AXIS, NEGATIVE)) |
| 53 | + Serial.println("Positive motion detected on Y-axis"); |
| 54 | + if (CurieIMU.motionDetected(Z_AXIS, POSITIVE)) |
| 55 | + Serial.println("Negative motion detected on Z-axis"); |
| 56 | + if (CurieIMU.motionDetected(Z_AXIS, NEGATIVE)) |
| 57 | + Serial.println("Positive motion detected on Z-axis"); |
| 58 | + } |
46 | 59 | }
|
47 | 60 |
|
48 | 61 | /*
|
|
0 commit comments