A little help with an accelerometer, please!

Hello! I am totally noob. With an Arduino Uno and a ADXL345 accelerometer, I have to trigger pin 12 when the accelerometer records movements/vibrations. I have tested the code for the accelerometer (below) and it works fine, I just don.t know how to trigger pin 12 when the accelerometer moves in any direction.
Thank you!!

#include <Wire.h>  
#define Register_ID 0
#define Register_2D 0x2D
#define Register_X0 0x32
#define Register_X1 0x33
#define Register_Y0 0x34
#define Register_Y1 0x35
#define Register_Z0 0x36
#define Register_Z1 0x37
int ADXAddress = 0x53;  //I2C address
int reading = 0;
int val = 0;
int X0,X1,X_out;
int Y0,Y1,Y_out;
int Z1,Z0,Z_out;
double Xg,Yg,Zg;

void setup()
{
  Serial.begin(9600);
  delay(100);
  Wire.begin(); 
  delay(100);
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_2D);
  Wire.write(8);
  Wire.endTransmission();
  Serial.println("Accelerometer Test ");
} 
void loop()
{
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_X0);
  Wire.write(Register_X1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2);
  {
    X0 = Wire.read();
    X1 = Wire.read();
    X1 = X1<<8;
    X_out = X0+X1;
  } 
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_Y0);
  Wire.write(Register_Y1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2);
  {
    Y0 = Wire.read();
    Y1 = Wire.read();
    Y1 = Y1<<8;
    Y_out = Y0+Y1;
  } 
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_Z0);
  Wire.write(Register_Z1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2);
  {
    Z0 = Wire.read();
    Z1 = Wire.read();
    Z1 = Z1<<8;
    Z_out = Z0+Z1;
  } 
  Xg = X_out/256.00;
  Yg = Y_out/256.00;
  Zg = Z_out/256.00;
  Serial.print("X=");
  Serial.print(Xg);
  Serial.print("\tY=");
  Serial.print(Yg);
  Serial.print("\tZ=");
  Serial.println(Zg);
  delay(300);  
}

The source of the code: here

I just don.t know how to trigger pin 12 when the accelerometer moves in any direction.

What values are you seeing for Xg, Yg, and Zg when the sensor sits perfectly still? When it moves?

Thank you for your help! I have to make this project, but using a digital accelerometer instead of that analog one. So the accelerometer does not have a specific position, and the changes detected should be somewhere to the minimum (like in order to detect an earthquake vibrations).

If you want to do some real science, and build a device that will detect earthquakes that don't destroy your home as well, this one and this one work very well.

Thank you, they seem real nice projects! But (un)fortunately I am just trying to help a 9th grade daughter of my neighbors do her homework, which is this simple project. Now all that I need is the code...

Balaurele: Thank you, they seem real nice projects! But (un)fortunately I am just trying to help a 9th grade daughter of my neighbors do her homework, which is this simple project. Now all that I need is the code...

The first think in your code is to define pin 12 with a name. The make it an output. You can find all this in any of the IDE examples.

The hard part will be to figure out what you mean by "trigger pin 12". Do you mean to go high or to go low? How long do you want this to occur or is it for ever?

Paul

This is the code for the project we have to make. The difference is that in that project the code is writtend for an analog accelerometer, and I have a digital one, as an analog one is very difficult to find. Thank you!

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(A2, INPUT);
pinMode(A4, INPUT);
pinMode(12, OUTPUT);
}


void loop() {
  // put your main code here, to run repeatedly:
int x = analogRead(A0);
int y = analogRead(A2);
int z = analogRead(A4);

Serial.print("x = ");
Serial.println(x);
/*Serial.print("y = ");
Serial.println(y);
Serial.println("z = ");
Serial.println(z);*/
delay(1000);

if( x>300)
{
 digitalWrite(12,HIGH);
}
else
{
  digitalWrite(12,LOW);
}
}

Balaurele: This is the code for the project we have to make. The difference is that in that project the code is writtend for an analog accelerometer, and I have a digital one, as an analog one is very difficult to find. Thank you!

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(A2, INPUT);
pinMode(A4, INPUT);
pinMode(12, OUTPUT);
}

void loop() {   // put your main code here, to run repeatedly: int x = analogRead(A0); int y = analogRead(A2); int z = analogRead(A4);

Serial.print("x = "); Serial.println(x); /Serial.print("y = "); Serial.println(y); Serial.println("z = "); Serial.println(z);/ delay(1000);

if( x>300) { digitalWrite(12,HIGH); } else {   digitalWrite(12,LOW); } }

Then you had best get started understanding what the original code does, so you can modify it. Perhaps it's time to consider just starting with new code using the digital device.

Paul

I just don.t know how to trigger pin 12 when the accelerometer moves in any direction.

If you study the code in reply #6, it uses an if statement to make a similar decision:

if( x>300)
{
 digitalWrite(12,HIGH);
}
else
{
  digitalWrite(12,LOW);
}
}

The above won't work well in any case, but it serves as an example. Study up a bit on if statements and generalize the example to include the Y and Z axes.

Hint: it would be a good idea to read the resting or average values of the X, Y and Z accelerations and subtract those from subsequent measurements before making any decisions.

pinMode(A0, INPUT);
pinMode(A2, INPUT);
pinMode(A4, INPUT);

Unnecessary, if all you're doing is analogRead.