-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathIMU_ShakeDetector.ino
52 lines (42 loc) · 1.01 KB
/
IMU_ShakeDetector.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
/*
Arduino LSM6DS3 - Shake Detector
This example reads the acceleration values from the LSM6DS3
sensor and turn on the LED D13 when there is a shake of the
device.
The circuit:
- Arduino Uno WiFi Rev 2 or Arduino Nano 33 IoT
created 9 Nov 2019
by Olivier Staquet
This example code is in the public domain.
*/
#include <Arduino_LSM6DS3.h>
void setup() {
// Initialize embedded LED
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
delay(50);
// Initialize IMU
if (!IMU.begin()) {
// Failed to initialize IMU, blink the internal LED
while (1) {
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
}
}
void loop() {
float x, y, z;
// Read acceleration values
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
delay(5);
}
// If the shake is more than 3G, light on the LED for 1 second
if(abs(x) > 3 || abs(y) > 3 || abs(z) > 3) {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
}
}