Hi, My fading PWM Leds aren't fading the way they should be...

I have the UNO, an accelerometer breakout board (ADXL345), all wired in and working fine.

I have the 3 legs of my RGB led connected to the PWM outputs, and then grounded through resistors.

I have converted the -ve figures from the accelerator and condensed to a 0-255 scale.

I have then set the brightness of the Red LED to the x axis, the green to the y axis and the blue to the z axis.

now, the theory is sound, that if i turn the unit 180 degrees (in one plane), the x (for instance) will change from 0-255, thus the red led should fade from off to full on, as with the other directions i should get a glowing set of colours that change as quickly (or slowly) as i turn the board.

What i get, is full on until it hits a point, and then full off with no gradual fade.

Please feel free to have a look at the code to see if it is code error.

// Include the Wire library so we can start using I2C.
#include <Wire.h>
// Include the Love Electronics ADXL345 library so we can use the accelerometer.
#include <ADXL345.h>

// Declare a global instance of the accelerometer.
ADXL345 accel;

// Set up a pin we are going to use to indicate our status using an LED.
int statusPin = 2; // I'm using digital pin 2.

// Fading LED connected to PWN output 9
int RedPin = 8;    // Red LED connected to digital pin 8
int GreenPin = 9;    // Red LED connected to digital pin 9
int BluePin = 10;    // Red LED connected to digital pin 10

void setup()
  // Begin by setting up the Serial Port so we can output our results.
  // Start the I2C Wire library so we can use I2C to talk to the accelerometer.
  // Ready an LED to indicate our status.
  pinMode(statusPin, OUTPUT);
  // Ready RGB Led to Outputs
  pinMode(RedPin, OUTPUT);  
  pinMode(GreenPin, OUTPUT);
  pinMode(BluePin, OUTPUT);
  // Create an instance of the accelerometer on the default address (0x1D)
  accel = ADXL345();
  // Check that the accelerometer is infact connected.
    Serial.println("Connected to ADXL345.");
    digitalWrite(statusPin, HIGH); // If we are connected, light our status LED.
    Serial.println("Could not connect to ADXL345.");
    digitalWrite(statusPin, LOW); // If we are not connected, turn our LED off.
  // Set the range of the accelerometer to a maximum of 2G.
  accel.SetRange(2, true);
  // Tell the accelerometer to start taking measurements.

void loop()
  if(accel.IsConnected) // If we are connected to the accelerometer.
    // Read the raw data from the accelerometer.
    AccelerometerRaw raw = accel.ReadRawAxis();
    //This data can be accessed like so:
    int xAxisRawData = raw.XAxis;
    // Read the *scaled* data from the accelerometer (this does it's own read from the accelerometer
    // so you don't have to ReadRawAxis before you use this method).
    // This useful method gives you the value in G thanks to the Love Electronics library.
    AccelerometerScaled scaled = accel.ReadScaledAxis();
    // This data can be accessed like so:
    float xAxisGs = scaled.XAxis;
    // We output our received data.
    Output(raw, scaled);

  void Output(AccelerometerRaw raw, AccelerometerScaled scaled)

    // set the colours and correct for negativity  
  int RedValue = ((raw.XAxis+256)/2) ; 
  int GreenValue = ((raw.YAxis+256)/2) ;
  int BlueValue = ((raw.ZAxis+256)/2) ;
  delay(30) ;

    // sets the value (range from 0 to 255):
  analogWrite(RedPin, RedValue);
  analogWrite(GreenPin, GreenValue);
  analogWrite(BluePin, BlueValue);    

  // Tell us about the this data, but scale it into useful units, take away the negatives and scale to 0-255
   Serial.print("   \tScaled:\t");
   Serial.print("   ");   
   Serial.print("   ");   

Thanks for looking and please be gentle!

Pin 8 isn't PWM. How you wire them up, have a pencil to show drawings?

by the technology invested in me, i have a badly drawn pdf!

Dropbox - Error (dropbox shared file, no bad news here!)

its only a simple circuit

i see now that on the uno the ~ signifies the pwm outputs, so i will move the pins / code from 8,9,10 to 9,10,11 this should solve the pwm issue.

but... shouldn't the 9 and 10 pins at the moment be fading, leaving just the red being weird?

You should use 3 resistors for 1 RGB leds, in series with each color led, otherwise when led with lower drop-voltage lights-up, it blocking two others.

The way you used to correct the negative value was incorrect, you can’t any value less then 128,
try this code

    // set the colours and correct for negativity  
  int RedValue = (raw.XAxis>0?raw.XAxis:((raw.XAxis+256)/2)) ; 
  int GreenValue = (raw.YAxis>0?raw.YAxis:((raw.YAxis+256)/2)) ;
  int BlueValue = (raw.ZAxis>0?raw.ZAxis:((raw.ZAxis+256)/2) ;

The way you used to correct the negative value was incorrect, you can’t any value less then 128,
try this code

ah, didn’t realise that.

but the fading led example in the arduino examples, goes smoothly from 0 > 255 in increments of 5? :S

    // set the colours and correct for negativity  

int RedValue = (raw.XAxis>0?raw.XAxis:((raw.XAxis+256)/2)) ;
  int GreenValue = (raw.YAxis>0?raw.YAxis:((raw.YAxis+256)/2)) ;
  int BlueValue = (raw.ZAxis>0?raw.ZAxis:((raw.ZAxis+256)/2) ;

whats this doing here?

“xaxis>0?” == if xaxis is greater than 0?

xaxis:(old code) << whats the purpose of the :frowning: ) phrase?

as a side note, its still flickery with the new code update, and the resistors for each leg

there is fading now, but it gets flicky at the bottom (lowest luministy of the led), i could make the formula ignore the bottom end of the spectrum so it goes say, 150>255 instead of 128>255?