detecting movement versus stillness with accelerom

I'm an arduino beginner so please bear with me...

I want to detect sudden movement with an accelerometer, I'm looking for an 'if' syntax' that will be 'true' ONLY if the accelerometer moves, regardless of the position its in...

I asked this question last week but still haven't found a solution to my problem..

basically, I need to compare readings from the accelerometer to see if they've changed, which would indicate that a movement has occured...

Here's my code (obviously not working)

const int xPin = 2; const int yPin = 3;

const int ledPin = 13; const int threshold = 50;

void setup() { Serial.begin(9600); pinMode(xPin, INPUT); pinMode(yPin, INPUT); pinMode(ledPin, OUTPUT); }

void loop() { // variables to read the pulse widths: int pulseX, pulseY; // variables to contain the resulting accelerations int Old_accelerationX, Old_accelerationY, New_accelerationX, New_accelerationY;

//read pulses from X and Y axis pulseX = pulseIn(xPin, HIGH); pulseY = pulseIn(yPin, HIGH);

//convert pulse width into acceleration Old_accelerationX = ((pulseX / 10) - 500) * 8; Old_accelerationY = ((pulseY / 10) - 500) * 8; //accelerationX2 = ((pulseX / 10) - 400) * 8;

Serial.print(Old_accelerationX); Serial.print("\t"); Serial.print(Old_accelerationY); Serial.println();

delay(100);

//////////////////////////////////////// LED sequence //compare old and new acceleration readings if (Old_accelerationX - New_accelerationX > threshold) { ///turn LED on digitalWrite(ledPin, HIGH); } else { ///turn LED off digitalWrite(ledPin, LOW); }

delay(200); //after delay, store new readings Old_accelerationX = New_accelerationX; Old_accelerationY = New_accelerationY;

}

You're only looking at the accelerometer about 3 times a second. You need to make the 'old' values static or global (or both)

Thanks groove, try not to laugh at me as I ask this, but how would I make them static or global?

Do I simply declare the 'old' above the void setup?

Also, try to edit your post, mark all the code and click the icon. This will make it easier to read your code :)

thanks AB, I was wondering how to separate the code from the text field... I tried the example you gave me last week but was unsuccessful-

any thoughts on how to get my code working?

You're declaring the variables inside the loop. Move them outside the loop so they're not reset every iteration :)

There's avery good chance that they're not overwritten, but it is best not to risk it and ensure you learn good habits.

[edit]Re-reads code, smacks forehead. [/edit]

Re-reads code, smacks forehead.

Why do I get the distinct impression that it wasn't your forehead you wanted to smack?

Re-reads code, smacks forehead.

Why do I get the distinct impression that it wasn't your forehead you wanted to smack?

I call it wishful thinking. If you want someone to fulfill your fetishist dreams I'm sure I could write an Arduino Smacker for you. ;) Endless pleasure! (as long as the power source provides power)

I'm sure I could write an Arduino Smacker for you.

Using a FIFO queue without calling count()?

I'm sure I could write an Arduino Smacker for you.

Using a FIFO queue without calling count()?

Now, you're getting too kinky!

Tired static variable for (Old_acceleration) and making it global…
still no dice.

What I have noticed is that LED only lights when X value goes into the positive, and turns off when the X value becomes negative.

Maybe my math is wrong-
New_accelerationX - Old_accelerationX > threshold

heres the code-

const int xPin = 2;
const int yPin = 3;

const int ledPin = 13;
const int threshold = 30;

int Old_accelerationX, Old_accelerationY;

void setup() {
  Serial.begin(9600);
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode(ledPin, OUTPUT);

}
  
  void loop() {
  // variables to read the pulse widths:
  int pulseX, pulseY;
  // variables to contain the resulting accelerations
  int New_accelerationX, New_accelerationY;
  
  //static int Old_accelerationX, Old_accelerationY;
    
    //read pulses from X and Y axis
    pulseX = pulseIn(xPin, HIGH);
    pulseY = pulseIn(yPin, HIGH);

    
    //convert pulse width into acceleration
 Old_accelerationX = ((pulseX / 10) - 500) * 8;
 Old_accelerationY = ((pulseY / 10) - 500) * 8;
 //accelerationX2 = ((pulseX / 10) - 400) * 8;
 
    
    Serial.print(Old_accelerationX);
    Serial.print("\t");
    Serial.print(Old_accelerationY);
    Serial.println();
    
    delay(5);
    
    //////////////////////////////////////// LED sequence
   //compare old and new acceleration readings
   if (New_accelerationX - Old_accelerationX > threshold) {
    ///turn LED on
    digitalWrite(ledPin, HIGH);
   }
   else {
    ///turn LED off
    digitalWrite(ledPin, LOW);
   }
  
  delay(50);
 //after delay, store new readings
  Old_accelerationX = New_accelerationX;
  Old_accelerationY = New_accelerationY;

}

Still smacking forehead. Considering extending smacking action.

I'm sure I'm making a very obvious mistake here grove; nonetheless, if you could impart your wisdom on me it would be much appreciated...

You’re reading the ‘old’ values from the accelerometers, rather than from memory.

how to I store the old value without it getting wiped by the next iteration of the loop?

Your "new" values are the ones that are automatic, and get "wiped". Your "old" values survive from "loop" to "loop".

Just concentrate on x, and work through a couple of iterations.

Thanks AWOL, I know I need to learn this stuff and get a comprehensive grasp on my own, but it can be frustrating...

I'm a graphic designer, very visually oriented- this stuff does not come naturally to me.

void loop() 
{
    int pulseX = pulseIn(xPin, HIGH);
    int New_accelerationX = ((pulseX / 10) - 500) * 8;
    Serial.print(New_accelerationX);
    Serial.print("\t");
    Serial.print(Old_accelerationX);
    Serial.println();

    delay(5);

    //////////////////////////////////////// LED sequence
   //compare old and new acceleration readings
   if ([glow]abs[/glow](New_accelerationX - Old_accelerationX) > threshold) {
    ///turn LED on
    digitalWrite(ledPin, HIGH);
   }
   else {
    ///turn LED off
    digitalWrite(ledPin, LOW);
   }

  delay(50);
 //after delay, store new readings
  Old_accelerationX = New_accelerationX;
}

Ah... I knew it had to be something with absolute value... Thanks man.