Advertisement
willhartley

FastLED ColorFromPalette For-Loop Replace with Millis()

May 16th, 2022 (edited)
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //please declare variables
  2.  
  3. static uint8_t startIndex = 0;
  4.  
  5. void loop()
  6. {
  7.     //ChangePalettePeriodically();
  8.    
  9.     //static uint8_t startIndex = 0;
  10.     //startIndex = startIndex + 1; /* motion speed */
  11.    
  12.     //FillLEDsFromPaletteColors( startIndex);
  13.     FillLEDsFromPaletteColors2( startIndex);
  14.    
  15.     //FastLED.show();
  16.     //FastLED.delay(1000 / UPDATES_PER_SECOND);
  17. }
  18.  
  19.  
  20.  
  21. // Original
  22.  
  23. void FillLEDsFromPaletteColors( uint8_t colorIndex)
  24. {
  25.     uint8_t brightness = 255;
  26.    
  27.     for( int i = 0; i < NUM_LEDS; ++i) {
  28.         leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  29.         colorIndex += 3;
  30.     }
  31. }
  32.  
  33. // For-loop switched for millis()
  34. void FillLEDsFromPaletteColors2(){
  35.  
  36.   currentPalette = RainbowStripeColors_p;
  37.  
  38.   static uint8_t temp_index = startIndex; //initialize once for colorfrompalette
  39.  
  40.   if(isLoaded == false){  //leds arent loaded. do nothing but load data
  41.     if(x < NUM_LEDS){   //original for-loop condition for(int i =0; x<NUM_LEDS;i++)
  42.       leds[x] = ColorFromPalette( currentPalette, temp_index, 40, currentBlending );
  43.       temp_index += 3;
  44.       x++;  //original for-loop incrementor
  45.     }else if(x == NUM_LEDS){  //leds are full,
  46.       x = 0;
  47.       isLoaded = true;    //leds are finally loaded, reset x and wait for timer
  48.     }
  49.   }
  50.  
  51.   if(millis() - previousMillis >= (20) && isLoaded == true){  //our fastled delay(20)
  52.     previousMillis = millis();
  53.     isLoaded = false;          //leds will no longer be loaded at FastLED show is called
  54.     startIndex += 1;
  55.     temp_index = startIndex;    //reset temp_index to start index to move led to next
  56.     FastLED.show();           //no longer loaded
  57.   }    
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement