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 the Love Electronics ADXL345 library so we can use the accelerometer.
// Declare a global instance of the accelerometer.
// 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
// 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.
// Ready RGB Led to Outputs
// 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.
// Tell the accelerometer to start taking measurements.
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.
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) ;
// sets the value (range from 0 to 255):
// Tell us about the this data, but scale it into useful units, take away the negatives and scale to 0-255
Thanks for looking and please be gentle!