humblehacker

fastLED Planetarium

Mar 17th, 2015
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. // Based on "ColorTwinkles.ino" by Mark Kriegsman
  2. // https://gist.github.com/kriegsman/5408ecd397744ba0393e
  3.  
  4.  
  5. #include "FastLED.h"
  6.  
  7. #define BLUEPIN 10
  8.  
  9. #define LED_PIN 3
  10. #define LED_TYPE WS2812
  11. #define COLOR_ORDER RGB
  12. #define NUM_LEDS 33
  13. CRGB leds[NUM_LEDS];
  14.  
  15. // Twinkling 'holiday' lights that fade up and down in brightness.
  16. // Colors are chosen from a palette; a few palettes are provided.
  17. //
  18. // The basic operation is that all pixels stay black until they
  19. // are 'seeded' with a relatively dim color. The dim colors
  20. // are repeatedly brightened until they reach full brightness, then
  21. // are darkened repeatedly until they are fully black again.
  22. //
  23. // A set of 'directionFlags' is used to track whether a given
  24. // pixel is presently brightening up or darkening down.
  25. //
  26. // For illustration purposes, two implementations of directionFlags
  27. // are provided: a simple one-byte-per-pixel flag, and a more
  28. // complicated, more compact one-BIT-per-pixel flag.
  29. //
  30. // Darkening colors accurately is relatively easy: scale down the
  31. // existing color channel values. Brightening colors is a bit more
  32. // error prone, as there's some loss of precision. If your colors
  33. // aren't coming our 'right' at full brightness, try increasing the
  34. // STARTING_BRIGHTNESS value.
  35. //
  36. // -Mark Kriegsman, December 2014
  37.  
  38. #define MASTER_BRIGHTNESS 96
  39.  
  40. #define STARTING_BRIGHTNESS 64
  41. #define FADE_IN_SPEED 32
  42. #define FADE_OUT_SPEED 20
  43. #define DENSITY 255
  44.  
  45. void showAnalogRGB( const CRGB& rgb)
  46. {
  47. analogWrite(BLUEPIN, rgb.b );
  48. }
  49.  
  50. void colorBars()
  51. {
  52. showAnalogRGB( CRGB::Blue ); delay(500);
  53. showAnalogRGB( CRGB::Black ); delay(500);
  54. }
  55.  
  56. void setup() {
  57. pinMode(BLUEPIN, OUTPUT);
  58. delay(3000);
  59. FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  60. FastLED.setBrightness(MASTER_BRIGHTNESS);
  61.  
  62.  
  63. }
  64.  
  65. void loop()
  66. {
  67. static uint8_t hue;
  68. hue = hue + 1;
  69. // Use FastLED automatic HSV->RGB conversion
  70. showAnalogRGB( CHSV( hue, 255, 255) );
  71.  
  72. delay(20);
  73. }
  74.  
  75.  
  76.  
  77. {
  78. chooseColorPalette();
  79. colortwinkles();
  80. FastLED.show();
  81. FastLED.delay(20);
  82. }
  83.  
  84.  
  85. CRGBPalette16 gPalette;
  86.  
  87. void chooseColorPalette()
  88. {
  89. uint8_t numberOfPalettes = 5;
  90. uint8_t secondsPerPalette = 10;
  91. uint8_t whichPalette = (millis()/(1000*secondsPerPalette)) % numberOfPalettes;
  92.  
  93. CRGB r(CRGB::Red), b(CRGB::Blue), w(85,85,85), g(CRGB::Green), W(CRGB::White), l(0xE1A024);
  94.  
  95. switch( whichPalette) {
  96. case 0: // Red, Green, and White
  97. //gPalette = CRGBPalette16( r,r,r,r, r,r,r,r, g,g,g,g, w,w,w,w );
  98. break;
  99. case 1: // Blue and White
  100. gPalette = CRGBPalette16( b,b,b,b, b,b,b,b, w,w,w,w, w,w,w,w );
  101. gPalette = CloudColors_p; // Blues and whites!
  102. break;
  103. case 2: // Rainbow of colors
  104. //gPalette = RainbowColors_p;
  105. break;
  106. case 3: // Incandescent "fairy lights"
  107. // gPalette = CRGBPalette16( l,l,l,l, l,l,l,l, l,l,l,l, l,l,l,l );
  108. break;
  109. case 4: // Snow
  110. //gPalette = CRGBPalette16( W,W,W,W, w,w,w,w, w,w,w,w, w,w,w,w );
  111. break;
  112. }
  113. }
  114.  
  115. enum { GETTING_DARKER = 0, GETTING_BRIGHTER = 1 };
  116.  
  117. void colortwinkles()
  118. {
  119. // Make each pixel brighter or darker, depending on
  120. // its 'direction' flag.
  121. brightenOrDarkenEachPixel( FADE_IN_SPEED, FADE_OUT_SPEED);
  122.  
  123. // Now consider adding a new random twinkle
  124. if( random8() < DENSITY ) {
  125. int pos = random16(NUM_LEDS);
  126. if( !leds[pos]) {
  127. leds[pos] = ColorFromPalette( gPalette, random8(), STARTING_BRIGHTNESS, NOBLEND);
  128. setPixelDirection(pos, GETTING_BRIGHTER);
  129. }
  130. }
  131. }
  132.  
  133. void brightenOrDarkenEachPixel( fract8 fadeUpAmount, fract8 fadeDownAmount)
  134. {
  135. for( uint16_t i = 0; i < NUM_LEDS; i++) {
  136. if( getPixelDirection(i) == GETTING_DARKER) {
  137. // This pixel is getting darker
  138. leds[i] = makeDarker( leds[i], fadeDownAmount);
  139. } else {
  140. // This pixel is getting brighter
  141. leds[i] = makeBrighter( leds[i], fadeUpAmount);
  142. // now check to see if we've maxxed out the brightness
  143. if( leds[i].r == 255 || leds[i].g == 255 || leds[i].b == 255) {
  144. // if so, turn around and start getting darker
  145. setPixelDirection(i, GETTING_DARKER);
  146. }
  147. }
  148. }
  149. }
  150.  
  151. CRGB makeBrighter( const CRGB& color, fract8 howMuchBrighter)
  152. {
  153. CRGB incrementalColor = color;
  154. incrementalColor.nscale8( howMuchBrighter);
  155. return color + incrementalColor;
  156. }
  157.  
  158. CRGB makeDarker( const CRGB& color, fract8 howMuchDarker)
  159. {
  160. CRGB newcolor = color;
  161. newcolor.nscale8( 255 - howMuchDarker);
  162. return newcolor;
  163. }
  164.  
  165.  
  166. // For illustration purposes, there are two separate implementations
  167. // provided here for the array of 'directionFlags':
  168. // - a simple one, which uses one byte (8 bits) of RAM for each pixel, and
  169. // - a compact one, which uses just one BIT of RAM for each pixel.
  170.  
  171. // Set this to 1 or 8 to select which implementation
  172. // of directionFlags is used. 1=more compact, 8=simpler.
  173. #define BITS_PER_DIRECTION_FLAG 1
  174.  
  175.  
  176. #if BITS_PER_DIRECTION_FLAG == 8
  177. // Simple implementation of the directionFlags array,
  178. // which takes up one byte (eight bits) per pixel.
  179. uint8_t directionFlags[NUM_LEDS];
  180.  
  181. bool getPixelDirection( uint16_t i) {
  182. return directionFlags[i];
  183. }
  184.  
  185. void setPixelDirection( uint16_t i, bool dir) {
  186. directionFlags[i] = dir;
  187. }
  188. #endif
  189.  
  190.  
  191. #if BITS_PER_DIRECTION_FLAG == 1
  192. // Compact (but more complicated) implementation of
  193. // the directionFlags array, using just one BIT of RAM
  194. // per pixel. This requires a bunch of bit wrangling,
  195. // but conserves precious RAM. The cost is a few
  196. // cycles and about 100 bytes of flash program memory.
  197. uint8_t directionFlags[ (NUM_LEDS+7) / 8];
  198.  
  199. bool getPixelDirection( uint16_t i) {
  200. uint16_t index = i / 8;
  201. uint8_t bitNum = i & 0x07;
  202. // using Arduino 'bitRead' function; expanded code below
  203. return bitRead( directionFlags[index], bitNum);
  204. // uint8_t andMask = 1 << bitNum;
  205. // return (directionFlags[index] & andMask) != 0;
  206. }
  207.  
  208. void setPixelDirection( uint16_t i, bool dir) {
  209. uint16_t index = i / 8;
  210. uint8_t bitNum = i & 0x07;
  211. // using Arduino 'bitWrite' function; expanded code below
  212. bitWrite( directionFlags[index], bitNum, dir);
  213. // uint8_t orMask = 1 << bitNum;
  214. // uint8_t andMask = 255 - orMask;
  215. // uint8_t value = directionFlags[index] & andMask;
  216. // if( dir ) {
  217. // value += orMask;
  218. // }
  219. // directionFlags[index] = value;
  220. }
  221. #endif
  222.  
  223. OceanVisulaight.ino:73:1: error: expected unqualified-id before '{' token
  224. Error compiling
Advertisement
Add Comment
Please, Sign In to add comment