Skip to content
53 changes: 53 additions & 0 deletions examples/AccelerometerTap/AccelerometerTap.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Arduino LSM6DS3 - Accelerometer Tap

this code is to detect tap

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Remove pointless blank lines.

using IMU.accelerationAvailable()

*/

#include <Arduino_LSM6DS3.h>

void setup() {
Serial.begin(9600);

while (!Serial);

while (!IMU.begin()) {

Serial.println("Failed to initialize IMU!");

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while (!Serial);
while (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (!Serial);
while (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");

Remove pointless blank lines.

delay (3000); // wait for 3 sec and check if it can be initialize again

}

}

float tapThreshold = 0.05 ; //0.05g acceleration in some direction is considered as tap. it can be change for the required sensitivity.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
float tapThreshold = 0.05 ; //0.05g acceleration in some direction is considered as tap. it can be change for the required sensitivity.
float tapThreshold = 0.05; //0.05 g acceleration in some direction is considered as tap. it can be changed for the required sensitivity.


int down = 3 ; // signifing the direction of which is facing downward 1 for x axis ; 2 for y axis ; 3 for z axis;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int down = 3 ; // signifing the direction of which is facing downward 1 for x axis ; 2 for y axis ; 3 for z axis;
int down = 3; // signifying the direction which is facing downward: 1 for x axis; 2 for y axis; 3 for z axis

Fix formatting and typo.


Why not make this a char so you can do this:

char down = 'z'; // the axis which is facing downward


void loop() {

float x, y, z;

if (IMU.accelerationAvailable()) {

IMU.readAcceleration(x, y, z);

if ((x > tapThreshold || x < -tapThreshold) && down != 1 ) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Remove pointless blank lines.

Serial.println("Tap detected across X-axis");
}
if ((y > tapThreshold || y < -tapThreshold) && down != 2 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ((y > tapThreshold || y < -tapThreshold) && down != 2 ) {
if ((y > tapThreshold || y < -tapThreshold) && down != 2) {

Fix formatting.


Serial.println("Tap detected across Y-axis");
}
if ((z > tapThreshold || z < -tapThreshold)&& down != 3 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ((z > tapThreshold || z < -tapThreshold)&& down != 3 ) {
if ((z > tapThreshold || z < -tapThreshold) && down != 3) {

Fix formatting.


Serial.println("Tap detected across Z-axis");
}
}

}