Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Kyle Halvorson 10/21/14
- //Toyota Supra G-Meter Project V1.4
- //Accelerometer sensor reading and mapping to two WS2811 LED strips
- //A combination of a few example sketches from around the web. Removed serial data output and gyro/thermometer reading/output to maximize latency.
- #include<Wire.h>
- const int MPU=0x68; // I2C address of the MPU-6050
- int16_t AcX,AcY;
- #include <FastLED.h> // FastLED library
- #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 255 // How bright do we want to go
- CRGB Xleds[NUM_LEDS]; // Initialize our array
- CRGB Yleds[NUM_LEDS];
- 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, 13, COLOR_ORDER>(Xleds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- LEDS.addLeds<LED_TYPE, 12, COLOR_ORDER>(Yleds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.setBrightness(BRIGHTNESS);
- }
- void loop(){
- Wire.beginTransmission(MPU);
- Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
- Wire.endTransmission(false);
- Wire.requestFrom(MPU,4,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)
- // 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
- // Figure out how many leds would be lit, and in what direction
- int XLit = abs(AcX) / (25000/(NUM_LEDS/2));
- int YLit = abs(AcY) / (25000/(NUM_LEDS/2));
- // sanity check, just in case we get larger values than we're expecting!
- if(XLit > (NUM_LEDS/2)) {
- XLit = NUM_LEDS/2;
- }
- // empty out the existing leds
- fill_solid(Xleds,NUM_LEDS,CRGB::Black);
- if(AcX < 0) {
- // we want to light up numLit leds, starting from the midpoint, going down
- for(int xi = 1; xi <= XLit; xi++) {
- Xleds[(NUM_LEDS/2)-xi] = CRGB::Green;
- }
- } else {
- // we want to light up numLit leds, starting from the midpoint, going forward
- for(int xi = 0; xi < XLit; xi++) {
- Xleds[(NUM_LEDS/2)+xi] = CRGB::Yellow;
- }
- }
- // sanity check, just in case we get larger values than we're expecting!
- if(YLit > (NUM_LEDS/2)) {
- YLit = NUM_LEDS/2;
- }
- // empty out the existing leds
- fill_solid(Yleds,NUM_LEDS,CRGB::Black);
- if(AcY < 0) {
- // we want to light up numLit leds, starting from the midpoint, going down
- for(int yi = 1; yi <= YLit; yi++) {
- Yleds[(NUM_LEDS/2)-yi] = CRGB::White;
- }
- } else {
- // we want to light up numLit leds, starting from the midpoint, going forward
- for(int yi = 0; yi < YLit; yi++) {
- Yleds[(NUM_LEDS/2)+yi] = CRGB::Red;
- }
- }
- LEDS.show();
- }
Advertisement
Add Comment
Please, Sign In to add comment