Lilypad Accelerometer ADXL335 Help

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