I would like to create a motion detector using either an MPU-6050 or GY-61. I can't seem to find what I'm looking for via google and search, maybe I'm not inputting the correct terms.
I'm not skilled enough to write the complex code but would appreciate if someone had seen the code around if you could post a link so I could take a look? All I want to do is register/count movement beyond a certain threshold, very much like a pedometer or sleep tracker; I don't need to know which axis or orientation just the a simple log of movements.
I have the light blue bean and the code below with it's on-board accelerometer works well, although I'd like to use my hardware. The problem being is this code references specific bean libraries that are installed that talk to the on-board accelerometer.
Ideally I'd like to port this code over to use with my hardware, but figured maybe someone had seen the code I'm after or could point me in the right direction.
I've just ordered the ADXL345 as it seems to have a setting which detects movement built into the library, but I don't want to pass up the opportunity to further understand the accelerometer code better.
From what I understand I need to take the raw values from the accelerometer and divide by X to find the G force, take a measurement, store it then continue to check if it's changed; if it has then do something.
Do I need to calculate the G force or can I use the raw data? I'm guessing I'd also need to smooth the raw data a little as it jumps around a little.
Thanks.
Setup would be using either a Uno or Mega then eventually using a Nano, running off battery with a HC-05 communicating to android which logs the events.
/*
This sketch shows you how to monitor if your Bean moves by sampling the acceleration at 5Hz and measuring change.
The LED will blink red when it's being moved.
Please note that if motion detection is triggered often, the LED use will drain the battery quickly.
This example code is in the public domain.
*/
// When acceleration change goes beyond this threshold, the LED will blink.
#define THRESHOLD 50
AccelerationReading previousAccel;
void setup() {
// Turn off the Bean's LED
Bean.setLed(0, 0, 0);
// Initial reading
previousAccel = Bean.getAcceleration();
}
// This function calculates the difference between two acceleration readings
int getAccelDifference(AccelerationReading readingOne, AccelerationReading readingTwo){
int deltaX = abs(readingTwo.xAxis - readingOne.xAxis);
int deltaY = abs(readingTwo.yAxis - readingOne.yAxis);
int deltaZ = abs(readingTwo.zAxis - readingOne.zAxis);
// Return the magnitude
return deltaX + deltaY + deltaZ;
}
void loop() {
// Get the current acceleration with a conversion of 3.91×10-3 g/unit.
AccelerationReading currentAccel = Bean.getAcceleration();
// Find the difference between the current acceleration and that of 200ms ago.
int accelDifference = getAccelDifference(previousAccel, currentAccel);
// Update previousAccel for the next loop.
previousAccel = currentAccel;
// Check if the Bean has been moved beyond our threshold.
if(accelDifference > THRESHOLD){
// Blink the LED
Bean.setLed(255, 0, 0);
//Bean.sleep(500);
delay (500);
Bean.setLed(0, 0, 0);
Bean.sleep(500);
delay (500);
}else{
Bean.sleep(500);
}
}