Osepp Uno R3 Plus- ADXL345- I2C- Translate Output to LEDs

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);
  

    }

You used the ledPin array in setup() but abandoned it in loop(). How about another array that corresponds to the threshold to turn on each led and compare the x value to each entry in the array?

You only want one led on at a time, so begin with the largest threshold and look at them all in turn. If x is over the threshold turn the corresponding led on, turn all other the others off.

#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      

// x value threshold for each led
int ledThreshold[] = {                                                     
  10, 25, 50, 75, 100, 125, 150, 175, 200, 255                                          
};

//////////////////////////////////////////////////////////////
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

    boolean ledOn = false;  // true => an led is on

  // turn on an led if over threshold
  for (int thisLed = ledCount-1; thisLed >= 0; thisLed--) {

    // if x reached threshold for this led and no led is on
    if( (x >= ledThreshold[thisLed]) && !ledOn )
    {
      // turn this led on
      digitalWrite(ledPins[thisLed], HIGH);

      // set flag
      ledOn = true;

    } 
    else {

      // this led is off
      digitalWrite(ledPins[thisLed], LOW);

    } // else

  } // for

  // Output x value
  Serial.println(x);                      //see actual value for x in the serial monitor

  delay(100);

}

I am very greatful lfor your response and I knew there would be more graceful ways to code this.

For me this is a learning exercise and as such I really need to know the mistakes in my approach.

I am using code from another sketch with the array and actually wrote the latter half myself.

Being so new, I read, I take snippets of code from others and then try to finish the sketch

with my own code. 1 wrong syntax, though, simple for you, can be an hour of trouble shooting for me.

I will study this example you gave me and try to come to an understanding of it. But simple things like

the double && I have no idea what that means. I did try to compile it and recieved an error of acc. not

declared in this scope. I would like to complete my first exercise and then tackle these issues.

Again thank you so much.

OK ! Hooah ! I looked at the code you gave me and saw the line //Accelerometer acc;

commented out. I removed the comments and it became code and the whole sketch works !

I will study this so I can come to an understanding of it although I still would like to know my mistakes in the

the first sketch.

Hey, good troubleshooting. Sorry, I had that line commented so that it would compile on my computer without the library (I've edited my post to correct it).

The double ampersand means "and", so the line

if( (x >= ledThreshold[thisLed]) && !ledOn )

is saying "if x is over the given threshold and the variable ledOn is false". The ledOn variable is necessary because we only want to turn one led on and if x is over, say, the eighth threshold it will also be over the first through seventh. Starting at the highest threshold and working down is more simple than starting low and going up, because as soon as one test passes we know that there will be no other leds turn on.

As for your code, you had a misplaced curly brace. Check out this snippet:

if (x >= 50)
 {
  digitalWrite(3, HIGH);
  digitalWrite(2, LOW); ////////////////////**************
 }
  else
  {
  digitalWrite(3,LOW);
/////////////////////////////////////
if (x >= 75)
 {

You meant for just the digitalWrite() to be in the else clause but the closing brace is just before the delay near the end of loop(), so when x is 50 or greater the code after the else never gets executed. There is no test for greater than 75, etc.

Formatting your code with Tools->Auto Format can greatly help with things like this. You can tell by the indentation that something fishy is going on.

OH sir you are a gentleman and a scholar ! I thank you very much I have 2 versions that work now.

I didn't know about the auto format . Thank you for that as well.

Fantastic !