File tree 1 file changed +16
-8
lines changed
examples/AccelerometerTap
1 file changed +16
-8
lines changed Original file line number Diff line number Diff line change 8
8
9
9
#include < Arduino_LSM6DS3.h>
10
10
11
+ float tapThreshold ;
12
+ float xi, yi , zi ;
13
+
11
14
void setup () {
15
+
12
16
Serial.begin (9600 );
13
17
14
18
while (!Serial);
15
-
19
+
16
20
while (!IMU.begin ()) {
17
21
Serial.println (" Failed to initialize IMU!" );
18
22
delay (3000 ); // wait for 3 sec and check if it can be initialized again
19
23
}
24
+
25
+ if (IMU.accelerationAvailable ()) {
26
+ IMU.readAcceleration (xi, yi, zi); // initial values of acceleration including gravity or some zero error.
27
+ }
28
+
29
+ tapThreshold = 0.05 ; // 0.05 g acceleration in some direction is considered as tap. it can be changed for the required sensitivity.
20
30
}
21
- float tapThreshold = 0.05 ; // 0.05 g acceleration in some direction is considered as tap. it can be changed for the required sensitivity.
22
-
23
- int down = 3 ; // signifying the direction of which is facing downward 1 for x axis; 2 for y axis; 3 for z axis;
24
31
25
32
void loop () {
26
33
float x, y, z;
27
34
if (IMU.accelerationAvailable ()) {
28
35
IMU.readAcceleration (x, y, z);
29
-
30
- if ((x > tapThreshold || x < -tapThreshold) && down != 1 ) {
36
+
37
+ // workes on difference from initial acceleration to the current acceleration
38
+ if (x -xi > tapThreshold || x-xi < -tapThreshold){
31
39
Serial.println (" Tap detected across X-axis" );
32
40
}
33
41
34
- if ((y > tapThreshold || y < -tapThreshold) && down != 2 ) {
42
+ if (y-yi > tapThreshold || y-yi < -tapThreshold){
35
43
Serial.println (" Tap detected across Y-axis" );
36
44
}
37
45
38
- if ((z > tapThreshold || z < -tapThreshold) && down != 3 ) {
46
+ if (z-zi > tapThreshold || z-zi < -tapThreshold) {
39
47
Serial.println (" Tap detected across Z-axis" );
40
48
}
41
49
}
You can’t perform that action at this time.
0 commit comments