Advertisement
GyroGearloose

FastLED Neopixel Ring & Rotating Beacon

Nov 16th, 2015
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.81 KB | None | 0 0
  1. /*
  2. FastLED Samples for Neopixel Ring
  3. Original sketches from the FastLED community
  4. Some comments by Gyro Gearloose  :)
  5. */
  6.  
  7.  
  8. #include <FastLED.h>
  9.  
  10. #define DATA_PIN    6
  11. //#define CLK_PIN     8
  12. //#define LED_TYPE    LPD8806
  13. //#define COLOR_ORDER GRB
  14. //#define LED_TYPE    APA102
  15. //#define COLOR_ORDER GBR      
  16. #define LED_TYPE    WS2811
  17. #define COLOR_ORDER GRB         // check your color order !
  18.  
  19. #define LED_COUNT    24        
  20. CRGB leds[LED_COUNT];
  21.  
  22. #define BRIGHTNESS         200
  23. #define FRAMES_PER_SECOND  120
  24.  
  25. int BOTTOM_INDEX = 0;
  26. int TOP_INDEX = int(LED_COUNT/2);
  27. int EVENODD = LED_COUNT%2;
  28. int ledsX[LED_COUNT][3];     //-ARRAY FOR COPYING WHATS IN THE LED STRIP CURRENTLY (FOR CELL-AUTOMATA, MARCH, ETC)
  29.  
  30. int thisdelay = 20;          //-FX LOOPS DELAY VAR
  31. int thisstep = 10;           //-FX LOOPS DELAY VAR
  32. int thishue = 0;             //-FX LOOPS DELAY VAR
  33. int thissat = 255;           //-FX LOOPS DELAY VAR
  34. int max_bright = 255;        //-SET MAX BRIGHTNESS TO 1/2
  35.  
  36. int thisindex = 0;           //-SET SINGLE LED VAR
  37. int thisRED = 0;
  38. int thisGRN = 0;
  39. int thisBLU = 0;
  40.  
  41. //---LED FX VARS
  42. int idex = 0;                //-LED INDEX (0 to LED_COUNT-1
  43. int ihue = 0;                //-HUE (0-255)
  44. int ibright = 0;             //-BRIGHTNESS (0-255)
  45. int isat = 0;                //-SATURATION (0-255)
  46. int bouncedirection = 0;     //-SWITCH FOR COLOR BOUNCE (0-1)
  47. float tcount = 0.0;          //-INC VAR FOR SIN LOOPS
  48. int lcount = 0;              //-ANOTHER COUNTING VAR
  49.  
  50. //---FIND INDEX OF ANTIPODAL OPPOSITE LED
  51. int antipodal_index(int i) {
  52.   int iN = i + TOP_INDEX;
  53.   if (i >= TOP_INDEX) {iN = ( i + TOP_INDEX ) % LED_COUNT; }
  54.   return iN;
  55. }
  56.  
  57.  
  58. void setup() {
  59.   delay(2000); // 2 second delay for recovery
  60.  
  61.   // tell FastLED about the LED strip configuration
  62.   //FastLED.addLeds<LED_TYPE, DATA_PIN, CLOCK_PIN, COLOR_ORDER, DATA_RATE_MHZ(5)>(leds, LED_COUNT);   // you can lower the data rate (1..5 MHZ)
  63.   //FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, LED_COUNT).setCorrection(TypicalLEDStrip);
  64.   FastLED.addLeds<LED_TYPE,DATA_PIN, COLOR_ORDER>(leds, LED_COUNT).setCorrection(DirectSunlight);    //CarbonArc
  65.  
  66.   // set master brightness control
  67.   FastLED.setBrightness(BRIGHTNESS);
  68. }
  69.  
  70. // List of patterns to cycle through.  Each is defined as a separate function below.
  71.  typedef void (*SimplePatternList[])();
  72.  
  73.  SimplePatternList gPatterns = {
  74.  ems_lightsONE,
  75.  ems_lightsALL,
  76.  pulse_one_color_all,
  77.  ems_lightsSTROBE,
  78.  sinelon,
  79.  kitt                    // last has no comma
  80.  } ;
  81.  
  82.  uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  83.  uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  84.  
  85.  void loop()
  86.  {
  87.   // Call the current pattern function once, updating the 'leds' array
  88.   gPatterns[gCurrentPatternNumber]();
  89.  
  90.   // send the 'leds' array out to the actual LED strip
  91.   FastLED.show();                                                // taken out for Power Management
  92.   // insert a delay to keep the framerate modest
  93.   FastLED.delay(1000/FRAMES_PER_SECOND);
  94.   EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
  95.  
  96.   EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow;
  97.                                          // if you take this out the color will stay red in the sinelon function
  98.  }
  99.  
  100.  #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  101.  void nextPattern()
  102.  {
  103.   // add one to the current pattern number, and wrap around at the end
  104.   gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  105.  }
  106.  
  107. void one_color_all(int cred, int cgrn, int cblu) {       //-SET ALL LEDS TO ONE COLOR
  108.     for(int i = 0 ; i < LED_COUNT; i++ ) {
  109.       leds[i].setRGB( cred, cgrn, cblu);
  110.     }
  111. }
  112.  
  113. void one_color_allHSV(int ahue) {    //-SET ALL LEDS TO ONE COLOR (HSV)
  114.     for(int i = 0 ; i < LED_COUNT; i++ ) {
  115.       leds[i] = CHSV(ahue, thissat, 255);
  116.     }
  117. }
  118.  
  119. void ems_lightsONE() {                    //-m7-EMERGENCY LIGHTS (TWO COLOR SINGLE LED)
  120.   idex++;
  121.   if (idex >= LED_COUNT) {idex = 0;}
  122.   int idexR = idex;
  123.   int idexB = antipodal_index(idexR);
  124.   int thathue = (thishue + 160) % 255;  // blue
  125.  
  126.   for(int i = 0; i < LED_COUNT; i++ ) {
  127.     if (i == idexR) {leds[i] = CHSV(thishue, thissat, 255);}
  128. //    else if (i == idexB) {leds[i] = CHSV(thathue, thissat, 255);}   // if you want blue
  129.     else if (i == idexB) {leds[i] = CRGB(0,0,0);}    
  130.  
  131.     else {leds[i] = CHSV(0, 0, 0);}
  132.   }
  133.   FastLED.show();  
  134.   delay(thisdelay + 10);    // set here the speed
  135. }
  136.  
  137. void ems_lightsALL() {                  //-m8-EMERGENCY LIGHTS (TWO COLOR SOLID)
  138.   idex++;
  139.   if (idex >= LED_COUNT) {idex = 0;}
  140.   int idexR = idex;
  141.   int idexB = antipodal_index(idexR);
  142.   int thathue = (thishue + 160) % 255;
  143.   leds[idexR] = CHSV(thishue, thissat, 255);
  144. //  leds[idexB] = CHSV(thathue, thissat, 255);  // if you want blue
  145.   leds[idexB] = CRGB(0,0,0);  
  146.  
  147.   FastLED.show();  
  148.   delay(thisdelay);
  149. }
  150.  
  151.  
  152. void pulse_one_color_all() {              //-m10-PULSE BRIGHTNESS ON ALL LEDS TO ONE COLOR
  153.   if (bouncedirection == 0) {
  154.     ibright++;
  155.     if (ibright >= 255) {bouncedirection = 1;}
  156.   }
  157.   if (bouncedirection == 1) {
  158.     ibright = ibright - 1;
  159.     if (ibright <= 1) {bouncedirection = 0;}        
  160.   }  
  161.     for(int idex = 0 ; idex < LED_COUNT; idex++ ) {
  162.       leds[idex] = CHSV(thishue, thissat, ibright);
  163.     }
  164.     LEDS.show();    
  165.     delay(thisdelay-10
  166.    
  167.     );
  168. }
  169.  
  170. void ems_lightsSTROBE() {                  //-m26-EMERGENCY LIGHTS (STROBE LEFT/RIGHT)
  171.   thisdelay = thisdelay + 20;
  172.   int thishue = 0;
  173.   int thathue = (thishue + 160) % 255;
  174.   for(int x = 0 ; x < 5; x++ ) {
  175.     for(int i = 0 ; i < TOP_INDEX; i++ ) {
  176.         leds[i] = CHSV(thishue, thissat, 255);
  177.     }
  178.     FastLED.show(); delay(thisdelay);
  179.     one_color_all(0, 0, 0);
  180.     FastLED.show(); delay(thisdelay);
  181.   }
  182.   for(int x = 0 ; x < 5; x++ ) {
  183.     for(int i = TOP_INDEX ; i < LED_COUNT; i++ ) {
  184.         leds[i] = CHSV(thathue, thissat, 255);
  185.     }
  186.     FastLED.show(); delay(thisdelay);
  187.     one_color_all(0, 0, 0);
  188.     FastLED.show(); delay(thisdelay);
  189.   }
  190.   thisdelay = 20;
  191. }
  192.  
  193. void kitt() {                                     //-m28-KNIGHT INDUSTIES 2000
  194.   int rand = random(0, TOP_INDEX);
  195.   for(int i = 0; i < rand; i++ ) {
  196.     leds[TOP_INDEX+i] = CHSV(thishue, thissat, 255);
  197.     leds[TOP_INDEX-i] = CHSV(thishue, thissat, 255);
  198.     FastLED.show();
  199.     delay(thisdelay-10/rand);
  200.   }
  201.   for(int i = rand; i > 0; i-- ) {
  202.     leds[TOP_INDEX+i] = CHSV(thishue, thissat, 0);
  203.     leds[TOP_INDEX-i] = CHSV(thishue, thissat, 0);
  204.     FastLED.show();
  205.     delay(thisdelay-10/rand);
  206.   }  
  207. }
  208.  
  209.  void sinelon()
  210.  {
  211.   // a colored dot sweeping back and forth, with fading trails
  212.   fadeToBlackBy( leds, LED_COUNT, 20);
  213.   int pos = beatsin16(13,0,LED_COUNT);
  214.   leds[pos] += CHSV( gHue, 255, 192);
  215.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement