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
#include<Wire.h>
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.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);
LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
set_max_power_in_volts_and_milliamps(5, 500);
}
void loop(){
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
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);
delay(3);
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);
}