Skip to content
Snippets Groups Projects
Commit 75b31124 authored by Robert Cheetham's avatar Robert Cheetham
Browse files

Signed-off-by: Robert Cheetham <rec1g18@soton.ac.uk>

parent 9a4136c6
No related branches found
No related tags found
No related merge requests found
#include <Arduino.h>
#include<Wire.h>
#include <math.h>
const int MPU_addr=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ; // These are the variables for reading the raw accelerometer value from the device.
double rRaw,previousR,previousPreviousR; // The resultant sqrt((x^2)+(y^2)+(z^2)) and the previous ones.
double total; // The total weighted as a low pass filter. Half current and quarter previous two.
double maxTotal,minTotal; // The max and minimun in the current session.
int steps = 0; // Number of steps.
bool below; // Boolean to determine if the value has gone 10000 less than the maximum in that period.
unsigned long lastTimeRead =0; // Used to determine how long until next reading
void step(){ // I imagine this part will be where the bluetooth implemention is?
steps++;
}
void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);
previousR=14088.24; // To make sure the starting values arnt offset.
previousPreviousR=14088.24; // These are the common values.
below=true;
maxTotal=2000; // Will obviously be overwritten in first instance.
minTotal=50000;
}
void loop(){
unsigned long now = millis(); // Checks current time
if ((now-lastTimeRead)>5) // Checks to see if enough time has elapsed (will reduce with ne sensor)
{
Wire.beginTransmission(MPU_addr); // Reads accelerometer
Wire.write(0x3B); //
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); //
AcX=Wire.read()<<8|Wire.read(); //
AcY=Wire.read()<<8|Wire.read(); //
AcZ=Wire.read()<<8|Wire.read(); //
rRaw=sqrt(pow(AcX,2)+pow(AcY,2)+pow(AcZ,2)); // The resultant of each axis
total= 0.5*rRaw+0.25*previousR+0.25*previousPreviousR; // The curved amount
if ((below)&&(total-minTotal>=10000)&&(total>=30000)){ // Make sure above a certain value and has increased by a certain amount.
step(); // These will be altered.
below=false;
minTotal=total; // Resets minmum.
}
minTotal=min(minTotal,total); // Overrides min/max
maxTotal=max(maxTotal,total);
if (maxTotal-total>=10000){ // Checks has dropped by 10000
maxTotal=total;
below=true;
}
lastTimeRead=now; // Resets to current amounts
previousPreviousR=previousR;
previousR=rRaw;
Serial.print("\n"); // Just prints steps, obviously remove.
Serial.print(steps);Serial.print(",");
}
delay(5);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment