Created
July 22, 2020 15:47
-
-
Save squirelo/b8c79f44a5d5ae60a6c79987f67d26f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino_LSM9DS1.h> | |
const float accelerationThreshold = 3.5; // threshold of significant in G's | |
const int firePin = 2; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(LED_BUILTIN, OUTPUT); | |
pinMode(firePin, OUTPUT); | |
digitalWrite(firePin, LOW); | |
// initialize the IMU | |
if (!IMU.begin()) { | |
Serial.println("Failed to initialize IMU!"); | |
while (1); | |
} | |
// print out the samples rates of the IMUs | |
Serial.print("Accelerometer sample rate = "); | |
Serial.print(IMU.accelerationSampleRate()); | |
Serial.println(" Hz"); | |
Serial.print("Gyroscope sample rate = "); | |
Serial.print(IMU.gyroscopeSampleRate()); | |
Serial.println(" Hz"); | |
Serial.println(); | |
} | |
void loop() { | |
float aX, aY, aZ, gX, gY, gZ; | |
if (IMU.accelerationAvailable()) { | |
// read the acceleration data | |
IMU.readAcceleration(aX, aY, aZ); | |
// sum up the absolutes | |
float aSum = fabs(aX) + fabs(aY) + fabs(aZ); | |
// check if it's above the threshold | |
if (aSum >= accelerationThreshold) { | |
digitalWrite(LED_BUILTIN, HIGH); | |
digitalWrite(firePin,HIGH); | |
delay(1000); | |
digitalWrite(LED_BUILTIN, LOW); | |
digitalWrite(firePin,LOW); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment