Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.12 KB | None | 0 0
  1. #include <FastLED.h>
  2. #include <Adafruit_DotStar.h>
  3. #include <Adafruit_LSM303.h>
  4.  
  5. #define DATAPIN 6
  6. #define CLOCKPIN 12
  7. #define NUM_LEDS 31
  8. #define BRIGHTNESS 64
  9. #define LED_TYPE WS2811
  10. #define COLOR_ORDER RGB
  11. CRGB leds[NUM_LEDS];
  12.  
  13. #define UPDATES_PER_SECOND 2
  14.  
  15. // This example shows several ways to set up and use 'palettes' of colors
  16. // with FastLED.
  17. //
  18. // These compact palettes provide an easy way to re-colorize your
  19. // animation on the fly, quickly, easily, and with low overhead.
  20. //
  21. // USING palettes is MUCH simpler in practice than in theory, so first just
  22. // run this sketch, and watch the pretty lights as you then read through
  23. // the code. Although this sketch has eight (or more) different color schemes,
  24. // the entire sketch compiles down to about 6.5K on AVR.
  25. //
  26. // FastLED provides a few pre-configured color palettes, and makes it
  27. // extremely easy to make up your own color schemes with palettes.
  28. //
  29. // Some notes on the more abstract 'theory and practice' of
  30. // FastLED compact palettes are at the bottom of this file.
  31.  
  32.  
  33.  
  34. CRGBPalette16 currentPalette;
  35. TBlendType currentBlending;
  36. Adafruit_LSM303 lsm;
  37.  
  38. extern CRGBPalette16 myRedWhiteBluePalette;
  39. extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
  40.  
  41.  
  42. void setup() {
  43. delay( 3000 ); // power-up safety delay
  44. FastLED.addLeds<DOTSTAR, DATAPIN, CLOCKPIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  45. FastLED.setBrightness( BRIGHTNESS );
  46. Serial.begin(9600);
  47.  
  48. // Try to initialise and warn if we couldn't detect the chip
  49. if (!lsm.begin())
  50. {
  51. Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!");
  52. while (1);
  53. }
  54. // currentPalette = RainbowColors_p;
  55. // currentBlending = LINEARBLEND;
  56. }
  57.  
  58.  
  59. void loop()
  60. {
  61. // ChangePalettePeriodically();
  62. for (int i = 0; i < NUM_LEDS; i++) {
  63. lsm.read();
  64.  
  65. float Pi = 3.14159;
  66.  
  67. // Calculate the angle of the vector y,x
  68. float heading = (atan2(lsm.magData.y, lsm.magData.x) * 180) / Pi;
  69.  
  70. // Normalize to 0-360
  71. if (heading < 0)
  72. {
  73. heading = 360 + heading;
  74. }
  75.  
  76. int lolz = 0;
  77.  
  78. if (heading >= 0 && heading <= 90) {
  79. lolz = 180 + (1.1 * heading);
  80. leds[i] = CHSV(lolz, 255, 255);
  81. } else if (heading > 90 && heading <= 180) {
  82. lolz = 279 + (1.22 * (heading - 90));
  83. leds[i] = CHSV(lolz, 255, 255);
  84. } else if (heading > 180 && heading <= 270) {
  85. lolz = 20 + (0.522 * (heading - 180));
  86. leds[i] = CHSV(lolz, 255, 255);
  87. } else {
  88. lolz = 247 + (1.256 * (heading - 270));
  89. leds[i] = CHSV(lolz, 255, 255);
  90. }
  91.  
  92. // FastLED.showColor(CHSV(i, 255, 255));
  93. Serial.print("Mag X: "); Serial.print((int)lsm.magData.x); Serial.print(" ");
  94. Serial.print("Y: "); Serial.print((int)lsm.magData.y); Serial.print(" ");
  95. Serial.print("Z: "); Serial.println((int)lsm.magData.z); Serial.print(" ");
  96.  
  97. FastLED.show();
  98. FastLED.delay(UPDATES_PER_SECOND);
  99. }
  100.  
  101.  
  102. }
  103.  
  104. void FillLEDsFromPaletteColors( uint8_t colorIndex)
  105. {
  106. uint8_t brightness = 255;
  107.  
  108. for( int i = 0; i < NUM_LEDS; i++) {
  109. leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  110. colorIndex += 3;
  111. }
  112. }
  113.  
  114.  
  115. // There are several different palettes of colors demonstrated here.
  116. //
  117. // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
  118. // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
  119. //
  120. // Additionally, you can manually define your own color palettes, or you can write
  121. // code that creates color palettes on the fly. All are shown here.
  122.  
  123. void ChangePalettePeriodically()
  124. {
  125. uint8_t secondHand = (millis() / 1000) % 60;
  126. static uint8_t lastSecond = 99;
  127.  
  128. if( lastSecond != secondHand) {
  129. lastSecond = secondHand;
  130. if( secondHand == 0) { currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; }
  131. if( secondHand == 10) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; }
  132. if( secondHand == 15) { currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND; }
  133. if( secondHand == 20) { SetupPurpleAndGreenPalette(); currentBlending = LINEARBLEND; }
  134. if( secondHand == 25) { SetupTotallyRandomPalette(); currentBlending = LINEARBLEND; }
  135. if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; }
  136. if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); currentBlending = LINEARBLEND; }
  137. if( secondHand == 40) { currentPalette = CloudColors_p; currentBlending = LINEARBLEND; }
  138. if( secondHand == 45) { currentPalette = PartyColors_p; currentBlending = LINEARBLEND; }
  139. if( secondHand == 50) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; }
  140. if( secondHand == 55) { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; }
  141. }
  142. }
  143.  
  144. // This function fills the palette with totally random colors.
  145. void SetupTotallyRandomPalette()
  146. {
  147. for( int i = 0; i < 16; i++) {
  148. currentPalette[i] = CHSV( random8(), 255, random8());
  149. }
  150. }
  151.  
  152. // This function sets up a palette of black and white stripes,
  153. // using code. Since the palette is effectively an array of
  154. // sixteen CRGB colors, the various fill_* functions can be used
  155. // to set them up.
  156. void SetupBlackAndWhiteStripedPalette()
  157. {
  158. // 'black out' all 16 palette entries...
  159. fill_solid( currentPalette, 16, CRGB::Black);
  160. // and set every fourth one to white.
  161. currentPalette[0] = CRGB::White;
  162. currentPalette[4] = CRGB::White;
  163. currentPalette[8] = CRGB::White;
  164. currentPalette[12] = CRGB::White;
  165.  
  166. }
  167.  
  168. // This function sets up a palette of purple and green stripes.
  169. void SetupPurpleAndGreenPalette()
  170. {
  171. CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  172. CRGB green = CHSV( HUE_GREEN, 255, 255);
  173. CRGB black = CRGB::Black;
  174.  
  175. currentPalette = CRGBPalette16(
  176. green, green, black, black,
  177. purple, purple, black, black,
  178. green, green, black, black,
  179. purple, purple, black, black );
  180. }
  181.  
  182.  
  183. // This example shows how to set up a static color palette
  184. // which is stored in PROGMEM (flash), which is almost always more
  185. // plentiful than RAM. A static PROGMEM palette like this
  186. // takes up 64 bytes of flash.
  187. const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
  188. {
  189. CRGB::Red,
  190. CRGB::Gray, // 'white' is too bright compared to red and blue
  191. CRGB::Blue,
  192. CRGB::Black,
  193.  
  194. CRGB::Red,
  195. CRGB::Gray,
  196. CRGB::Blue,
  197. CRGB::Black,
  198.  
  199. CRGB::Red,
  200. CRGB::Red,
  201. CRGB::Gray,
  202. CRGB::Gray,
  203. CRGB::Blue,
  204. CRGB::Blue,
  205. CRGB::Black,
  206. CRGB::Black
  207. };
  208.  
  209.  
  210.  
  211. // Additionl notes on FastLED compact palettes:
  212. //
  213. // Normally, in computer graphics, the palette (or "color lookup table")
  214. // has 256 entries, each containing a specific 24-bit RGB color. You can then
  215. // index into the color palette using a simple 8-bit (one byte) value.
  216. // A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
  217. // is quite possibly "too many" bytes.
  218. //
  219. // FastLED does offer traditional 256-element palettes, for setups that
  220. // can afford the 768-byte cost in RAM.
  221. //
  222. // However, FastLED also offers a compact alternative. FastLED offers
  223. // palettes that store 16 distinct entries, but can be accessed AS IF
  224. // they actually have 256 entries; this is accomplished by interpolating
  225. // between the 16 explicit entries to create fifteen intermediate palette
  226. // entries between each pair.
  227. //
  228. // So for example, if you set the first two explicit entries of a compact
  229. // palette to Green (0,255,0) and Blue (0,0,255), and then retrieved
  230. // the first sixteen entries from the virtual palette (of 256), you'd get
  231. // Green, followed by a smooth gradient from green-to-blue, and then Blue.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement