Advertisement
Guest User

FastLED test

a guest
Dec 5th, 2021
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.80 KB | None | 0 0
  1. //#define USE_WS2812SERIAL
  2. #define USE_OCTOWS2811
  3. //#include <WS2812Serial.h>
  4. #include <OctoWS2811.h>
  5. #include <OneButton.h>
  6. #include <FastLED.h>
  7.  
  8. #define NUM_LEDS 80
  9. #define DATA_PIN 1
  10. //#define LED_TYPE WS2812B
  11. //#define LED_TYPE WS2812SERIAL
  12. #define LED_TYPE OCTOWS2811
  13. #define ORDER GRB
  14. #define BRIGHTNESS 128
  15. #define SATURATION 255
  16.  
  17. #define BUTTON_PIN 3
  18.  
  19. #define NUM_ANIMATION 18
  20.  
  21. #define FORWARD 0
  22. #define BACKWARD 1
  23. #define SLOW 250
  24. #define MEDIUM 50
  25. #define FAST 5
  26.  
  27. volatile int animation = 18;
  28. int oldAnimation = animation;
  29. CRGB leds[NUM_LEDS];
  30.  
  31. OneButton button = OneButton(BUTTON_PIN, true, true);
  32.  
  33. void setup() {
  34.   randomSeed(analogRead(0));
  35.   button.attachClick(button_handler);
  36.  
  37.   //FastLED.addLeds<1, LED_TYPE, DATA_PIN, ORDER>(leds, NUM_LEDS);
  38.   //FastLED.addLeds<LED_TYPE, DATA_PIN, ORDER>(leds, NUM_LEDS);
  39.   FastLED.addLeds<LED_TYPE, ORDER>(leds, NUM_LEDS);
  40.   Serial.begin(9600);
  41.   delay(1000);
  42. }
  43.  
  44. void button_handler() {
  45.   animation < NUM_ANIMATION ? animation++ : animation = 1;
  46.   //animation < 5 ? animation++ : animation = 1;
  47. }
  48.  
  49. void loop() {
  50.   button.tick();
  51.   EVERY_N_SECONDS( 2 ) {
  52.      animation < NUM_ANIMATION ? animation++ : animation = 1;
  53.    }
  54.   oldAnimation = animation;
  55.   switch (animation) {
  56.       case 1:
  57.         white();
  58.         break;
  59.       case 2:
  60.         colorWipe(CHSV(0, 0, 0), FAST, BACKWARD);
  61.         break;
  62.       case 3:
  63.         hue(0, SATURATION);
  64.         break;
  65.       case 4:
  66.         colorWipe(CHSV(0, 0, 0), FAST, FORWARD);
  67.         break;
  68.       case 5:
  69.         rainbow(MEDIUM);
  70.         break;
  71.       case 6:
  72.         point(MEDIUM);
  73.         break;
  74.       case 7:
  75.         allRandom(MEDIUM);
  76.         break;
  77.       case 8:
  78.         disolve(15, 100, MEDIUM);
  79.         break;
  80.       case 9:
  81.         lightning(CHSV(0, 0, BRIGHTNESS), 20, 50, MEDIUM);
  82.         break;
  83.       case 10:
  84.         lightning(15, 50, MEDIUM);
  85.         break;
  86.       case 11:
  87.         flash(CHSV(random(256), SATURATION, BRIGHTNESS), 10, SLOW);
  88.         break;
  89.       case 12:
  90.         flash(50, MEDIUM);
  91.         break;
  92.       case 13:
  93.         for (int i = 0; i < 2; i++){
  94.           cylon(CHSV(random(256), SATURATION, BRIGHTNESS), 10,FAST);
  95.         }
  96.         break;
  97.       case 14:
  98.         for (int i = 0; i < 3 && oldAnimation == animation; i++){
  99.           button.tick();
  100.           theaterChase(CHSV(random(256), SATURATION, BRIGHTNESS),10,SLOW);
  101.         }
  102.         break;
  103.       case 15:
  104.         for (int i = 0; i < 3 && oldAnimation == animation; i++) {
  105.           button.tick();
  106.           CHSV c1 = CHSV(random(256), SATURATION, BRIGHTNESS);
  107.           CHSV c2 = CHSV(random(256), SATURATION, BRIGHTNESS);
  108.           stripes(c1,c2,5);
  109.           delayForMillis(2000);
  110.           button.tick();
  111.           stripes(c2,c1,5);
  112.           delayForMillis(2000);
  113.           button.tick();
  114.         }
  115.         break;
  116.       case 16:
  117.         theaterChaseRainbow(1,MEDIUM);
  118.         break;
  119.       case 17:
  120.         black();
  121.         break;
  122.       case 18:
  123.         test();
  124.         break;  
  125.   }
  126. }
  127.  
  128. void delayForMillis(unsigned long time) {
  129.   unsigned long start = millis();
  130.   do {
  131.     button.tick();
  132.   } while (millis() - start < time);
  133. }
  134.  
  135. void test() {
  136.   button.tick();
  137.   leds[0] = CHSV(0, SATURATION, BRIGHTNESS);
  138.   leds[1] = CHSV(96, SATURATION, BRIGHTNESS);
  139.   leds[2] = CHSV(160, SATURATION, BRIGHTNESS);
  140.   FastLED.show();
  141. }
  142.  
  143. void black() {
  144.   hue(CHSV(0, 0, 0));
  145. }
  146.  
  147. void white () {
  148.   hue(0, 0);
  149. }
  150.  
  151. void hue (CHSV hue) {
  152.   for (int i = 0; i < NUM_LEDS && oldAnimation == animation; i++) {
  153.     button.tick();
  154.     leds[i] = hue;
  155.   }
  156.   FastLED.show();
  157. }
  158.  
  159. void hue (int hue, int saturation) {
  160.   /*for (int i = 0; i < NUM_LEDS && oldAnimation == animation; i++) {
  161.     button.tick();
  162.     leds[i] = CHSV(hue, saturation, BRIGHTNESS);
  163.   }*/
  164.  
  165.   button.tick();
  166.   fill_solid(leds, NUM_LEDS, CHSV(hue, saturation, BRIGHTNESS));
  167.  
  168.   FastLED.show();
  169. }
  170.  
  171. void rainbow (int speed) {
  172.   for (int j = 0; j < 255 && oldAnimation == animation; j++) {
  173.     button.tick();
  174.     for (int i = 0; i < NUM_LEDS; i++) {
  175.       leds[i] = CHSV(i - (j * 2), SATURATION, BRIGHTNESS);
  176.     }
  177.     FastLED.show();
  178.     delayForMillis(speed);
  179.   }
  180. }
  181.  
  182. void point (int speed ) {
  183.   for (int led = 0; led < NUM_LEDS && oldAnimation == animation; led++) {
  184.     button.tick();
  185.     for (int i = 0; i < NUM_LEDS; i++) {
  186.       leds[i] = CHSV(0, 0, 0);
  187.     }
  188.     leds[led] = CHSV(0, 0, BRIGHTNESS);
  189.     FastLED.show();
  190.     delayForMillis(speed);
  191.   }
  192. }
  193.  
  194. void allRandom (int speed){
  195.   for(int i = 0; i < NUM_LEDS && oldAnimation == animation; i++){
  196.     button.tick();
  197.     leds[i] = CHSV(random(256), SATURATION, BRIGHTNESS);
  198.   }
  199.   FastLED.show();
  200.   delayForMillis(speed);
  201. }
  202.  
  203. void disolve (int simultaneous, int cycles, int speed) {
  204.   for (int i = 0; i < cycles && oldAnimation == animation; i++) {
  205.     button.tick();
  206.     for (int j = 0; j < simultaneous; j++){
  207.       int idx = random(NUM_LEDS);
  208.       leds[idx] = CHSV(0, 0, 0);
  209.     }
  210.     FastLED.show();
  211.     delayForMillis(speed);
  212.   }
  213.  
  214.   black();
  215. }
  216.  
  217. void colorWipe (CHSV c, int speed, int direction) {
  218.   for (int i = 0; i < NUM_LEDS && oldAnimation == animation; i++) {
  219.     button.tick();
  220.     if(direction == FORWARD) {
  221.       leds[i] = c;
  222.     } else {
  223.       leds[NUM_LEDS-1-i] = c;
  224.     }
  225.     FastLED.show();
  226.     delayForMillis(speed);
  227.   }
  228. }
  229.  
  230. void lightning (CHSV c, int simultaneous, int cycles, int speed) {
  231.   int flashes[simultaneous];
  232.  
  233.   for (int i = 0; i < cycles && oldAnimation == animation; i++) {
  234.     button.tick();
  235.     for (int j = 0; j < simultaneous; j++) {
  236.       int idx = random(NUM_LEDS);
  237.       flashes[j] = idx;
  238.       leds[idx] = c;
  239.     }
  240.     FastLED.show();
  241.     delayForMillis(speed);
  242.     for (int s = 0; s < simultaneous; s++){
  243.       leds[flashes[s]] = CHSV(0, 0, 0);
  244.     }
  245.     delayForMillis(speed);
  246.   }
  247. }
  248.  
  249. void lightning (int simultaneous, int cycles, int speed) {
  250.   int flashes[simultaneous];
  251.  
  252.   for (int i = 0; i < cycles && oldAnimation == animation; i++) {
  253.     button.tick();
  254.     for (int j = 0; j < simultaneous; j++) {
  255.       int idx = random(NUM_LEDS);
  256.       flashes[j] = idx;
  257.       leds[idx] = CHSV(random(256), SATURATION, BRIGHTNESS);
  258.     }
  259.     FastLED.show();
  260.     delayForMillis(speed);
  261.     for (int s = 0; s < simultaneous; s++) {
  262.       leds[flashes[s]] = CHSV(0, 0, 0);
  263.     }
  264.     delayForMillis(speed);
  265.   }
  266. }
  267.  
  268. void flash (CHSV c, int count, int speed) {
  269.   for (int i = 0; i < count && oldAnimation == animation; i++) {
  270.     button.tick();
  271.     hue(c);
  272.     delayForMillis(speed);
  273.     black();
  274.     delayForMillis(speed);
  275.   }
  276. }
  277.  
  278. void flash (int count, int speed) {
  279.   for (int i = 0; i < count && oldAnimation == animation; i++) {
  280.     button.tick();
  281.     hue(CHSV(random(256), SATURATION, BRIGHTNESS));
  282.     delayForMillis(speed);
  283.     black();
  284.     delayForMillis(speed);
  285.   }
  286. }
  287.  
  288. void cylon (CHSV c, int width, int speed) {
  289.   // First slide the leds in one direction
  290.   for (int i = 0; i <= NUM_LEDS-width && oldAnimation == animation; i++) {
  291.     button.tick();
  292.     for (int j = 0; j < width; j++) {
  293.       leds[i+j] = c;
  294.     }
  295.  
  296.     FastLED.show();
  297.  
  298.     for (int j = 0; j < 5; j++) {
  299.       leds[i+j] = CHSV(0, 0, 0);
  300.     }
  301.     delayForMillis(speed);
  302.   }
  303.  
  304.   for(int i = NUM_LEDS-width; i >= 0 && oldAnimation == animation; i--) {
  305.     button.tick();
  306.     for (int j = 0; j < width; j++) {
  307.       leds[i+j] = c;
  308.     }
  309.    
  310.     FastLED.show();
  311.    
  312.     for (int j = 0; j < width; j++) {
  313.       leds[i+j] = CHSV(0, 0, 0);
  314.     }
  315.     delayForMillis(speed);
  316.   }
  317. }
  318.  
  319. void theaterChase (CHSV c, int cycles, int speed) {
  320.   for (int j = 0; j < cycles && oldAnimation == animation; j++) {
  321.     for (int q = 0; q < 3; q++) {
  322.       button.tick();  
  323.       for (int i = 0; i < NUM_LEDS; i = i + 3) {
  324.         int pos = i + q;
  325.         leds[pos] = c;    //turn every third pixel on
  326.       }
  327.      
  328.       FastLED.show();
  329.  
  330.       delayForMillis(speed);
  331.  
  332.       for (int i = 0; i < NUM_LEDS; i = i + 3) {
  333.         button.tick();  
  334.         leds[i + q] = CHSV(0, 0, 0);        //turn every third pixel off
  335.       }
  336.     }
  337.   }
  338. }
  339.  
  340. void stripes(CHSV c1, CHSV c2, int width){
  341.   for(int i=0; i<NUM_LEDS && oldAnimation == animation; i++){
  342.     button.tick();
  343.     if(i % (width * 2) < width){
  344.       leds[i] = c1;
  345.     }
  346.     else{
  347.       leds[i] = c2;
  348.     }
  349.   }
  350.   FastLED.show();
  351. }
  352.  
  353. void theaterChaseRainbow (int cycles, int speed) {
  354.   for (int j = 0; j < 256 * cycles && oldAnimation == animation; j++) {
  355.     for (int q = 0; q < 3; q++) {
  356.       for (int i = 0; i < NUM_LEDS; i = i + 3) {
  357.         button.tick();  
  358.         int pos = i + q;
  359.         leds[pos] = CHSV((i + j) % 255, SATURATION, BRIGHTNESS);
  360.       }
  361.       FastLED.show();
  362.  
  363.       delayForMillis(speed);
  364.  
  365.       for (int i = 0; i < NUM_LEDS; i = i + 3) {
  366.         button.tick();  
  367.         leds[i + q] = CHSV(0, 0, 0);
  368.       }
  369.     }
  370.   }
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement