Guest User

g meter

a guest
Oct 22nd, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. //Kyle Halvorson 10/21/14
  2. //Toyota Supra G-Meter Project V1.4
  3. //Accelerometer sensor reading and mapping to two WS2811 LED strips
  4. //A combination of a few example sketches from around the web. Removed serial data output and gyro/thermometer reading/output to maximize latency.
  5.  
  6. #include<Wire.h>
  7. const int MPU=0x68; // I2C address of the MPU-6050
  8. int16_t AcX,AcY;
  9.  
  10. #include <FastLED.h> // FastLED library
  11.  
  12. #define NUM_LEDS 10 // Number of LED's
  13. #define COLOR_ORDER GRB // Change the order as necessary
  14. #define LED_TYPE WS2811 // What kind of strip are you using?
  15. #define BRIGHTNESS 255 // How bright do we want to go
  16.  
  17. CRGB Xleds[NUM_LEDS]; // Initialize our array
  18. CRGB Yleds[NUM_LEDS];
  19.  
  20.  
  21.  
  22. void setup(){
  23. Wire.begin();
  24. Wire.beginTransmission(MPU);
  25. Wire.write(0x6B); // PWR_MGMT_1 register
  26. Wire.write(0); // set to zero (wakes up the MPU-6050)
  27. Wire.endTransmission(true);
  28. Serial.begin(9600);
  29.  
  30. LEDS.addLeds<LED_TYPE, 13, COLOR_ORDER>(Xleds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  31. LEDS.addLeds<LED_TYPE, 12, COLOR_ORDER>(Yleds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  32. FastLED.setBrightness(BRIGHTNESS);
  33. }
  34. void loop(){
  35. Wire.beginTransmission(MPU);
  36. Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
  37. Wire.endTransmission(false);
  38. Wire.requestFrom(MPU,4,true); // request a total of 14 registers
  39. AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  40. AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  41. // AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  42. // Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  43. // Serial.print("AcX = "); Serial.print(AcX);
  44. // Serial.print(" | AcY = "); Serial.print(AcY);
  45. // Serial.print(" | AcZ = "); Serial.print(AcZ);
  46. // Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53); //equation for temperature in degrees C from datasheet
  47.  
  48.  
  49. // Figure out how many leds would be lit, and in what direction
  50. int XLit = abs(AcX) / (25000/(NUM_LEDS/2));
  51. int YLit = abs(AcY) / (25000/(NUM_LEDS/2));
  52.  
  53. // sanity check, just in case we get larger values than we're expecting!
  54. if(XLit > (NUM_LEDS/2)) {
  55. XLit = NUM_LEDS/2;
  56. }
  57.  
  58. // empty out the existing leds
  59. fill_solid(Xleds,NUM_LEDS,CRGB::Black);
  60.  
  61. if(AcX < 0) {
  62. // we want to light up numLit leds, starting from the midpoint, going down
  63. for(int xi = 1; xi <= XLit; xi++) {
  64. Xleds[(NUM_LEDS/2)-xi] = CRGB::Green;
  65. }
  66. } else {
  67. // we want to light up numLit leds, starting from the midpoint, going forward
  68. for(int xi = 0; xi < XLit; xi++) {
  69. Xleds[(NUM_LEDS/2)+xi] = CRGB::Yellow;
  70. }
  71. }
  72.  
  73.  
  74.  
  75. // sanity check, just in case we get larger values than we're expecting!
  76. if(YLit > (NUM_LEDS/2)) {
  77. YLit = NUM_LEDS/2;
  78. }
  79.  
  80. // empty out the existing leds
  81. fill_solid(Yleds,NUM_LEDS,CRGB::Black);
  82.  
  83. if(AcY < 0) {
  84. // we want to light up numLit leds, starting from the midpoint, going down
  85. for(int yi = 1; yi <= YLit; yi++) {
  86. Yleds[(NUM_LEDS/2)-yi] = CRGB::White;
  87. }
  88. } else {
  89. // we want to light up numLit leds, starting from the midpoint, going forward
  90. for(int yi = 0; yi < YLit; yi++) {
  91. Yleds[(NUM_LEDS/2)+yi] = CRGB::Red;
  92. }
  93. }
  94.  
  95. LEDS.show();
  96. }
Advertisement
Add Comment
Please, Sign In to add comment