outPut adxl345 I2c to Bank of leds....? HELP

I’m learning to program and this is my first project. I’m trying to output data in the range of 0 to 1 G from

the OSEPP adxl345 with I2C to a row of 10 leds. As the g loads go up more leds light up. I’m not interested in negative

values. I am using 2 known example programs, Led_Bar_graph and ReadingGs. The latter being from OSEPP’s

example files for the adxl345. My understanding of the functions and language in general is very

rudimentary . I have been reading and studying many examples of the functions but I’m stuck.

I will post the original 2 code examples and the Melded code of my creation. I can get some leds to light

in various way dependent on what I use for the variables. The circuit works but not in the way I’d like it


Thank you for your help.

ReadingGs.ino (1.15 KB)

LED_Bar_Graph.ino (456 Bytes)

Reading_Gs_MODIFIED.ino (3.44 KB)

    //  My first attempt at programming....
    //  I am trying to combine 2 example sketches "Reading_Gs " and "Bar_Graph"
    //  to map a range of readings from 0 to 1 G to the number of leds which is 10 
    //  I am hoping to achieve a smooth transition of lighted leds as I tilt the sensor from level to 90 degrees 
    //  serial monitor works well and I see variables from -1.00 to 1.00 plus though I am not interested in negative values.


#include <Wire.h>
#include "ADXL345lib.h"

Accelerometer acc;
bool fail;

const int ledCount = 10;    // the number of LEDs in the bar graph      // CODE FROM BAR GRAPH 

int ledPins[] = {                                                       // CODE FROM BAR GRAPH 
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11                                        // CODE FROM BAR GRAPH 
};   // an array of pin numbers to which LEDs are attached              // CODE FROM BAR GRAPH 

void setup() {

    // loop over the pin array and set them all to output:                // CODE FROM BAR GRAPH 
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {                  // CODE FROM BAR GRAPH 
    pinMode(ledPins[thisLed], OUTPUT);

    // set the I2C address of the accelerometer
    // With the OSEPP Accelerometer use:
    //      OSEPP_ACC_SW_ON    - Switch is in the ON position
    //      OSEPP_ACC_SW_OFF   - Switch is in the OFF position
    // begin will return 0 on success
    if (acc.begin(OSEPP_ACC_SW_ON) != 0)
        Serial.println("Error connecting to accelerometer");
        fail = true;



void loop() {

     // don't bother reading if we failed to connect
    if (fail)

     // 3 doubles to store the data
    double x, y, z;

     // Read Gs
     // Note: You can also read the raw data by calling
     //       readRaw() - readRaw takes int16_t instead of doubles
    if (acc.readGs(&x,&y,&z) != 0)
        Serial.println("Failed to read accelerometer");

     // map the result to a range from 0 to the number of LEDs:    // CODE FROM BAR GRAPH 
  int ledLevel = map(x, 0, 2, 0, ledCount);                        //Original Line-- int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
                                                                   //x= output from x axis, 0= lowest range, 2.00= highest reading
                                                                   //from sensor, LedCount=number of leds to spread out the reading.
      // loop over the LED array:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
      // if the array element's index is less than ledLevel,
      // turn the pin for this element on:
    if (thisLed < ledLevel) {
      digitalWrite(ledPins[thisLed], HIGH);
      // turn off all pins higher than the ledLevel:
    else {
      digitalWrite(ledPins[thisLed], LOW);
      //  code to print values to serial monitor 
    Serial.print("X: ");
   // Serial.print(" Y: ");
   // Serial.print(y);
   // Serial.print(" Z: ");
   // Serial.println(z);

Please use code tags for the code you want people to look at - nobody likes downloading stuff. I put your sketch into code tags this time.

Thank you sir, my apologies.