2
2
Arduino LSM6DS3 - Accelerometer Tap
3
3
4
4
this code is to detect tap
5
-
6
5
using IMU.accelerationAvailable()
7
6
8
7
*/
@@ -15,39 +14,29 @@ void setup() {
15
14
while (!Serial);
16
15
17
16
while (!IMU.begin ()) {
18
-
19
17
Serial.println (" Failed to initialize IMU!" );
20
-
21
- delay (3000 ); // wait for 3 sec and check if it can be initialize again
22
-
18
+ delay (3000 ); // wait for 3 sec and check if it can be initialized again
23
19
}
24
-
25
20
}
21
+ float tapThreshold = 0.05 ; // 0.05 g acceleration in some direction is considered as tap. it can be change for the required sensitivity.
26
22
27
- float tapThreshold = 0.05 ; // 0.05g acceleration in some direction is considered as tap. it can be change for the required sensitivity.
28
-
29
- int down = 3 ; // signifing the direction of which is facing downward 1 for x axis ; 2 for y axis ; 3 for z axis;
23
+ int down = 3 ; // signifying the direction of which is facing downward 1 for x axis ; 2 for y axis ; 3 for z axis;
30
24
31
25
void loop () {
32
-
33
26
float x, y, z;
34
-
35
27
if (IMU.accelerationAvailable ()) {
36
-
37
28
IMU.readAcceleration (x, y, z);
38
-
39
- if ((x > tapThreshold || x < -tapThreshold) && down != 1 ) {
40
-
29
+
30
+ if ((x > tapThreshold || x < -tapThreshold) && down != 1 ) {
41
31
Serial.println (" Tap detected across X-axis" );
42
32
}
43
- if ((y > tapThreshold || y < -tapThreshold) && down != 2 ) {
44
-
33
+
34
+ if ((y > tapThreshold || y < -tapThreshold) && down != 2 ) {
45
35
Serial.println (" Tap detected across Y-axis" );
46
36
}
47
- if ((z > tapThreshold || z < -tapThreshold)&& down != 3 ) {
48
-
37
+
38
+ if ((z > tapThreshold || z < -tapThreshold)&& down != 3 ) {
49
39
Serial.println (" Tap detected across Z-axis" );
50
40
}
51
41
}
52
-
53
42
}
0 commit comments