 # Calculating acceleration and velocity using accelerometer

Hi guys, i am designing a project using an accelerometer and some sensors to determine the speed of an oncoming vehicle and the speed at which i am traveling to determine whether its safe to overtake, however i have tested my code and my velocity gives me negative values and from time to time my accelerations does so too; can anyone help me spot this error please.

# include "WProgram.h"

ACC::ACC(int x , int y){ xPin = x; yPin = y; time = micros();

pinMode(xPin,INPUT); pinMode (yPin,INPUT); } //convert time to acceleration double ACC:: accel(){ pulX = pulseIn (xPin, HIGH); accelerationX =(((pulX / 10) - 500) * 8)/100; return accelerationX;

} //convert acceleration to velocity double ACC::velocityX(){ double velX = (accel()*pulX)/1000000; return velX; } //convert time to distance double ACC:: distance_behind(){ distance = (velocityX()*pulX)/1000000; return distance; }

Accelerometers can't measure velocity - only changes in velocity - you need integrate acceleration over time by repeated sampling, There will be drift that increases with time in this integrate value that needs correcting for somehow.

my velocity gives me negative values and from time to time my accelerations does so too;

Just like the value of investments, accelerations can go up as well as down.