Skip to content

Commit 46b3e86

Browse files
committed
resolve #3
tried to resolve the requested querry.
1 parent 1618d6b commit 46b3e86

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

examples/AccelerometerTap/AccelerometerTap.ino

+16-8
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,42 @@
88

99
#include <Arduino_LSM6DS3.h>
1010

11+
float tapThreshold ;
12+
float xi, yi , zi ;
13+
1114
void setup() {
15+
1216
Serial.begin(9600);
1317

1418
while (!Serial);
15-
19+
1620
while (!IMU.begin()) {
1721
Serial.println("Failed to initialize IMU!");
1822
delay(3000); // wait for 3 sec and check if it can be initialized again
1923
}
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.
2030
}
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;
2431

2532
void loop() {
2633
float x, y, z;
2734
if (IMU.accelerationAvailable()) {
2835
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){
3139
Serial.println("Tap detected across X-axis");
3240
}
3341

34-
if ((y > tapThreshold || y < -tapThreshold) && down != 2) {
42+
if (y-yi > tapThreshold || y-yi < -tapThreshold){
3543
Serial.println("Tap detected across Y-axis");
3644
}
3745

38-
if ((z > tapThreshold || z < -tapThreshold) && down != 3) {
46+
if (z-zi > tapThreshold || z-zi < -tapThreshold) {
3947
Serial.println("Tap detected across Z-axis");
4048
}
4149
}

0 commit comments

Comments
 (0)