Lilypad Accelerometer ADXL335 Help

Hi,

I have a Lilypad Arduino ATmega 328 and a Lilypad Accelrometer ADXL335.

I have used the example code to retrieve input values from the ADXL335 and that seems to be working fine. When the ADXL335 is flat and stationary I am getting values around 512 for x and y, and 612 for z.

As a simple experiment, I am trying to get individual LEDs to light up depending on the tilt of the ADXL335. I decided to start with the x axis. When I tilt the ADXL335 away from me I get minimum values of about 370 and when I tilt it towards me I get maximum values of about 640.

The hardware is setup as follows:
xpin = A3
ypin = A2
zpin = A1

  • to + on lilypad
  • to - on lilypad
    xforward LED to 6
    xbackward LED to 7

I have written some code in an attempt to get xforward to light up when I tilt the ADXL335 towards me and x backwards to light up when I title the ADXL355 away from me. However I am failing. To test my connection I wrote an else statement to get the lights to blink. The lights just blink all the time, regardless of my xpin readings.

Any advice would be great. Here is my code:

/*
 ADXL3xx
 
 Reads an Analog Devices ADXL3xx accelerometer and communicates the
 acceleration to the computer.  The pins used are designed to be easily
 compatible with the breakout boards from Sparkfun, available from:
 http://www.sparkfun.com/commerce/categories.php?c=80

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

 The circuit:
 analog 0: accelerometer self test
 analog 1: z-axis
 analog 2: y-axis
 analog 3: x-axis
 analog 4: ground
 analog 5: vcc
 
 created 2 Jul 2008
 by David A. Mellis
 modified 4 Sep 2010
 by Tom Igoe 
 
 This example code is in the public domain.

*/

// 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 xforward = 6;
int xbackward = 7;
int ledState = LOW;

void setup()
{
  // initialize the serial communications:
  Serial.begin(9600);
  
  // Provide ground and power by using the analog inputs as normal
  // digital pins.  This makes it possible to directly connect the
  // breakout board to the Arduino.  If you use the normal 5V and
  // GND pins on the Arduino, you can remove these lines.

  pinMode(xforward, LOW);
  pinMode(xbackward, LOW);
  
}

void loop()
{
  // print the sensor values:
  Serial.print(analogRead(xpin));
  // print a tab between values:
  Serial.print("x \t");
  Serial.print(analogRead(ypin));
  // print a tab between values:
  Serial.print("y \t");
  Serial.print(analogRead(zpin));
  Serial.println("z");
  // delay before next reading:
  delay(1000);
  
  if(xpin > 370 && xpin < 512){     //get xbackward to light up when tilt back
    digitalWrite(xbackward, HIGH);
    digitalWrite(xforward, LOW);
}

else if(xpin > 512 && xpin < 640) {   //get xforward to light up when tilt back
  digitalWrite(xforward, HIGH);
  digitalWrite(xbackward, LOW);
}

else {
  ledState = !ledState;              //get xforward and xbackward to blink
  digitalWrite(xforward, ledState);
  digitalWrite(xbackward, ledState);
}
}

HI, there are some errors in the code

this is not right semantically:

pinMode(xforward, LOW);
pinMode(xbackward, LOW);

Use INPUT or OUTPUT


Your if then else construct does not map all possible values => missing <= 370, 512, >= 640

Most important, you should not use xpin as that is the pinnumber !!! You should assign the output of analogRead(pinn) to some var.

I changed your code a bit, give it a try. Note I removed the blink code with a double HIGH

/*
 ADXL3xx
 
 Reads an Analog Devices ADXL3xx accelerometer and communicates the
 acceleration to the computer.  The pins used are designed to be easily
 compatible with the breakout boards from Sparkfun, available from:
 http://www.sparkfun.com/commerce/categories.php?c=80

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

 The circuit:
 analog 0: accelerometer self test
 analog 1: z-axis
 analog 2: y-axis
 analog 3: x-axis
 analog 4: ground
 analog 5: vcc
 
 created 2 Jul 2008
 by David A. Mellis
 modified 4 Sep 2010
 by Tom Igoe 
 
 This example code is in the public domain.

*/

// 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 x;
int y;
int z;

int xforward = 6;
int xbackward = 7;
int ledState = LOW;

void setup()
{
  // initialize the serial communications:
  Serial.begin(9600);
  
  // Provide ground and power by using the analog inputs as normal
  // digital pins.  This makes it possible to directly connect the
  // breakout board to the Arduino.  If you use the normal 5V and
  // GND pins on the Arduino, you can remove these lines.

  pinMode(xforward, OUTPUT);
  pinMode(xbackward, OUTPUT);
  
}

void loop()
{
  // READ THE SENSORS
  x = analogRead(xpin);
  y = analogRead(ypin);
  z = analogRead(zpin);
  
  // PRINT SENSOR VALUES:
  Serial.print("x: ");
  Serial.println(x, DEC);
  Serial.print("y: ");
  Serial.println(y, DEC);
  Serial.print("z: ");
  Serial.println(z, DEC);
  
  // BURN THE LEDS
  if (x < 370)
  {
    digitalWrite(xbackward, LOW);
    digitalWrite(xforward, LOW);   
  }
  else if (x < 512)     //get xbackward to light up when tilt back
  {
    digitalWrite(xbackward, HIGH);
    digitalWrite(xforward, LOW);
  } 
  else if( x < 640)  //get xforward to light up when tilt back
  {
    digitalWrite(xforward, HIGH);
    digitalWrite(xbackward, LOW);
  }
  else  // x >= 640   //get xforward and xbackward to blink
  {          
    digitalWrite(xforward, HIGH);
    digitalWrite(xbackward, HIGH);
  }

  // WAIT FOR NEXT LOOP    
  delay(1000);
}
1 Like