Determine if vehicle is not moving using accelerometer

Hi everyone,

For my project I would like to determine if my car is moving or not.
To do this, I mounted an LSM303 accelerometer in the car.

A LED must turn ON when the car is stationary and the LED must turn OFF when the car is MOVING.

Now I want to code the following, for example:

The LSM303 X-axis measures a value less than 50 for atleast 1 second: car is stationary
The LSM303 X-axis measures a value more then 50: car is moving.

How can I determine if a value exists for a certain amount of time?

Thanks!

Some important law of physics says that you can't know whether you're moving at a constant speed or whether you're stationary.
At least not with an accelerometer.

In theory a car moving constant velocity has no acceleration to detect. In practice moving cars vibrate enough that motion can be detected reasonably well. The difficulty is differentiation between motion induced vibrant and movement by passengers or engine vibration.

Yeah... Theoretically, you'd know you're moving if you accelerate and never decelerate. In the real world you can't measure small acceleration/deceleration because you don't have infinite resolution and the noise can overwhelm the measurement.

How can I determine if a value exists for a certain amount of time?

One approach might be to average the readings over a "certain amount of time".

Accelerometers measure the acceleration due to Earth's gravity in addition to the acceleration caused by other forces. This means that the accelerometer always reports a large value for the total acceleration, except in the case of free fall.

I would use it as a vibration detector. Any value over the threshold counts as movement. So just record the millis() each time you see those values. When the last detection is older than the time then you probably arent moving.

You set the threshold and time by experiment and personal preference.

Ttduino:
The LSM303 X-axis measures a value less than 50 for at least 1 second: car is stationary

How can I determine if a value exists for a certain amount of time?

Reset the timer when the value is out of range:

    static unsigned long startTime = millis();
    if (value >= 50)
        startTime = millis();
    if (millis() - startTime >= 1000)
    {
        // Value has been below 50 for at least one full second.
    }

Mount a mouse to your front bumper.