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

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

parent 75b31124
Branches WristBand
No related tags found
No related merge requests found
#ifndef STEPCOUNTER_HPP
#define STEPCOUNTER_HPP
#define ADXL375_DEBUG_PRINT /* If defined it will send ADXL375 x data trough UART */
//#define ADXL375_BOOL_OUTPUT /* Used if a boolean change is needed in stead of interger
#define _robADXL375_setup setup
#define _robADXL375_reading loop
#include <Wire.h> // Wire library - used for I2C communication
#include <Arduino.h>
#define ADXL375 0x53 // The ADXL375 sensor I2C address
void _robADXL375_step();
void _robADXL375_setup();
void _robADXL375_reading();
#endif
\ No newline at end of file
#include <Arduino.h>
#include<Wire.h>
#include <math.h>
//
// Base I2C code for interaction taken from
// https://howtomechatronics.com/tutorials/arduino/how-to-track-orientation-with-arduino-and-adxl345-accelerometer/
//
// And then adapted to work with ADXL375
//
//
// Step counter sends an interger number of steps.
//
// This can be changed to a boolean by defining ADXL375_BOOL_OUTPUT in header. However not sure what is preferred.
//
// In order to full embed comment out #define _robADXL375_setup setup
// #define _robADXL375_reading loop Rob xx
//
#include "StepCounter.hpp"
const int MPU_addr=0x68; // I2C address of the MPU-6050
int16_t xRaw; // Outputs
int16_t xRawMin=100;
int16_t xRawMax=0;
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.
int steps=0; // Counts number of punches . Not needed in final.
#ifdef ADXL375_BOOL_OUTPUT
bool step =false;
#endif
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
bool below=false; // Determines if a full pulse has occurred
bool above =false;
void step(){ // I imagine this part will be where the bluetooth implemention is?
void _robADXL375_step(){ // I imagine this part will be where the bluetooth implemention is?
steps++;
#ifdef ADXL375_BOOL_OUTPUT
step =true;
#endif
}
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 _robADXL375_setup() {
Serial.begin(115200); // Initiate serial communication for printing the results on the Serial monitor
Wire.begin(); // Initiate the Wire library
// Set ADXL375 in measuring mode
Wire.beginTransmission(ADXL375); // Start communicating with the device
Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
// Enable measurement
Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable
Wire.endTransmission();
delay(10);
}
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.
}
void _robADXL375_reading() {
// === Read acceleromter data === //
Wire.beginTransmission(ADXL375);
Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(ADXL375, 2, true); // Read 2 registers xRaw,
xRaw = ( Wire.read()| Wire.read() << 8); // X-axis value
xRawMin=min(xRawMin,xRaw); // Overrides min/max
xRawMax=max(xRawMax,xRaw);
if ((below)&&(xRaw>=70)&&(xRaw-xRawMin>=30)){ // Make sure above a certain value and has increased by a certain amount.
above=true;
below=false;
xRawMin=xRaw;
}
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);
if (xRawMax-xRaw>=30&&above){ // Ensures value has gone back below limit
_robADXL375_step();
below=true;
above=false;
xRawMax=xRaw;
}
if (xRawMax-xRaw>=30){ // Ensures value is below a limit.
below=true;
xRawMax=xRaw;
#ifdef ADXL375_BOOL_OUTPUT
step =false;
#endif
}
#ifdef ADXL375_DEBUG_PRINT
Serial.print(xRaw);Serial.print(","); // Debug printing
Serial.println(steps);
#endif
delay(10);
}
\ 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