comparing readings from a sensor over time...

Hey everybody, I'm an arduino beginner, so please bear with me:

I want to detect sudden movement with an accelerometer: Basically, I want an 'if' syntax that will be 'true' ONLY if the accelerometer moves- regardless of the position that it's in.

My best guess is to take a reading, and then take another reading a few milliseconds later- " if x1 - x2 != 0, then... " but I don't know how to consistently take several readings from the same sensor....

I've looked over all the tutorials and haven't found a simple way to do this, so I come to you, the forum.

Any help would be much appreciated.

You're unlikely to get identical consecutive readings, even when stationary, so take the absolute value of the difference of the readings and see if that is greater than some small non-zero value.

Would you like to know anything about the movement, or simply that a movement of X magnitude has occurred in T time ?

The simplest, linear approach would be to measure at time t0 and store that in a variable x, then when time is t0+T you need to compare x against the current reading, and store the current reading as x for the next check at t0+(2*T).

PSEUDOCODE: [UNTESTED]

if (time_for_read) {
  //read current x and y
  cx = getCurrentX();
  cy = getCurrentY();
  //calc magnitude
  magnitute = sqrt( ((cx-x)^2) + ((cy-y)^2) );
  //compare to see if there has been a movement greater than THRESHOLD
  if (magnitute > THRESHOLD) {
    //MOVEMENT DETECTED
  }
  //store current reading
  x = cx;
  y = cy;
}

Thanks alphabeta- I'm trying to determine if a movement of x or y has occurred in time... I'm playing with your code now...

Why do you need a sqrt operation?

Well. One does not need it, but now magnitude is in the same unit of measure as the reading. So THRESHOLD can be defined as a 'sensible' number. Of course, one could eliminate the sqrt (premature optimization?) and define THRESHOLD as value*value.

okay.... is cx the variable to store the raw pulse data coming straight off the accelerometer, or does it store accelerometer data calculated with time?

I'm having trouble declaring these variables...

would it be possible to take a reading, use the delay function and take another reading? (probably not, but that's what I was trying before... naturally, it didn't work)

cx is the value right off the acc. yes. The current value of x

if you want to test the code, for say an interval of 50ms you could do:

delay(50); //wait 50 ms each loop
boolean shouldRead = true; //hack for testing
if (shouldRead ) {
  //read current x and y
  cx = getCurrentX();
  cy = getCurrentY();
  //calc magnitude
  magnitute = sqrt( ((cx-x)^2) + ((cy-y)^2) );
  //compare to see if there has been a movement greater than THRESHOLD
  if (magnitute > THRESHOLD) {
    //MOVEMENT DETECTED
  }
  //store current reading
  x = cx;
  y = cy;
}

I foresee another problem. You're using an accelerometer to detect movement, which isn't necessarily a sufficient sensor. Consider if the object is moving but not accelerating, how will you know (without doing some integration)? Are there more bounds to this problem?