C Program Code for position detection

How to write a C Program Code for Position Detection in C Programming Language ?


Solution:

/* This is an example for the Adafruit MMA8451 Accel breakout board*/


#include <Wire.h>
#include<Time.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>

Adafruit_MMA8451 mma = Adafruit_MMA8451();
double x=0,y=0,z=0,ux=0,uy=0,uz=0,ax=0,ay=0,az=0;

void setup(void) {
  Serial.begin(9600);
 
  Serial.print("Adafruit MMA8451 test!");
 

  if (! mma.begin()) {
    Serial.print("Couldnt start");
    while (1);
  }
  Serial.print("MMA8451 found!");
 
  mma.setRange(MMA8451_RANGE_2_G);
 
  Serial.print("Range = "); Serial.print(2 << mma.getRange());
  Serial.print("G");
 
}

void loop() {
  // Read the 'raw' data in 14-bit counts
 
  mma.read();
// Serial.print("X:\t"); Serial.print(mma.x);
  //Serial.print("\tY:\t"); Serial.print(mma.y);
  //Serial.print("\tZ:\t"); Serial.print(mma.z);
  //Serial.println();

  /* Get a new sensor event */
  sensors_event_t event;
  mma.getEvent(&event);

  /* Display the results (acceleration is measured in m/s^2) */
  x=((ux+ ((ax)*(second()-1)))*(second()-1) + 0.5 * (ax) * ((second()-1) * (second()-1)));
  y=((uy+ ((ay)*(second()-1)))*(second()-1) + 0.5 * (ay) * ((second()-1) * (second()-1)));
  z=((uz+ ((az)*(second()-1)))*(second()-1) + 0.5 * (az) * ((second()-1) * (second()-1)));

  ux=(ux+ ((ax)*(second()-1)));
  uy=(uy+ ((ay)*(second()-1)));
  uz=(uz+ ((az)*(second()-1)));

  ax=event.acceleration.x;
  ay=event.acceleration.y;
  az=event.acceleration.z;

 
  Serial.print(x); Serial.print(",");
  Serial.print(y); Serial.print(",");
  Serial.print(z); Serial.print(",");

//  Serial.print(event.acceleration.y); Serial.print(".");
//  Serial.print(event.acceleration.z); Serial.print(".");
  Serial.print(second()); Serial.print("#");
 

 
  /* Get the orientation of the sensor */
  uint8_t o = mma.getOrientation();
 
  switch (o) {
    case MMA8451_PL_PUF:
    //  Serial.println("Portrait Up Front");
      break;
    case MMA8451_PL_PUB:
      //Serial.println("Portrait Up Back");
      break;  
    case MMA8451_PL_PDF:
   //   Serial.println("Portrait Down Front");
      break;
    case MMA8451_PL_PDB:
     // Serial.println("Portrait Down Back");
      break;
    case MMA8451_PL_LRF:
     // Serial.println("Landscape Right Front");
      break;
    case MMA8451_PL_LRB:
      //Serial.println("Landscape Right Back");
      break;
    case MMA8451_PL_LLF:
      //Serial.println("Landscape Left Front");
      break;
    case MMA8451_PL_LLB:
      //Serial.println("Landscape Left Back");
      break;
    }
  Serial.println();
  delay(1000);
 
}


Learn More :