-
-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathMotionDetect.ino
79 lines (64 loc) · 2.81 KB
/
MotionDetect.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Copyright (c) 2016 Intel Corporation. All rights reserved.
See the bottom of this file for license terms.
*/
/*
This sketch example demonstrates how the BMI160 accelerometer on the
Intel(R) Curie(TM) module can be used to detect motion events
*/
#include "CurieIMU.h"
bool blinkState = false; // state of the LED
unsigned long loopTime = 0; // get the time since program started
unsigned long interruptsTime = 0; // get the time when motion event is detected
void setup() {
Serial.begin(9600); // initialize Serial communication
while(!Serial) ; // wait for serial port to connect.
/* Initialise the IMU */
CurieIMU.begin();
CurieIMU.attachInterrupt(eventCallback);
/* Enable Motion Detection */
CurieIMU.setDetectionThreshold(CURIE_IMU_MOTION, 20); // 20mg
CurieIMU.setDetectionDuration(CURIE_IMU_MOTION, 10); // trigger times of consecutive slope data points
CurieIMU.interrupts(CURIE_IMU_MOTION);
Serial.println("IMU initialisation complete, waiting for events...");
}
void loop() {
// if motion is detected in 1000ms, LED will be turned up
loopTime = millis();
if(abs(loopTime -interruptsTime) < 1000 )
blinkState = true;
else
blinkState = false;
digitalWrite(13, blinkState);
}
static void eventCallback(void){
if (CurieIMU.getInterruptStatus(CURIE_IMU_MOTION)) {
if (CurieIMU.motionDetected(X_AXIS, POSITIVE))
Serial.println("Negative motion detected on X-axis");
if (CurieIMU.motionDetected(X_AXIS, NEGATIVE))
Serial.println("Positive motion detected on X-axis");
if (CurieIMU.motionDetected(Y_AXIS, POSITIVE))
Serial.println("Negative motion detected on Y-axis");
if (CurieIMU.motionDetected(Y_AXIS, NEGATIVE))
Serial.println("Positive motion detected on Y-axis");
if (CurieIMU.motionDetected(Z_AXIS, POSITIVE))
Serial.println("Negative motion detected on Z-axis");
if (CurieIMU.motionDetected(Z_AXIS, NEGATIVE))
Serial.println("Positive motion detected on Z-axis");
interruptsTime = millis();
}
}
/*
Copyright (c) 2016 Intel Corporation. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/