Hi there,
So I went and did the other LOL here is the code both in Battery and Debug on the expansion board.
It uses the onboard IMU of the Chip.
Red LED is VIM (Vibration,Impact,Motion) can support any of it, easy to add some BLE and send signals either way.
here’s the Video short
and here’s the code.
// debug code added /8/14/24
//
// V I M code Demo
//
//
#include <LSM6DS3.h>
#include <Wire.h>
#include <U8x8lib.h>
#define DEBUG_MODE // Comment this line out to disable debug mode
LSM6DS3 myIMU(I2C_MODE, 0x6A); // Create an instance of class LSM6DS3
#ifdef DEBUG_MODE
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* clock=*/ PIN_WIRE_SCL, /* data=*/ PIN_WIRE_SDA, /* reset=*/ U8X8_PIN_NONE);
#endif
float aX, aY, aZ, gX, gY, gZ;
const float accelerationThreshold = 1.25; // threshold of significant in G's
const int numSamples = 59;
int samplesRead = numSamples;
const int buttonPin = 1; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
int BuzzerPin = A3; // A3 , P0.29 PIN 4
uint8_t interruptCount = 0; // Amount of received interrupts
uint8_t prevInterruptCount = 0; // Interrupt Counter from last loop
int buzzer = BuzzerPin;
int Onthemove = 0; // Not Moving Status
void setup() {
// put your setup code here, to run once:
#ifdef DEBUG_MODE
Serial.begin(9600);
delay(1000); // relax...
Serial.println("Processor came out of reset.\n");
u8x8.begin();
u8x8.setFlipMode(1); // set number from 1 to 3, the screen word will rotate 180
pinMode(LED_BUILTIN, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input:
pinMode(BuzzerPin, OUTPUT);
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
setLedRGB(false, false, true); // set blue led
#endif
// Call .begin() to configure the IMU
if (myIMU.begin() != 0) {
#ifdef DEBUG_MODE
Serial.println("Device error");
#endif
} else {
#ifdef DEBUG_MODE
Serial.println("IMU Good");
Serial.println("aX,aY,aZ,gX,gY,gZ");
#endif
}
#ifdef DEBUG_MODE
u8x8.setFont(u8x8_font_8x13B_1x2_r);
u8x8.clearDisplay();
u8x8.setCursor(4, 0);
u8x8.print("Grab Me ");
u8x8.setCursor(5, 3);
u8x8.print("&");
u8x8.setCursor(1, 6);
u8x8.print("Activity-");
u8x8.print("Demo");
#endif
}
void loop() {
// wait for significant motion
while (samplesRead == numSamples) {
// read the acceleration data
aX = myIMU.readFloatAccelX();
aY = myIMU.readFloatAccelY();
aZ = myIMU.readFloatAccelZ();
// 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;
setLedRGB(true,false, false); // set RED only
Onthemove = 1;
#ifdef DEBUG_MODE
tone (buzzer, 800);
delay (800);
noTone(buzzer);
delay (200);
u8x8.clearDisplay();
u8x8.setCursor(5, 3);
u8x8.print("ON The Move");
#endif
break;
}
setLedRGB(false, true, false); // set green only
#ifndef DEBUG_MODE
noTone(buzzer);
#endif
}
// check if all the required samples have been read since
// the last time significant motion was detected
while (samplesRead < numSamples) {
// read the acceleration and gyroscope data
samplesRead++;
#ifdef DEBUG_MODE
// print the data in CSV format
Serial.print(myIMU.readFloatAccelX(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatAccelY(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatAccelZ(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatGyroX(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatGyroY(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatGyroZ(), 3);
Serial.println();
#endif
if (samplesRead == numSamples) {
#ifdef DEBUG_MODE
// add an empty line if it's the last sample
Serial.println();
if (Onthemove != 0 ){
u8x8.clearDisplay();
u8x8.setCursor(4, 0);
u8x8.print("Grab Me ");
u8x8.setCursor(5, 3);
u8x8.print("AGAIN");
}
#endif
}
}
}
// -------------------- Utilities ------------------------- //
void setLedRGB(bool red, bool green, bool blue) {
if (!blue) { digitalWrite(LEDB, HIGH); } else { digitalWrite(LEDB, LOW); }
if (!green) { digitalWrite(LEDG, HIGH); } else { digitalWrite(LEDG, LOW); }
if (!red) { digitalWrite(LEDR, HIGH); } else { digitalWrite(LEDR, LOW); }
}
Read the comment to see how it works, AMA if you need help.
HTH
GL PJ
Filmed on a Potato
This demo depicts the code for motion / Vibration (if you set it low enough) and for Impact Alerts using the onboard accelerometer & RGB LED the buzzer and display as well as the serial output of the readings are included if you don’t comment out the debug defines flag , If so then no serial output or Display or Buzzer code will be compiled making it very good on power and Tiny in size and if you add some sleep code to it very long battery life.