Arduino control RGB LED with Accelerometer..Help!

I have a project in which i need to control RGB LED using Accelerometer and the basic idea is to change the color of led if the xyz axis of accelerometer changes so if its normal it should be green and if it tilts anyway it should be turn into red. I have picked up and tried to modify it.

The issues are:

  1. Even if i write digitalWrite(redpin, HIGH); and digitalWrite(greenpin, LOW); it turns on the green light and couldnt figure out in this code what is wrong.

  2. I need it to be smooth transition from green to light green to red and same way around but i couldnt achieve it with this code please share some ideas :frowning:

Thanks in advance.

#include <Wire.h>

#define i2cID 0x1D
#define redpin 9
#define greenpin 10
#define bluepin 11





void setup()

{

  pinMode(redpin,OUTPUT);
  pinMode(greenpin,OUTPUT);
  pinMode(bluepin,OUTPUT);
  digitalWrite(redpin, HIGH);
  digitalWrite(greenpin, HIGH);
  digitalWrite(bluepin, HIGH);


  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(57600);

  Serial.println("Wire.begin");

  Wire.beginTransmission(i2cID);
  Wire.send(0x21); // CTRL_REG2 (21h)
  Wire.send(B01000000);
  //SPI 4/3 wire
  //1=ReBoot - reset chip defaults
  //n/a
  //filter off/on
  //filter for freefall 2
  //filter for freefall 1
  //filter freq MSB
  //filter freq LSB - Hipass filter (at 400hz) 00=8hz, 01=4hz, 10=2hz, 11=1hz (lower by 4x if sample rate is 100hz)
  Wire.endTransmission();

  Wire.beginTransmission(i2cID);
  Wire.send(0x20); // CTRL_REG1 (20h)
  Wire.send(B01000111);
  //sample rate 100/400hz
  //power off/on
  //2g/8g
  //self test
  //self test
  //z enable
  //y enable
  //x enable
  Wire.endTransmission();

}

void loop()
{

#define outXhigh 0x29
#define outYhigh 0x2B
#define outZhigh 0x2D
#define statusReg 0x27

  boolean goodRead;
  goodRead = false;

  //----------Status Register-----------------------
  byte status;
  status = B00000000;
  //

  //-------------------------------
  Wire.beginTransmission(i2cID);
  Wire.send(statusReg);
  Wire.endTransmission();

  Wire.requestFrom(i2cID, 1);
  if(Wire.available())
  {
    status = Wire.receive();
  }



  //----------X Values-----------------------
  byte x_val;

  //-------------------------------
  Wire.beginTransmission(i2cID);
  Wire.send(outXhigh);
  Wire.endTransmission();

  Wire.requestFrom(i2cID, 1);
  if(Wire.available())
  {
    x_val = Wire.receive();
  }


  //----------Y Values-----------------------
  byte y_val;

  //-------------------------------
  Wire.beginTransmission(i2cID);
  Wire.send(outYhigh);
  Wire.endTransmission();

  Wire.requestFrom(i2cID, 1);
  if(Wire.available())
  {
    y_val = Wire.receive();
  }

  //----------Z Values-----------------------
  byte z_val;

  //-------------------------------
  Wire.beginTransmission(i2cID);
  Wire.send(outZhigh);
  Wire.endTransmission();

  Wire.requestFrom(i2cID, 1);
  if(Wire.available())
  {
    z_val = Wire.receive();
    goodRead=true;
  }
  //-------------------------------


  if (goodRead==true)
  {
  
    if(x_val < 10 && y_val > 50 && z_val > 240) {
     
      Serial.print(" X Axis is = ");
      Serial.print(x_val, DEC);
      Serial.print("\t");
      digitalWrite(redpin, HIGH);
      digitalWrite(greenpin, LOW);
     
    }

    else {

      digitalWrite(greenpin, HIGH);
      digitalWrite(redpin, LOW);
    }

      Serial.print(" Y Axis is = ");
      Serial.print(y_val, DEC);
      Serial.print("\t");
     
      Serial.print(" Z Axis is = ");
      Serial.print(z_val, DEC);

    Serial.println();
  }
  else
    Serial.print("no data");
    delay(100);
}

You need to tell us how your LEDs are wired.

Ok now that you raised that question im a bit confused here and will try to attach a image of how i attached my led and my video of the latest development with accelerometer but i doubt maybe cant see the image properly because of low image quality.

Anyway this is the image link i followed to connect the LED as below:

http://cjparish.blogspot.com/2009/12/rgb-led.html

http://2.bp.blogspot.com/_lGksE4X_fOc/Szyr6Rg2HwI/AAAAAAAAAAc/2g77TpjHRV4/s1600-h/SAM_8391.JPG

But now i saw another post where it is showing different This is the new link i came across in the forum as below:

http://halley.cc/arduino/arduino-duemilanove-triled-small.jpg

Please suggest which one is the right now? im fairly new to this so really confused.

See the link below for the video of my development so far:

http://www.youtube.com/watch?v=GPAUluCE3w8

Thanks...