How to use an accerlometer to trun on an led

i am working on a project where i have an adxl 377 accelerometer hooked up to the arduino and i want to use the analog output from the accelerometer to turn on the led. i want a threshold 70g but to start off i just need to know how i should go about writing the code to turn the led on. i tried to write the code but its not working. any help would be appreciated. I have included the code below

// these constants describe the pins. They won't change:
const int xpin = A3; // x-axis of the accelerometer
const int ypin = A2; // y-axis
const int zpin = A1; // z-axis (only on 3-axis models)
int led = 13;

void setup()
{
// initialize the serial communications:

Serial.begin(9600);
pinMode(led, OUTPUT);
}

void loop()
{

int X = analogRead(xpin);
int Y = analogRead(ypin);
int Z = analogRead(zpin);

float Volt_X = X * 0.003222656;
float Volt_Y = Y * 0.003222656;
float Volt_Z = Z * 0.003222656;

float Cal_X = (Volt_X - 1.65)*1000.0;
float Cal_Y = (Volt_Y - 1.65)*1000.0;
float Cal_Z = (Volt_Z - 1.65)*1000.0;

float G_X = (Cal_X/6.5)-1.49;
float G_Y = (Cal_Y/6.5);
float G_Z = (Cal_Z/6.5);

// float Total_Gs = sqrt(sqrt(G_X)+ sqrt(G_Y) + sqrt(G_Z));

Serial.print("X_Value");
Serial.print(G_X);
Serial.print("\t");
Serial.print("Y_Value");
Serial.print(G_Y);
Serial.print("\t");
Serial.print("Z_Value");
Serial.print(G_Z);
Serial.print("\t");

if (G_X == 70)
{
digitalWrite(led,HIGH);
delay(1000);
}

Serial.println ();

delay(100);

}

Hi,

I know nothing about the accelerometer side of things so I'll assume you have that mastered :stuck_out_tongue:

Seems the problem part is this? (Note the use of code tags: the # above the :wink: smily)

 if (G_X == 70)
  {
    digitalWrite(led,HIGH);
    delay(1000);
  }

First you should probably be looking for greater than or equal to: your equals to will seldom be exactly matched. Then I guess you need to switch it off next time round if it's less than 70? So try something like this:

 if (G_X >= 70)    //note >= now
  {
    digitalWrite(led,HIGH);
    delay(1000);
  }
else
  {
    digitalWrite(led,LOW);  // turn off if G_X is not >= 70
  }

That should get you started.

You eventually might like to look at BlinkWithoutDelay so as not to tie your processor up just so the light can be seen....

will the device actual report a 70g value ?

And do you realise, you are only looking at the x direction ?

For what particular reason are you subtracting 1.49 when you calculate G_X ?