Threshold the Scaled Analog ADXL335 Accelerometer Input to Turn on Light

My question revolves around how to set this threshold to control an LED when the accelerometer is exposed to a force higher than the threshold g force. If I set the threshold within the analog range of the board and replace 'X' in the if statement with 'x'- the code works, however, when I scale the analog input 'x' the threshold does not work. By this, I mean the LED remains ON for any positive 'X' value.

This is my first project and any help is appreciated. Thanks

This is my circuit setup, except the inputs are in 0, 1, and 2 and an LED is wired to GRND and 13:

int x = 0; //input x from ADXL335
int y = 1; //input y from ADXL335
int z = 2; //input z from ADXL335
int X = 0;
int Y = 0;
int Z = 0;
const int ledPin = 13;
const int threshold = .2; //threshold measured in g force
const int zeroAccel = ((1023/3.3)*1.65); // where 1023 us the max analog range, 3.3 is the max voltage, and 1.65 is 
//the voltage on an axis when it has zero acceleration 
const int analogToGforce = ((.330*1023)/3.3); // where 330mV/G is the sensitivity on the spec sheet. This converts the 
//analog values to a g force 

void setup()
{
  Serial.begin(9600);      // sets the serial port to 9600 baud
  
  analogReference(EXTERNAL);

  pinMode(ledPin, OUTPUT); //sets led at output 
  pinMode(x, INPUT); //sets x axis input 
  pinMode(y, INPUT);
  pinMode(z, INPUT);
}

void loop()
{
float x, y, z; 
  x = analogRead(0);       
  y = analogRead(1);       
  z = analogRead(2);       

float X, Y, Z; 
X = ((x-zeroAccel)/analogToGforce);
Y = ((y-zeroAccel)/analogToGforce);
Z = ((z-zeroAccel)/analogToGforce);
 
 if ( X > threshold ){ \\Issue is here
    digitalWrite(ledPin, HIGH);
    delay (50);
 }else{
   digitalWrite(ledPin, LOW);
   delay (50);
 }
   
 
  Serial.print("accelerations are x, y, z: ");
  Serial.print(x, DEC);    
  Serial.print(" ");       
  Serial.print(y, DEC);    
  Serial.print(" ");       
  Serial.println(z, DEC);  
  
  
  Serial.print("accelerations are X, Y, Z:");
  Serial.println();
  
  Serial.print(X, DEC); 
  Serial.println(" g");
  Serial.print(" ");
  
  Serial.print(Y, DEC); 
  Serial.println(" g");
  Serial.print(" ");
  
  Serial.print(Z, DEC); 
  Serial.println(" g");
  Serial.print(" ");
  
  
  
  delay(1000);              
}
int x = 0; //input x from ADXL335
int y = 1; //input y from ADXL335
int z = 2; //input z from ADXL335
int X = 0;
int Y = 0;
int Z = 0;

There is NO excuse for using variable names that differ only in case. Use your imagination.

  pinMode(x, INPUT); //sets x axis input
  pinMode(y, INPUT);
  pinMode(z, INPUT);

That is useless when you are using the pins as analog pins. You just diddled with the pin on the other side of the board.

float x, y, z;
  x = analogRead(0);       
  y = analogRead(1);       
  z = analogRead(2);       

float X, Y, Z;
X = ((x-zeroAccel)/analogToGforce);
Y = ((y-zeroAccel)/analogToGforce);
Z = ((z-zeroAccel)/analogToGforce);

It is especially stupid to then create local variables with the same name but different types.

I quit reading at this point.

const int threshold = .2;

I think maybe you need to go read up on what an integer is.

Yeah, I just started learning how to program. I am still trying to learn the syntax and methods through trial and error.

PaulS:

int x = 0; //input x from ADXL335

int y = 1; //input y from ADXL335
int z = 2; //input z from ADXL335
int X = 0;
int Y = 0;
int Z = 0;



There is NO excuse for using variable names that differ only in case. Use your imagination.



pinMode(x, INPUT); //sets x axis input
 pinMode(y, INPUT);
 pinMode(z, INPUT);



That is useless when you are using the pins as analog pins. You just diddled with the pin on the other side of the board.



float x, y, z;
 x = analogRead(0);      
 y = analogRead(1);      
 z = analogRead(2);

float X, Y, Z;
X = ((x-zeroAccel)/analogToGforce);
Y = ((y-zeroAccel)/analogToGforce);
Z = ((z-zeroAccel)/analogToGforce);



It is especially stupid to then create local variables with the same name but different types.

I quit reading at this point.