Guest User

5PointStar

a guest
Nov 29th, 2016
1,776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.24 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. // Good Reference!
  4. // http://fastled.io/docs/3.1/group___colorutils.html#gafcc7dac88e25736ebc49a9faf2a1c2e2
  5.  
  6.  
  7. #define DATA_PIN 10
  8. #define BRIGHTNESS_PIN A1
  9. #define COLOR_ORDER RGB
  10. #define LED_TYPE WS2812
  11. #define NUM_LEDS 100
  12.  
  13. #define FRAMES_PER_SECOND 120
  14.  
  15.  
  16.  
  17. // brightness control added 3/6/2016
  18. // original limits:
  19. //#define MASTER_BRIGHTNESS 128 // Set the master brigtness value [should be greater then min_brightness value].
  20. //#define MIN_BRIGHTNESS 30 // Set a minimum brightness level.
  21. #define MASTER_BRIGHTNESS 192 // Set the master brigtness value [should be greater then min_brightness value].
  22. #define MIN_BRIGHTNESS 10 // Set a minimum brightness level.
  23. int potValA; // Variable to store potentiometer A value (for brightness A)
  24. uint8_t brightnessa; // Mapped master brightness for color A based on potentiometer reading.
  25.  
  26. #define NUM_VIRTUAL_LEDS NUM_LEDS+1
  27. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  28. CRGB leds[NUM_VIRTUAL_LEDS];
  29.  
  30.  
  31.  
  32.  
  33. // Added November 2, 2016
  34. // star pattern
  35. const uint8_t starCount = 70;
  36.  
  37. static const uint8_t starArray[] PROGMEM =
  38. { 0, 1, 2, 3, 4, 5 , 6,
  39. 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
  40. 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  41. 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
  42. 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
  43. 93, 94, 95, 96, 97, 98, 99 };
  44.  
  45.  
  46. const uint8_t innerCount = 30;
  47.  
  48. static const uint8_t innerArray[] PROGMEM =
  49. { 7, 8, 9, 10, 11, 12,
  50. 67, 68, 69, 70, 71, 72,
  51. 27, 28, 29, 30, 31, 32,
  52. 87, 88, 89, 90, 91, 92,
  53. 47, 48, 49, 50, 51, 52
  54. };
  55.  
  56. uint8_t offset_for_star_rotation = 0;
  57.  
  58. uint16_t holdTime = 60; // Milliseconds to hold position before advancing.
  59. uint8_t spacing = 20; // Sets pixel spacing. [Use 2 or greater]
  60. int8_t delta = 1; // Sets forward or backwards direction amount. (Can be negative.)
  61. uint8_t width = 3; // Can increase the number of pixels (width) of the chase. [1 or greater]
  62.  
  63. uint8_t hue = 60; // Starting color (marque_v3)
  64.  
  65. boolean fadingTail = 1; // Add fading tail? [1=true, 0=falue]
  66. //uint8_t fadeRate = 150; // How fast to fade out tail. [0-255]
  67. uint8_t fadeRate = 70; // How fast to fade out tail. [0-255]
  68.  
  69. uint8_t hue2_shift = 60; // Hue shift for secondary color. Use 0 for no shift. [0-255]
  70. boolean DEBUG = 0; // Print some info to serial monitor. [1=true, 0=falue]
  71.  
  72. int16_t pos; // Pixel position.
  73. int8_t advance; // Stores the advance amount.
  74. uint8_t color; // Stores a hue color.
  75.  
  76.  
  77.  
  78. // =============================================================================
  79. // setup routine
  80. // =============================================================================
  81. // put your setup code here, to run once:
  82. void setup() {
  83. delay(3000); // 3 second delay for recovery
  84. Serial.begin(57600);
  85.  
  86. // tell FastLED about the LED strip configuration
  87. FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  88. }
  89.  
  90.  
  91. // List of patterns to cycle through. Each is defined as a separate function below.
  92. typedef void (*SimplePatternList[])();
  93. //SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm };
  94. //SimplePatternList gPatterns = { fill_solid_color, trace_inner_star, rainbow, sinelon, trace_star, juggle, bpm };
  95. //SimplePatternList gPatterns = { fill_solid_color, trace_inner_star, trace_star, gradient_fill };
  96. //SimplePatternList gPatterns = { gradient_fill_2, sinelon, fill_solid_color, trace_inner_star, juggle, trace_star, rainbow, bpm };
  97.  
  98. // I love these 4 patterns -- this is a great base:
  99. // SimplePatternList gPatterns = { gradient_fill_2, fill_solid_color, trace_inner_star, trace_star };
  100. // These are 4 patterns are very energetic, they should be used intermittenly within the whole solution, maybe 1 per 2 above?
  101. // SimplePatternList gPatterns = { rainbow, sinelon, juggle, bpm };
  102.  
  103. SimplePatternList gPatterns = { marque_v3, gradient_fill_2, fill_solid_color, trace_inner_star, trace_star };
  104.  
  105.  
  106. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  107. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  108.  
  109.  
  110.  
  111. // =============================================================================
  112. // loop routine
  113. // =============================================================================
  114. // put your main code here, to run repeatedly:
  115. void loop() {
  116.  
  117. // brightness control
  118. // potValA = analogRead(BRIGHTNESS_PIN); // Read potentiometer B (for brightness).
  119. //Serial.println (potValA);
  120. // brightnessa = map(potValA, 0, 1023, MIN_BRIGHTNESS, MASTER_BRIGHTNESS);
  121. // Map value between min_brightness and MASTER brightness values.
  122. // Note: We are limiting the lowest possible brightness value to the
  123. // min_brightness value assigned up top.
  124. FastLED.setBrightness(MASTER_BRIGHTNESS);
  125. //Serial.println (brightnessa);
  126.  
  127.  
  128. // Call the current pattern function once, updating the 'leds' array
  129. gPatterns[gCurrentPatternNumber]();
  130.  
  131.  
  132. // send the 'leds' array out to the actual LED strip
  133. FastLED.show();
  134. // insert a delay to keep the framerate modest
  135. FastLED.delay(1000/FRAMES_PER_SECOND);
  136.  
  137.  
  138. // do some periodic updates
  139. EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  140. EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
  141.  
  142. EVERY_N_SECONDS(10){ // Demo: Change the hue every N seconds.
  143. hue = hue + random8(30,61); // Shift the hue to something new.
  144. }
  145.  
  146. }
  147.  
  148.  
  149.  
  150. // =============================================================================
  151. // Sub-Routines
  152. // =============================================================================
  153.  
  154.  
  155. void nextPattern()
  156. {
  157. // add one to the current pattern number, and wrap around at the end
  158. gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  159. }
  160.  
  161.  
  162.  
  163.  
  164. // ========================================
  165. // Routines for Pattern Display
  166. // ========================================
  167.  
  168. // 11/6 modified for 4 pixel trace
  169. void trace_star()
  170. {
  171.  
  172. uint8_t hue1 = random8(255);
  173.  
  174. fill_black();
  175.  
  176. for (int i=0; i<starCount; i++) {
  177. uint8_t offset = pgm_read_byte(&starArray[i]);
  178. leds[offset] = CHSV(hue1, 255, 255);
  179.  
  180. uint8_t offset2;
  181. uint8_t offset3;
  182. uint8_t offset4;
  183. if ( (i+1)<starCount ) {
  184. offset2 = pgm_read_byte(&starArray[i+1]);
  185. }
  186. else {
  187. offset2 = pgm_read_byte(&starArray[i+1-starCount]);
  188. }
  189. leds[offset2] = CHSV(hue1, 255, 255);
  190.  
  191.  
  192. if ( (i+2)<starCount ) {
  193. offset3 = pgm_read_byte(&starArray[i+2]);
  194. }
  195. else {
  196. offset3 = pgm_read_byte(&starArray[i+2-starCount]);
  197. }
  198. leds[offset3] = CHSV(hue1, 255, 255);
  199.  
  200. if ( (i+3)<starCount ) {
  201. offset4 = pgm_read_byte(&starArray[i+3]);
  202. }
  203. else {
  204. offset4 = pgm_read_byte(&starArray[i+3-starCount]);
  205. }
  206. leds[offset4] = CHSV(hue1, 255, 255);
  207.  
  208. FastLED.show();
  209. leds[offset] = CRGB::Black;
  210. //delay(60);
  211. delay(40);
  212. }
  213. }
  214.  
  215.  
  216. void trace_inner_star() {
  217.  
  218. uint8_t hue1 = random8(255);
  219. uint8_t offset2;
  220.  
  221. for (int i=0; i<innerCount; i++) {
  222. uint8_t offset = pgm_read_byte(&innerArray[i]);
  223.  
  224. //Serial.print ("i=");
  225. //Serial.print (i);
  226. //Serial.print (" array location = ");
  227. //Serial.println (offset);
  228.  
  229. leds[offset] = CHSV(hue1, 255, 255);
  230.  
  231.  
  232. if ( (i+1)<starCount ) {
  233. offset2 = pgm_read_byte(&innerArray[i+1]);
  234. }
  235. else {
  236. offset2 = pgm_read_byte(&innerArray[i+1-innerCount]);
  237. }
  238. leds[offset2] = CHSV(hue1, 255, 255);
  239.  
  240.  
  241. FastLED.show();
  242. leds[offset] = CRGB::Black;
  243. delay(35);
  244. }
  245. }
  246.  
  247.  
  248. void fill_solid_color()
  249. {
  250.  
  251. uint8_t hue1 = random8(255);
  252. uint8_t hue2 = random8(255);
  253.  
  254. for (int i=0; i<NUM_LEDS; i++) {
  255. Serial.println (i);
  256. /*
  257. leds[i] = CRGB::Red;
  258. leds[i-1] = CRGB::Blue;
  259. FastLED.show();
  260. leds[i] = CRGB::Black;
  261. */
  262. leds[i] = CHSV( hue1, 255, 255);
  263. leds[i-1] = CHSV( hue2, 255, 255);
  264. FastLED.show();
  265. leds[i] = CRGB::Black;
  266.  
  267. //delay(30);
  268. delay(20);
  269. }
  270. }
  271.  
  272. void fill_black()
  273. {
  274. FastLED.clear();
  275. //fill_solid(leds, NUM_LEDS, CRGB::Black);
  276. }
  277.  
  278.  
  279. void rainbow()
  280. {
  281. // FastLED's built-in rainbow generator
  282. fill_rainbow( leds, NUM_LEDS, gHue, 7);
  283. }
  284.  
  285.  
  286. void sinelon()
  287. {
  288. // a colored dot sweeping back and forth, with fading trails
  289. fadeToBlackBy( leds, NUM_LEDS, 20);
  290. int pos = beatsin16(13,0,NUM_LEDS);
  291. leds[pos] += CHSV( gHue, 255, 192);
  292. }
  293.  
  294. void bpm()
  295. {
  296. // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  297. uint8_t BeatsPerMinute = 62;
  298. CRGBPalette16 palette = PartyColors_p;
  299. uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  300. for( int i = 0; i < NUM_LEDS; i++) { //9948
  301. leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  302. }
  303. }
  304.  
  305.  
  306. void juggle() {
  307. // eight colored dots, weaving in and out of sync with each other
  308. fadeToBlackBy( leds, NUM_LEDS, 20);
  309. byte dothue = 0;
  310. for( int i = 0; i < 8; i++) {
  311. leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
  312. dothue += 32;
  313. }
  314. }
  315.  
  316.  
  317. // draws a line that fades between 2 random colors
  318. // TODO: Add logic to rotate the starting point
  319. void gradient_fill() {
  320.  
  321. // uint8_t hue1 = 60;
  322. // uint8_t hue2 = random8(255);
  323. uint8_t hue1 = random8(255);
  324. uint8_t hue2 = hue1 + random8(30,61);
  325.  
  326. for( int i = 0; i < NUM_LEDS; i++){
  327. //fill_gradient (leds, 0, CHSV(0, 255, 255), i, CHSV(96, 255, 255), SHORTEST_HUES);
  328. fill_gradient (leds, 0, CHSV(hue1, 255, 255), i, CHSV(hue2, 255, 255), SHORTEST_HUES);
  329. delay(25);
  330. FastLED.show();
  331. //FastLED.clear();
  332. }
  333. }
  334.  
  335. // draws a line that fades between 2 random colors
  336. // Added logic to rotate the starting corner
  337. void gradient_fill_2() {
  338.  
  339. uint8_t hue1 = random8(255);
  340. uint8_t hue2 = hue1 + random8(30,61);
  341. uint8_t offset2;
  342. CRGB grad[NUM_LEDS]; // A place to save the gradient colors. (Don't edit this)
  343. fill_gradient (grad, 0, CHSV(hue1, 255, 255), NUM_LEDS, CHSV(hue2, 255, 255), SHORTEST_HUES);
  344.  
  345. // display each pixel one at a time
  346. for( int i = 0; i < NUM_LEDS; i++){
  347.  
  348. if ( (i+offset_for_star_rotation) < NUM_LEDS ) {
  349. offset2 = i+offset_for_star_rotation;
  350. }
  351. else {
  352. offset2 = i+offset_for_star_rotation-NUM_LEDS;
  353. }
  354.  
  355. leds[offset2] = grad[i];
  356. //delay(25);
  357. delay(12);
  358. FastLED.show();
  359. }
  360.  
  361. // rotate star starting point for next loop
  362. offset_for_star_rotation += 20;
  363. if (offset_for_star_rotation >= 100) {
  364. offset_for_star_rotation = 0;
  365. }
  366.  
  367. FastLED.clear();
  368. }
  369.  
  370. // Adapted from code by Marc Miller. Original Header:
  371. //
  372. //***************************************************************
  373. // Marquee fun (v3)
  374. // Pixel position down the strip comes from this formula:
  375. // pos = spacing * (i-1) + spacing
  376. // i starts at 0 and is incremented by +1 up to NUM_LEDS/spacing.
  377. //
  378. // Marc Miller, May 2016
  379. //***************************************************************
  380. void marque_v3() {
  381.  
  382.  
  383. for (uint8_t i=0; i<(NUM_LEDS/spacing); i++){
  384. for (uint8_t w = 0; w<width; w++){
  385. pos = (spacing * (i-1) + spacing + advance + w) % NUM_LEDS;
  386. if ( w % 2== 0 ){ // Is w even or odd?
  387. color = hue;
  388. } else {
  389. color = hue + hue2_shift;
  390. }
  391.  
  392. leds[pos] = CHSV(color,255,255);
  393. }
  394.  
  395. if (DEBUG==1) { // Print out lit pixels if DEBUG is true.
  396. Serial.print(" "); Serial.print(pos);
  397. }
  398. delay(10);
  399. }
  400. if (DEBUG==1) { Serial.println(" "); }
  401. FastLED.show();
  402.  
  403. // Fade out tail or set back to black for next loop around.
  404. if (fadingTail == 1) {
  405. fadeToBlackBy(leds, NUM_LEDS,fadeRate);
  406. } else {
  407. for (uint8_t i=0; i<(NUM_LEDS/spacing); i++){
  408. for (uint8_t w = 0; w<width; w++){
  409. pos = (spacing * (i-1) + spacing + advance + w) % NUM_LEDS;
  410. leds[pos] = CRGB::Black;
  411. }
  412. }
  413. }
  414.  
  415. // Advance pixel postion down strip, and rollover if needed.
  416. advance = (advance + delta + NUM_LEDS) % NUM_LEDS;
  417. }
  418.  
  419.  
  420. // END
Advertisement
Add Comment
Please, Sign In to add comment