Advertisement
TolentinoCotesta

FastLed + Encoder LAMP

Mar 24th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.20 KB | None | 0 0
  1. #include <FastLED.h>
  2. #include <Encoder.h>
  3. #include <EEPROM.h>
  4.  
  5. #define LONG  1000   // Tempo "pressione tasto lunga" (Cambio modalità)
  6. #define MEDIUM  500    // Tempo "pressione tasto media" (Non usato)
  7. #define SHORT 100    // Tempo "pressione tasto breve" (Cambio animazione)
  8.  
  9. // CIE1931 correction table
  10. // Necessaria per linearizzare la variazione di luminosità
  11. unsigned int cie_table[33] = {
  12.   255, 254, 253, 252, 251, 250, 248, 246, 244, 241,
  13.   238, 234, 230, 225, 220, 214, 208, 201, 193, 185,
  14.   176, 166, 156, 144, 132, 119, 105, 90, 74, 57,
  15.   39, 20, 0
  16. };
  17.  
  18. // *************************  FastLed   *************************************//
  19. ///////////////////////////////////////////////////////////////////////////////
  20. FASTLED_USING_NAMESPACE
  21. #define DATA_PIN    5
  22. #define LED_TYPE    WS2812B
  23. #define COLOR_ORDER GRB
  24. #define NUM_LEDS    10
  25. CRGB leds[NUM_LEDS];
  26. #define FRAMES_PER_SECOND  120
  27. int brightness = cie_table[16];
  28. bool animation = true;
  29. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  30.  
  31. // ***********************    Encoder   *************************************//
  32. ///////////////////////////////////////////////////////////////////////////////
  33. #define ENC_SW   2
  34. #define ENC_DT   3
  35. #define ENC_CLK  4
  36. Encoder myEnc(ENC_DT, ENC_CLK);
  37. long Position = -1;
  38.  
  39. uint32_t pressTime;
  40.  
  41. // ****************************  Setup  *************************************//
  42. ///////////////////////////////////////////////////////////////////////////////
  43.  
  44. void setup() {  
  45.   pinMode(ENC_SW, INPUT);
  46.   Serial.begin(115200);
  47.   Serial.println("START");
  48.   EEPROM.get(0, brightness);
  49.   EEPROM.get(10, gCurrentPatternNumber);
  50.  
  51.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);  
  52.   Serial.print("Luminosità ("); Serial.print(brightness); Serial.print("): ");
  53.   Serial.println(255 - cie_table[brightness]);
  54.   FastLED.setBrightness(255 - cie_table[brightness]);
  55. }
  56.  
  57. // ***********************  Animations  *************************************//
  58. ///////////////////////////////////////////////////////////////////////////////
  59.  
  60. // List of patterns to cycle through.  Each is defined as a separate function below.
  61. typedef void (*SimplePatternList[])();                                          
  62. SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, inoise8_fire, besin, dot_beat,
  63.                                 sinelon, bpm, confetti, juggle, onesine, plasma, fill_grad };                  
  64. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  65. uint8_t timeval = 20;  
  66.  
  67. // ************************  Main loop  *************************************//
  68. ///////////////////////////////////////////////////////////////////////////////
  69. void loop(){
  70.   // Call the current pattern function once, updating the 'leds' array
  71.   gPatterns[gCurrentPatternNumber]();
  72.  
  73.   // send the 'leds' array out to the actual LED strip
  74.   FastLED.show();  
  75.   // insert a delay to keep the framerate modest
  76.   FastLED.delay(1000/FRAMES_PER_SECOND);
  77.  
  78.   // do periodic updates
  79.   EVERY_N_MILLISECONDS( 20 ) {
  80.     gHue++;
  81.   } // slowly cycle the "base color" through the rainbow
  82.  
  83.   EVERY_N_MILLIS_I(thisTimer, timeval) {
  84.       thisTimer.setPeriod(timeval);                 // We can change the timeval on the fly with this line, which is pretty sweet.
  85.       gPatterns[gCurrentPatternNumber]();           // Call the current pattern function.
  86.     }  
  87.  
  88.   pressTime = millis();
  89.   readButton(ENC_SW);
  90.  
  91.   // Update brightness
  92.   Position = myEnc.read();
  93.   if (Position != 0) {
  94.     if(Position > 0)
  95.       brightness++;
  96.     else
  97.       brightness--;  
  98.     brightness = constrain(brightness, 0, 32);
  99.     Serial.print("Luminosità ("); Serial.print(brightness); Serial.print("): ");
  100.     Serial.println(255 - cie_table[brightness]);
  101.     FastLED.setBrightness(255 - cie_table[brightness]);
  102.     EEPROM.put(0, brightness);
  103.     // Reset encoder position
  104.     myEnc.write(0);
  105.     // Rallentiamo un po' la variazione
  106.     delay(50);
  107.   }
  108. }
  109.  
  110.  
  111.  
  112.  
  113. void readButton(const uint8_t btn){
  114.   if(digitalRead(btn) == LOW){
  115.     delay(SHORT);
  116.     static uint8_t pressType = 0;
  117.     uint32_t pTime;
  118.     while(digitalRead(btn) == LOW){    
  119.       pTime = millis() - pressTime;
  120.       if(pTime > SHORT  && pTime <= MEDIUM)
  121.         pressType = 1;
  122.       else if (pTime > MEDIUM && pTime <= LONG)
  123.         pressType = 2;  
  124.       else if (pTime > LONG){
  125.         pressType = 3;
  126.         break;
  127.       }
  128.     }
  129.     switch(pressType){
  130.       case 0: break;
  131.       case 1: shortClick();  break;
  132.       case 2: break;
  133.       case 3: longClick(); break;
  134.     }
  135.   }  
  136. }
  137.  
  138. void shortClick(){
  139.     fadeToBlackBy(leds, NUM_LEDS, 2);
  140.     nextPattern(); // change patterns
  141.     Serial.println("Cambio animazione");
  142.     EEPROM.put(10, gCurrentPatternNumber);
  143. }
  144.  
  145. void longClick(){
  146.     fadeToBlackBy(leds, NUM_LEDS, 2);
  147.     Serial.println("Cambio colore");
  148.     animation = !animation;
  149. }
  150.  
  151.  
  152. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  153. void nextPattern(){
  154.   // add one to the current pattern number, and wrap around at the end
  155.   gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  156. }
  157.  
  158.  
  159. CRGBPalette16 currentPalette(PartyColors_p);
  160. TBlendType    currentBlending = LINEARBLEND;                                    // NOBLEND or LINEARBLEND
  161. #define qsubd(x, b)  ((x>b)?b:0)                              // Digital unsigned subtraction macro. if result <0, then => 0. Otherwise, take on fixed value.
  162. #define qsuba(x, b)  ((x>b)?x-b:0)  
  163.  
  164.  
  165. // Let's add some glitter, thanks to Mark
  166. void addGlitter( fract8 chanceOfGlitter) {                                        
  167.   if( random8() < chanceOfGlitter)
  168.     leds[random16(NUM_LEDS)] += CRGB::White;
  169. } // addGlitter()
  170.  
  171.  
  172.  
  173. // LED Animations
  174.  
  175. void confetti(){
  176.   // random colored speckles that blink in and fade smoothly
  177.   fadeToBlackBy( leds, NUM_LEDS, 10);
  178.   int pos = random16(NUM_LEDS);
  179.   leds[pos] += CHSV( gHue + random8(64), 200, 255);
  180. }
  181.  
  182.  
  183. void rainbow() {  
  184.   fill_rainbow( leds, NUM_LEDS, gHue, 7);
  185. }
  186.  
  187. void rainbowWithGlitter() {  
  188.   fill_rainbow( leds, NUM_LEDS, gHue, 7);
  189.   addGlitter(120);
  190. }
  191.  
  192.  
  193. void bpm(){
  194.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  195.   uint8_t BeatsPerMinute = 62;  
  196.   uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  197.   for( int i = 0; i < NUM_LEDS; i++) { //9948
  198.     leds[i] = ColorFromPalette(currentPalette, gHue+(i*2), beat-gHue+(i*10));
  199.   }
  200. }
  201.  
  202. void sinelon(){
  203.   // a colored dot sweeping back and forth, with fading trails
  204.   fadeToBlackBy( leds, NUM_LEDS, 100);
  205.   int pos = beatsin16( 8, 0, NUM_LEDS-1 );
  206.   leds[pos] += CHSV( gHue, 255, 192);
  207. }
  208.  
  209.  
  210. void dot_beat() {
  211.   uint8_t   count =   0;                                        // Count up to 255 and then reverts to 0
  212.   uint8_t fadeval = 224;                                        // Trail behind the LED's. Lower => faster fade.  
  213.   uint8_t bpm = 30;
  214.   uint8_t inner = beatsin8(bpm, NUM_LEDS/4, NUM_LEDS/4*3);    // Move 1/4 to 3/4
  215.   uint8_t outer = beatsin8(bpm, 0, NUM_LEDS-1);               // Move entire length
  216.   uint8_t middle = beatsin8(bpm, NUM_LEDS/3, NUM_LEDS/3*2);   // Move 1/3 to 2/3
  217.  
  218.   leds[middle] = CRGB::Purple;
  219.   leds[inner] = CRGB::Blue;
  220.   leds[outer] = CRGB::Aqua;
  221.   nscale8(leds,NUM_LEDS,fadeval);                             // Fade the entire array. Or for just a few LED's, use  nscale8(&leds[2], 5, fadeval);
  222. } // dot_beat()
  223.  
  224.  
  225. void fill_grad() {  
  226.   uint8_t starthue = beatsin8(5, 0, 255);
  227.   uint8_t endhue = beatsin8(7, 0, 255);  
  228.   if (starthue < endhue)
  229.     fill_gradient(leds, NUM_LEDS, CHSV(starthue,255,255), CHSV(endhue,255,255), FORWARD_HUES);
  230.    else
  231.     fill_gradient(leds, NUM_LEDS, CHSV(starthue,255,255), CHSV(endhue,255,255), BACKWARD_HUES);  
  232. } // fill_grad()
  233.  
  234.  
  235. void inoise8_fire() {  
  236.   uint32_t xscale = 20;                                          // How far apart they are
  237.   uint32_t yscale = 3;                                           // How fast they move
  238.   uint8_t index = 0;
  239.   for(int i = 0; i < NUM_LEDS; i++) {
  240.     index = inoise8(i*xscale,millis()*yscale*NUM_LEDS/255);                                           // X location is constant, but we move along the Y at the rate of millis()
  241.     leds[i] = ColorFromPalette(currentPalette, min(i*(index)>>6, 255), i*255/NUM_LEDS, LINEARBLEND);  // With that value, look up the 8 bit colour palette value and assign it to the current LED.
  242.   }                                                                                                   // The higher the value of i => the higher up the palette index (see palette definition).
  243.                                                                                                       // Also, the higher the value of i => the brighter the LED.
  244. } // inoise8_fire()
  245.  
  246.  
  247. void juggle() {
  248.   // eight colored dots, weaving in and out of sync with each other
  249.   fadeToBlackBy( leds, NUM_LEDS, 20);
  250.   byte dothue = 0;
  251.   for( int i = 0; i < 8; i++) {
  252.     leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  253.     dothue += 32;
  254.   }
  255. }
  256.  
  257.  
  258. void onesine() {
  259. // Persistent local variable.
  260.   static int thisphase = 0;                                                     // Phase change value gets calculated.
  261. // Local variables. Play around with these.
  262.   uint8_t allfreq = 32;                                                         // You can change the frequency, thus distance between bars. Wouldn't recommend changing on the fly.
  263.   uint8_t thiscutoff = 192;                                                     // You can change the cutoff value to display this wave. Lower value = longer wave.
  264.   uint8_t bgbright = 10;                                                        // Brightness of background colour.
  265.   uint8_t colorIndex;
  266.   timeval = 30;                                                                 // Our EVERY_N_MILLIS_I timer value.
  267.  
  268.   thiscutoff = beatsin8(12,64, 224);
  269.   thisphase = beatsin16(20,-600, 600);
  270.   colorIndex = millis() >> 4;                                                   // millis() can be used for so many things.  
  271.   for (int k=0; k<NUM_LEDS; k++) {                                            // For each of the LED's in the strand, set a brightness based on a wave as follows:
  272.     int thisbright = qsuba(cubicwave8((k*allfreq)+thisphase), thiscutoff);      // qsub sets a minimum value called thiscutoff. If < thiscutoff, then bright = 0. Otherwise, bright = 128 (as defined in qsub)..
  273.     leds[k] = ColorFromPalette( currentPalette, colorIndex, thisbright, currentBlending);    // Let's now add the foreground colour. By Andrew Tuline.
  274.     colorIndex +=3;
  275.   }
  276.  
  277. } // onesine()
  278.  
  279.  
  280. void plasma() {
  281. // Persistent local variables
  282.   static int16_t thisphase = 0;                                                 // Phase of a cubicwave8.
  283.   static int16_t thatphase = 0;                                                 // Phase of the cos8.
  284.  
  285. // Temporary local variables
  286.   uint16_t thisbright;
  287.   uint16_t colorIndex;
  288.   timeval = 20;                                                                 // Our EVERY_N_MILLIS_I timer value.  
  289.   thisphase += beatsin8(6,-4,4);                                                // You can change direction and speed individually.
  290.   thatphase += beatsin8(7,-4,4);                                                // Two phase values to make a complex pattern. By Andrew Tuline.
  291.  
  292.   for (int k=0; k<NUM_LEDS; k++) {                                              // For each of the LED's in the strand, set a brightness based on a wave as follows.
  293.     thisbright = cubicwave8((k*8)+thisphase)/2;    
  294.     thisbright += cos8((k*10)+thatphase)/2;                                     // Let's munge the brightness a bit and animate it all with the phases.
  295.     colorIndex=thisbright;
  296.     thisbright = qsuba(thisbright, random(255));                              // qsuba chops off values below a threshold defined by sampleavg. Gives a cool effect.    
  297.     leds[k] = ColorFromPalette( currentPalette, colorIndex, thisbright, currentBlending);   // Let's now add the foreground colour.
  298.   }
  299.   addGlitter(random(50));                                                      // Add glitter based on sampleavg.
  300. } // plasma()
  301.  
  302.  
  303. void besin() {                                                             // Add a Perlin noise soundbar. This looks really cool.
  304.   timeval = 30;                                                            // Our EVERY_N_MILLIS_I timer value.
  305. // This works.
  306.   leds[NUM_LEDS/2] = ColorFromPalette(currentPalette, millis(), random(128), NOBLEND);
  307.   leds[NUM_LEDS/2-1] = ColorFromPalette(currentPalette, millis(), random(128), NOBLEND);
  308.   for (int i = NUM_LEDS - 1; i > NUM_LEDS/2; i--)                              // Move to the right.
  309.     leds[i] = leds[i - 1];
  310.   for (int i = 0; i < NUM_LEDS/2; i++)                                         // Move to the left.
  311.     leds[i] = leds[i + 1];                                                                     // Move the pixels to the left/right, but not too fast.
  312.   fadeToBlackBy(leds+NUM_LEDS/2-1, 2, 128);                                   // Fade the center, while waveit moves everything out to the edges.
  313.   //fadeToBlackBy(leds, NUM_LEDS, 2);                                                                                
  314. } // besin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement