I have an ADXL345 accelerometer attached to an OSEPP Uno R3 Plus through I2c.
I have bank of 10 red leds on a breadboard neg to ground through 330 ohm resistor.
Positive to digital pins 2-11.
My Goal.
I am trying to take the output from the acc. X axis only and translate that to the pins in succession.
So as I tip the x axis up towards the pull of gravity I get integers from 0-250. As the reading passes
25 pin 2 goes high and lights the first led. As the reading passes 50, pin 3 goes high and pin 2 goes
low, lighting the second led and turning off the first led and so on until the values go over 225 and pin
11 goes high.
I have put some code to test each led circuit by turning pins 2-11 high then low.
I have left in code to read &y,&z axis as I do not fully understand how leaving them out effects things.
I have tried to find good explanations on how the readRaw(&x,&y,&z) works in very simple terms
but I cannot.
My problem.
When first started up, leds 1-9 light, but not number 10. The circuit and led are fine it's just not
coded properly . 1-9 pass the circuit test then turn off. As it starts to react to the accelerometer
values only the first 2 leds light and turn off in response.
No other led will light. As I move the accelerometer from horizontal to vertical I see very clearly the
first 2 leds reacting perfectly but no other.
Also in the code I wrote the values to the serial monitor so I can see when I reach the various
thresholds. As the monitor shows 25 I do get a led lit and at 50 another, so the accelerometer works.
I'm sure there are more elegant ways to code this but as I am very early days into coding I
am trying to keep it as simple and basic as I can and I am still stuck.
Thank you for your help
#include <Wire.h>
#include "ADXL345lib.h"
Accelerometer acc;
const int ledCount = 10;
int ledPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11
}; // an array of pin numbers to which LEDs are attached
//////////////////////////////////////////////////////////////
void setup() {
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
digitalWrite(thisLed,HIGH); ///////////////////////////flash all leds to test
delay(1000); /////////Give time to see leds light individually
}
Serial.begin(57600);
if (acc.begin(OSEPP_ACC_SW_ON) != 0)
{
Serial.println("Error connecting to accelerometer");
return;
acc.setSensitivity(ADXL345_RANGE_PM2G);
}
}
///////////////////////////////////////////////////////////////
void loop() {
int16_t x, y, z;
acc.readRaw(&x, &y, &z); //read the accelerometer values and store them in variables x,y,z
// Output x value
Serial.println(x); //see actual value for x in the serial monitor
//////////////////////////////////////
if (x >= 25)
{
digitalWrite(2, HIGH);
}
else
{
digitalWrite(2,LOW);
}
//////////////////////////////////////
if (x >= 50)
{
digitalWrite(3, HIGH);
digitalWrite(2, LOW); ////////////////////**************
}
else
{
digitalWrite(3,LOW);
/////////////////////////////////////
if (x >= 75)
{
digitalWrite(4, HIGH);
digitalWrite(3, LOW); ////////////////////**************
}
else
{
digitalWrite(4,LOW);
}
////////////////////////////////////
if (x >= 100)
{
digitalWrite(5, HIGH);
digitalWrite(4, LOW); ////////////////////**************
}
else
{
digitalWrite(5,LOW);
}
////////////////////////////////////
if (x >= 125)
{
digitalWrite(6, HIGH);
digitalWrite(5, LOW); ////////////////////**************
}
else
{
digitalWrite(6,LOW);
}
////////////////////////////////////
if (x >= 150)
{
digitalWrite(7, HIGH);
digitalWrite(6, LOW); ////////////////////**************
}
else
{
digitalWrite(7,LOW);
}
///////////////////////////////////
if (x >= 175)
{
digitalWrite(8, HIGH);
digitalWrite(7, LOW); ////////////////////**************
}
else
{
digitalWrite(8,LOW);
}
////////////////////////////////////
if (x >= 200)
{
digitalWrite(9, HIGH);
digitalWrite(8, LOW); ////////////////////**************
}
else
{
digitalWrite(9,LOW);
}
///////////////////////////////////
if (x >= 225)
{
digitalWrite(10, HIGH);
digitalWrite(9, LOW); ////////////////////**************
}
else
{
digitalWrite(10,LOW);
}
}
delay(100);
}