Skip to content

Make IMU sketches button-less and start after significant motion #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 21 additions & 26 deletions ArduinoSketches/IMU_Capture/IMU_Capture.ino
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
/*
IMU Capture

This example uses the on-board IMU to start reading acceleration and gyroscope
This example uses the on-board IMU to start reading acceleration and gyroscope
data from on-board IMU and prints it to the Serial Monitor for one second
when the button is pressed.
when the significant motion is detected.

You can also use the Serial Plotter to graph the data.

The circuit:
- Arduino Nano 33 BLE or Arduino Nano 33 BLE Sense board.
- Button connected to pin 3 and GND.

Created by Don Coleman, Sandeep Mistry
Modified by Dominic Pajak, Sandeep Mistry

This example code is in the public domain.
*/

#include <Arduino_LSM9DS1.h>

const int buttonPin = 3; // the number of the pushbutton pin
const float accelerationThreshold = 2.5; // threshold of significant in G's
const int numSamples = 119;

int previousButtonState = HIGH;
int samplesRead = numSamples;

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

// initialize the pushbutton pin as an input with (internal) pullup:
pinMode(buttonPin, INPUT_PULLUP);

if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
Expand All @@ -41,33 +37,32 @@ void setup() {
}

void loop() {
// read the state of the push button pin:
int buttonState = digitalRead(buttonPin);
float aX, aY, aZ, gX, gY, gZ;

// compare the button state to the previous state
// to see if it has changed
if (buttonState != previousButtonState) {
if (buttonState == LOW) {
if (samplesRead == numSamples) {
// pressed
// wait for significant motion
while (samplesRead == numSamples) {
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) {
// reset the sample read count
samplesRead = 0;
break;
}
} else {
// released
}

// store the state as the previous state, for the next loop
previousButtonState = buttonState;
}

// check if the all the required samples have been read since
// the last time the button has been pressed
if (samplesRead < numSamples) {
// check if the all the required samples have been read since
// the last time the significant motion was detected
while (samplesRead < numSamples) {
// check if both new acceleration and gyroscope data is
// available
if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) {
float aX, aY, aZ, gX, gY, gZ;

// read the acceleration and gyroscope data
IMU.readAcceleration(aX, aY, aZ);
IMU.readGyroscope(gX, gY, gZ);
Expand Down
41 changes: 18 additions & 23 deletions ArduinoSketches/IMU_Classifier/IMU_Classifier.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

The circuit:
- Arduino Nano 33 BLE or Arduino Nano 33 BLE Sense board.
- Button connected to pin 3 and GND.

Created by Don Coleman, Sandeep Mistry
Modified by Dominic Pajak, Sandeep Mistry

This example code is in the public domain.
*/
Expand All @@ -29,10 +29,9 @@

#include "model.h"

const int buttonPin = 3; // the pin number of the pushbutton pin
const float accelerationThreshold = 2.5; // threshold of significant in G's
const int numSamples = 119;

int previousButtonState = HIGH;
int samplesRead = numSamples;

// global variables used for TensorFlow Lite (Micro)
Expand Down Expand Up @@ -65,9 +64,6 @@ void setup() {
Serial.begin(9600);
while (!Serial);

// initialize the pushbutton pin as an input with pullup:
pinMode(buttonPin, INPUT_PULLUP);

// initialize the IMU
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
Expand Down Expand Up @@ -103,32 +99,31 @@ void setup() {
}

void loop() {
// read the state of the push button pin:
int buttonState = digitalRead(buttonPin);
float aX, aY, aZ, gX, gY, gZ;

// compare the button state to the previous state
// to see if it has changed
if (buttonState != previousButtonState) {
if (buttonState == LOW) {
if (samplesRead == numSamples) {
// pressed }
// wait for significant motion
while (samplesRead == numSamples) {
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) {
// reset the sample read count
samplesRead = 0;
break;
}
} else {
// released
}

// store the state as the previous state, for the next loop
previousButtonState = buttonState;
}

// check if the all the required samples have been read since
// the last time the button has been pressed
if (samplesRead < numSamples) {
// the last time the significant motion was detected
while (samplesRead < numSamples) {
// check if new acceleration AND gyroscope data is available
if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) {
float aX, aY, aZ, gX, gY, gZ;

// read the acceleration and gyroscope data
IMU.readAcceleration(aX, aY, aZ);
IMU.readGyroscope(gX, gY, gZ);
Expand Down