Guest User

FastLed WS2812B with ADXL345

a guest
Sep 15th, 2016
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.31 KB | None | 0 0
  1. * Accelerometer connection pins (I2C) to Arduino are shown below:
  2.  
  3. Arduino     Accelerometer ADXL345
  4.   A5            SCL
  5.   A4            SDA
  6.   3.3V          CS
  7.   3.3V          VCC
  8.   GND           GND
  9. */
  10. #include "FastLED.h"
  11.  
  12. FASTLED_USING_NAMESPACE
  13.  
  14. #include <Wire.h>
  15. #include <ADXL345.h>
  16.  
  17. #if FASTLED_VERSION < 3001000
  18. #error "Requires FastLED 3.1 or later; check github for latest code."
  19. #endif
  20.  
  21. #define DATA_PIN    6
  22. //#define CLK_PIN   4
  23. #define LED_TYPE    WS2812B
  24. #define COLOR_ORDER GRB
  25. #define NUM_LEDS    30
  26. CRGB leds[NUM_LEDS];
  27.  
  28. #define BRIGHTNESS          96
  29. #define FRAMES_PER_SECOND  120
  30. int ledMode = 1;  
  31.  
  32. ADXL345 adxl; //variable adxl is an instance of the ADXL345 library
  33.  
  34. int x,y,z;  
  35. int rawX, rawY, rawZ;
  36. float X, Y, Z;
  37. float rollrad, pitchrad;
  38. float rolldeg, pitchdeg;
  39. float totalAccel;
  40.  
  41. #define LEDINTERVAL (1000UL)
  42. unsigned long rolltime = millis() + LEDINTERVAL;
  43.  
  44. void setup(){
  45.   Serial.begin(115200);
  46.   adxl.powerOn();
  47.   delay(3000); // 3 second delay for recovery
  48.  
  49.   // tell FastLED about the LED strip configuration
  50.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  51.   //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  52.  
  53.   // set master brightness control
  54.   FastLED.setBrightness(BRIGHTNESS);
  55. }
  56.  
  57. // List of patterns to cycle through.  Each is defined as a separate function below.
  58.  
  59. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  60.  
  61.  
  62.  
  63.  
  64. void loop(){
  65.  
  66.  
  67.   // signed comparison for proper handling of timer rollover
  68.  
  69.  // if((long)(millis() - rolltime) >= 0) {
  70.  
  71.   ledMode = 1;
  72.   adxl.readAccel(&x, &y, &z); //read the accelerometer values and store them in variables  x,y,z
  73.   // Output (x,y,z) on horizontal plane should be approximately (0,0,255)
  74.   // the following 3 lines is for an offset
  75.   rawX=x-7;
  76.   rawY=y-6;
  77.   rawZ=z+10;
  78.    
  79.   X = rawX/256.00; // used for angle calculations
  80.   Y = rawY/256.00; // used for angle calculations
  81.   Z = rawZ/256.00; // used for angle calculations
  82.    
  83.   rollrad = atan(Y/sqrt(X*X+Z*Z));  // calculated angle in radians
  84.   pitchrad = atan(X/sqrt(Y*Y+Z*Z)); // calculated angle in radians
  85.  
  86.   rolldeg = 180*(atan(Y/sqrt(X*X+Z*Z)))/PI; // calculated angle in degrees
  87.   pitchdeg = 180*(atan(X/sqrt(Y*Y+Z*Z)))/PI; // calculated angle in degrees
  88.   totalAccel = sqrt( sq(x) +sq(y) + sq(z) - sq(9.81) );
  89.   // print out values:
  90.   /*
  91.   Serial.print("x: "); Serial.print(x);    // raw data without offset
  92.   Serial.print(" y: "); Serial.print(y);     // raw data without offset
  93.   Serial.print(" z: "); Serial.print(z);     // raw data without offset
  94.   Serial.print(" rawX = "); Serial.print(rawX); // raw data with offset
  95.   Serial.print(" rawY = "); Serial.print(rawY); // raw data with offset
  96.   Serial.print(" rawZ = "); Serial.print(rawZ); // raw data with offset
  97.   Serial.print(" X = "); Serial.print(X);    // raw data with offset and divided by 256
  98.   Serial.print(" Y = "); Serial.print(Y);    // raw data with offset and divided by 256
  99.   Serial.print(" Z = "); Serial.print(Z);    // raw data with offset and divided by 256
  100. */
  101.   Serial.print("\t Angle according to x axis (Roll(deg)) = "); Serial.print(rolldeg);      // calculated angle in degrees
  102.   Serial.print("\t Angle according to y axis (Pitch(deg)) = "); Serial.println(pitchdeg);  // calculated angle in degrees
  103.   // Serial.print(" Roll(rad) = "); Serial.print(rollrad);   // calculated angle in radians
  104.   // Serial.print(" Pitch(rad) = "); Serial.print(pitchrad); // calculated angle in radians
  105.  
  106.  
  107.    if (( X > .2 ) and (totalAccel > 10 ))
  108.    {
  109.     ledMode = 3;
  110.    }
  111.    else if ( ( X < -.11 ) and (totalAccel > 10 ))
  112.    {
  113.     ledMode= 2;
  114.    }
  115. Serial.print(" LedMode = "); Serial.print(ledMode);  
  116. Serial.print("Total Accel: "); Serial.print(totalAccel);
  117. // rolltime += LEDINTERVAL;
  118. //  }  
  119.    
  120.  
  121.    
  122.    if (ledMode != 999) {
  123.  
  124.      switch (ledMode) {
  125.       case  1: sinelon(); break;
  126.       case  2: juggle(); break;
  127.       case  3: bpm(); break;
  128.       }
  129.       }
  130.  
  131.  
  132.   show_at_max_brightness_for_power();
  133.   delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
  134. //  FastLED.show();  
  135. //  FastLED.delay(1000/FRAMES_PER_SECOND);
  136. //  EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  137.  
  138. }
  139.  
  140. void confetti()
  141. {
  142.   // random colored speckles that blink in and fade smoothly
  143.   fadeToBlackBy( leds, NUM_LEDS, 10);
  144.   int pos = random16(NUM_LEDS);
  145.   leds[pos] += CHSV( gHue + random8(64), 200, 255);
  146. }
  147.  
  148. void sinelon()
  149. {
  150.   // a colored dot sweeping back and forth, with fading trails
  151.   fadeToBlackBy( leds, NUM_LEDS, 20);
  152.   int pos = beatsin16(13,0,NUM_LEDS);
  153.   leds[pos] += CHSV( gHue, 255, 192);
  154. }
  155.  
  156. void bpm()
  157. {
  158.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  159.   uint8_t BeatsPerMinute = 62;
  160.   CRGBPalette16 palette = PartyColors_p;
  161.   uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  162.   for( int i = 0; i < NUM_LEDS; i++) { //9948
  163.     leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  164.   }
  165. }
  166.  
  167. void juggle() {
  168.   // eight colored dots, weaving in and out of sync with each other
  169.   fadeToBlackBy( leds, NUM_LEDS, 20);
  170.   byte dothue = 0;
  171.   for( int i = 0; i < 8; i++) {
  172.     leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
  173.     dothue += 32;
  174.   }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment