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.
Serial.begin(9600);
// Start the I2C Wire library so we can use I2C to talk to the accelerometer.
Wire.begin();
// 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.
if(accel.EnsureConnected())
{
Serial.println("Connected to ADXL345.");
digitalWrite(statusPin, HIGH); // If we are connected, light our status LED.
}
else
{
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.
accel.EnableMeasurements();
}
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((raw.XAxis+256)/2);
Serial.print(" ");
Serial.print((raw.YAxis+256)/2);
Serial.print(" ");
Serial.print((raw.ZAxis+256)/2);
Serial.println("");
}
Thanks for looking and please be gentle!