Arduino + ADXL345 from Bajdi on Vimeo.
After running the example sketch sketches that come with the ADXL345 accelerometer library it was time to try something on my own. And the simplest thing is fading a couple of leds. By mapping the value of the Y-axis to 2 big 10mm leds connected to 2 PWM outputs on an Arduino Mega 2560 you can fade the leds by tilting the ADXL345 from left to right.
Here is the sketch:
// http://www.bajdi.com // ADXL345 over I2C // 2 leds connected to 2 PWM outputs // The leds fade by tilting the sensor over the Y-axis. #include "Wire.h" #include "ADXL345.h" ADXL345 adxl; void setup(){ Wire.begin(); adxl.powerOn(); delay(1); adxl.set_bw(ADXL345_BW_12); delay(500); } void loop(){ int x,y,z; adxl.readAccel(&x, &y, &z); //read the accelerometer values and store them in variables x,y,z if (y >= 20) { y = map(y, 20, 250, 0, 255); analogWrite(10, y); } else { analogWrite(10, 0); } if (y <= -20) { y = map(y, -250, -20, 255, 0); analogWrite(11, y); } else { analogWrite(11, 0); } }