Skip to content
Snippets Groups Projects
Select Git revision
  • b7fd71e75544d15948b9d075f402f96e3545b554
  • master default
  • Omar
  • Jack
4 results

sample_test.py

Blame
  • stepCounter.cpp 2.68 KiB
    //
    //        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"
    
    
    int16_t xRaw;  // Outputs
    
    int16_t xRawMin=100;
    int16_t xRawMax=0;
    
    int steps=0;                   //  Counts number of punches . Not needed in final.
    #ifdef ADXL375_BOOL_OUTPUT
    bool step =false;
    #endif
    
    
    bool below=false;             //  Determines if a full pulse has occurred
    bool above =false;
    
    void _robADXL375_step(){    //    I imagine this part will be where the bluetooth implemention is?
      steps++;
      #ifdef ADXL375_BOOL_OUTPUT
      step =true;
      #endif
      
    }
    
    
    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 _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;
      }
        
      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);
    }