I'm really new to this so call me dumb..

(Kyle Halvorson) #1

I’m really new to this so call me dumb… But how would I map leds to light up sucessively (the higher the value the more leds are lit and when the value is decreased they turn off, like a bar graph) across a strip of 10 leds with the readings from an accelerometer? the accelerometer reads values from -32000 to +32000 and I’d like leds 1-5 to light up with the negative values and 6-10 with the positive values. I’m terribe at coding but heres what I have so far…

// MPU-6050 Short Example Sketch
// By Arduino User JohnChi
// August 17, 2014
// Public Domain
const int MPU=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

#include <FastLED.h> // FastLED library

#define LED_DT 13 // Data pin
#define NUM_LEDS 10 // Number of LED’s
#define COLOR_ORDER GRB // Change the order as necessary
#define LED_TYPE WS2811 // What kind of strip are you using?
#define BRIGHTNESS 196 // How bright do we want to go

struct CRGB leds[NUM_LEDS]; // Initializxe our array

// Initialize global variables for sequences
int thisdelay = 50; // A delay value for the sequence(s)
int thishue = 179;
int thissat = 255;

void setup(){
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
set_max_power_in_volts_and_milliamps(5, 500);
void loop(){
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.requestFrom(MPU,14,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
Serial.print("AcX = “); Serial.print(AcX);
Serial.print(” | AcY = “); Serial.print(AcY);
Serial.print(” | AcZ = “); Serial.print(AcZ);
Serial.print(” | Tmp = “); Serial.print(Tmp/340.00+36.53); //equation for temperature in degrees C from datasheet
Serial.print(” | GyX = “); Serial.print(GyX);
Serial.print(” | GyY = “); Serial.print(GyY);
Serial.print(” | GyZ = "); Serial.println(GyZ);

int i = map(AcX, -32000, 32000, 1, NUM_LEDS);
for(int i = 0; i < NUM_LEDS; i++)
leds[i] = CRGB::Red;
fill_solid(leds, NUM_LEDS, CRGB::Black);


(Daniel Garcia) #2

// Figure out how many leds would be lit, and in what direction
int numLit = abs(AcX) / (32000/(NUM_LEDS/2));

// sanity check, just in case we get larger values than we’re expecting!
if(numLit > (NUM_LEDS/2)) {
numLit = NUM_LEDS/2;

// empty out the existing leds

if(AcX < 0) {
// we want to light up numLit leds, starting from the midpoint, going down
for(int i = 1; i <= numLit; i++) {
leds[(NUM_LEDS/2)-i] = CRGB::Red;
} else {
// we want to light up numLit leds, starting from the midpoint, going forward
for(int i = 0; i < numLit; i++) {
leds[(NUM_LEDS/2)+i] = CRGB::Red;


(Kyle Halvorson) #3

he IDE is saying theres a stray “/” in the program and is highlighting LEDS.show();
I can’t seem to find one though…

(Kyle Halvorson) #4

I got it to work! Thank you so much for the help, I really appreciate it!

(Davide Gomba) #5

Thanks for sharing this here.
I’m trying to do a very similar task.
Basically merging MPU-6050 (using the very same sketch used here) and WS2812 led using fastled library.
I’m not retrieving any error from the IDE, which is correctly printing all the data both from the gyroscope and the accelerometer.
Nothing happens on the LED side, connected - in my setup - on pin 8.

Any advices?
Thanks in advance.

(Nacho Soler) #6

@Davide_Gomba I have the same problem. Did you find a solution?