Help: Reliable readings from ADXL335

Hello I had been having a hard time figuring out how to get reliable readings from ADXL335 trying to compare the minimum & maximum readings with raw data. I based the code from the book Arduino Quick Start Guide to get the minimum and maximum readings but whenever I tilt the sensor lets say to the X(+/-) axis it would sometimes fall to the Y(+/-) axis even though how hard I tilt the sensor it seems that it is locking to Y when in fact I was tilting to the X axis. Below is my code.

//Analog read pins
const unsigned int X_AXIS_PIN = 2;
const unsigned int Y_AXIS_PIN = 1;
const unsigned int Z_AXIS_PIN = 0;
const unsigned int ADXL_LED = 13;
const unsigned int NUM_AXES = 3;
const unsigned int PINS[NUM_AXES] = { X_AXIS_PIN, Y_AXIS_PIN, Z_AXIS_PIN };
const unsigned int DELAY = 200;
const unsigned int BAUD_RATE = 9600;

//The minimum and maximum values that came from
//the accelerometer
int min_x, min_y, min_z;
int max_x, max_y, max_z;


void setup() {
  
  Serial.begin(BAUD_RATE);  
  
  pinMode(ADXL_LED, OUTPUT);
  digitalWrite(ADXL_LED, LOW);
  
  
  //Set minimum and maximum threshold
  min_x = min_y = min_z = 1000;
  max_x = max_y = max_z = -1000;
    
}


void loop() {
  
  delay(1);
  
  //Read values from pins
  int x = analogRead(PINS[0]);
  int y = analogRead(PINS[1]);
  int z = analogRead(PINS[2]);
  
  //Grab the minimum and maximum readings
  min_x = min(x, min_x); 
  max_x = max(x, max_x);
  
  min_y = min(y, min_y);
  max_y = max(y, max_y);
  
  min_z = min(z, min_z); 
  max_z = max(z, max_z);
    
  // Check if X axis equals to minimum
  if( x == min_x && x < max_x && y != min_y && y != max_y ) {
   
    Serial.print("RIGHT ");
    Serial.print(x);
    Serial.print(" = ");
    Serial.print(min_x);
    Serial.print("\t");
    
    Serial.print("LEFT ");
    Serial.print(x);
    Serial.print(" = ");
    Serial.print(max_x);
    Serial.print("\t");
    
    Serial.print("UP ");
    Serial.print(y);
    Serial.print(" = ");
    Serial.print(min_y);
    Serial.print("\t");
    
    Serial.print("DOWN ");
    Serial.print(y);
    Serial.print(" = ");
    Serial.println(max_y);

    x = y = 0;
   
    digitalWrite(ADXL_LED, HIGH);
    delay(DELAY);
    digitalWrite(ADXL_LED, LOW);
    
  }
  
  // Check if X axis equals to maximum
  if( x == max_x && x > min_x && y != min_y && y != max_y  ) {
   
 
    Serial.print("RIGHT ");
    Serial.print(x);
    Serial.print(" = ");
    Serial.print(min_x);
    Serial.print("\t");
    
    Serial.print("LEFT ");
    Serial.print(x);
    Serial.print(" = ");
    Serial.print(max_x);
    Serial.print("\t");
    
    Serial.print("UP ");
    Serial.print(y);
    Serial.print(" = ");
    Serial.print(min_y);
    Serial.print("\t");
    
    Serial.print("DOWN ");
    Serial.print(y);
    Serial.print(" = ");
    Serial.println(max_y);
    
    x = y = 0;

    digitalWrite(ADXL_LED, HIGH);
    delay(DELAY);
    digitalWrite(ADXL_LED, LOW);
   
  }
  
 
 // Check if Y axis equals to minimum
  if( y == min_y && y < max_y && x != min_x && x != max_x ) {
      
    Serial.print("RIGHT ");
    Serial.print(x);
    Serial.print(" = ");
    Serial.print(min_x);
    Serial.print("\t");
    
    Serial.print("LEFT ");
    Serial.print(x);
    Serial.print(" = ");
    Serial.print(max_x);
    Serial.print("\t");
    
    Serial.print("UP ");
    Serial.print(y);
    Serial.print(" = ");
    Serial.print(min_y);
    Serial.print("\t");
    
    Serial.print("DOWN ");
    Serial.print(y);
    Serial.print(" = ");
    Serial.println(max_y);
    
    x = y = 0;
    digitalWrite(ADXL_LED, HIGH);
    delay(DELAY);
    digitalWrite(ADXL_LED, LOW);
     
  }  
  
  // Check if Y axis equals to maximum
  if( y == max_y && y > min_y && x != min_x && x != max_x ) {
      
    Serial.print("RIGHT ");
    Serial.print(x);
    Serial.print(" = ");
    Serial.print(min_x);
    Serial.print("\t");
    
    Serial.print("LEFT ");
    Serial.print(x);
    Serial.print(" = ");
    Serial.print(max_x);
    Serial.print("\t");
    
    Serial.print("UP ");
    Serial.print(y);
    Serial.print(" = ");
    Serial.print(min_y);
    Serial.print("\t");
    
    Serial.print("DOWN ");
    Serial.print(y);
    Serial.print(" = ");
    Serial.println(max_y);
    
    x = y = 0;
    digitalWrite(ADXL_LED, HIGH);
    delay(DELAY);
    digitalWrite(ADXL_LED, LOW);
   
  }

}

The output would be something like this…

X- 509 = 265	X+ 509 = 509	Y+ 341 = 216	Y- 341 = 604
X- 510 = 265	X+ 510 = 510	Y+ 319 = 216	Y- 319 = 604
X- 511 = 265	X+ 511 = 511	Y+ 341 = 216	Y- 341 = 604
X- 264 = 264	X+ 264 = 511	Y+ 359 = 216	Y- 359 = 604
X- 262 = 262	X+ 262 = 511	Y+ 347 = 216	Y- 347 = 604
X- 259 = 259	X+ 259 = 511	Y+ 323 = 216	Y- 323 = 604
X- 251 = 251	X+ 251 = 511	Y+ 449 = 216	Y- 449 = 604
X- 239 = 239	X+ 239 = 511	Y+ 473 = 216	Y- 473 = 604 --> here I was tilting to X+ and was expecting to be equals to 511 but reading shows X- equals to minimum 239

Any comments and suggestions greatly appreciated… Thanks in advance

I am still learning the coding system but, I think this part may be the problem.

if( x == min_x && x < max_x && y != min_y && y != max_y ) {

Remove it and the other } that goes with it and see if your readings come out correctly. If that works you may need to make a new “min & max” function.

Mark

cyclegadget:
I am still learning the coding system but, I think this part may be the problem.

if( x == min_x && x < max_x && y != min_y && y != max_y ) {

Remove it and the other } that goes with it and see if your readings come out correctly. If that works you may need to make a new “min & max” function.

Mark

Thanks for the reply Mark, re: your suggestion yeah I thought so as well I am thinking of a way how to compare realtime readings from min and max values any suggestions?

I will give it a shot. There may be better ways but, here is what I think would work.

if( x < min){
  min = x;
}

 if (x > max){
max = x; 
}

Change X to Y or Z as needed.

cyclegadget:
I will give it a shot. There may be better ways but, here is what I think would work.

if( x < min){

min = x;
}

if (x > max){
max = x;
}




Change X to Y or Z as needed.

I guess I was not clear enough about the desired result my bad… actually what I was trying to accomplish is to check if X is equal to either min or max the same as with Y so if X is equals let’s say to min then do something if max do the other thing something like that… I have yet to find filtering those signal but I guess that will be hard because with just a itsy bitsy movement changes the readings… or there is a way?

Ok here is other ideas. I am sure there are better ways but, this is how I would do it.

if( x = min){
  //do this if x is equal to min  "true or false"
} 

if( x = max){
  //do this if x is equal to max  "true or false"
}

 if( x = min || x = max){
  //do this if x is equal to min or max  "or statement"
}

For filtering there is something called "smoothing" that has been mentioned. It takes several readings to get an average number.

http://www.arduino.cc/en/Tutorial/Smoothing

cyclegadget: For filtering there is something called "smoothing" that has been mentioned. It takes several readings to get an average number.

http://www.arduino.cc/en/Tutorial/Smoothing

Cool never thought of that will check it out thanks allot

Well I was able to partially smoothen-out the readings a bit but not still convinced that this is a way to go. Since variables min_x, min_y and min_z are filled with computed values within the loop(), I decided to record the previous values of these three variables to another three temp variables so that when next loop iteration runs I can see if there is a change on their values which gives me a hint that a tilt on an axis happened, if there is a tilt, I would then check if raw values coming from A0, A1, A2 (X, Y, Z respectively) are equals to the value of either min_x, min_y, min_z etc.. it will only display or do something if these conditions are met. so in code this goes something like this.

void setup() {

int min_x = -1000;
int max_x = 1000;
int temp_x = 0;

}

void loop() {

x = analogRead(0);

temp_x = min_x; // compare variable
min_x = min(x, min_x); // minimum tilt value on X+ axis
max_x = max(x, max_x); // maximum tilt value on X+ axis

if(temp_x == min_x) { 
  if(x == min_x) {
    // do something 
 }
} else {
  // do nothing just wait for next tilt
}

}