Advertisement
Fredbighead

Fade whole strip from one color to another

Nov 16th, 2022
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | Source Code | 0 0
  1. #include <FastLED.h>
  2. #define LED_PIN 6 //CONFIGURE LED STRIP HERE
  3. #define NUM_LEDS 164
  4. #define BRIGHTNESS 100
  5. #define LED_TYPE WS2812B
  6. #define COLOR_ORDER GRB
  7. CRGB leds[NUM_LEDS];
  8. #define UPDATES_PER_SECOND 100
  9.  
  10. //Previous value of RGB
  11. int redPrevious, greenPrevious, bluePrevious = 0;
  12.  
  13. //Current value of RGB
  14. float redCurrent, greenCurrent, blueCurrent = 0;
  15.  
  16. //Target value of RGB
  17. int redTarget, greenTarget, blueTarget = 0;
  18.  
  19. int fade_delay = 5;
  20. int steps = 50; //This is where you tell it how smooth of a transition you want it to be
  21.  
  22. void setup() {
  23. Serial.begin(9600);
  24. delay( 3000 ); // power-up safety delay
  25. FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  26. }
  27. void loop() { // Here is where you tell it what colours you want!
  28.  
  29. FadeToColour(0, 0, 225); // blue
  30. FadeToColour(0, 225, 0); // green
  31. FadeToColour(255, 100, 0); // redish-orange
  32. FadeToColour(0, 225, 0); // green
  33.  
  34. }
  35.  
  36. void FadeToColour(int r, int g, int b) {
  37. redPrevious = redCurrent;
  38. greenPrevious = greenCurrent;
  39. bluePrevious = blueCurrent;
  40.  
  41. redTarget = r;
  42. greenTarget = g;
  43. blueTarget = b;
  44.  
  45. float redDelta = redTarget - redPrevious;
  46. redDelta = redDelta / steps;
  47.  
  48. float greenDelta = greenTarget - greenPrevious;
  49. greenDelta = greenDelta / steps;
  50.  
  51. float blueDelta = blueTarget - bluePrevious;
  52. blueDelta = blueDelta / steps;
  53.  
  54. for (int j = 1; j < steps; j++) {
  55. redCurrent = redPrevious + (redDelta * j);
  56. greenCurrent = greenPrevious + (greenDelta * j);
  57. blueCurrent = bluePrevious + (blueDelta * j);
  58. fill_solid(redCurrent, greenCurrent, blueCurrent, leds);
  59. delay(fade_delay); //Delay between steps
  60. }
  61. delay(1000); //Wait at peak colour before continuing
  62. }
  63.  
  64. void fill_solid(int r, int g, int b, int lednum) {
  65. Serial.println(r);
  66.  
  67. for(int x = 0; x < NUM_LEDS; x++){ //This tells it to affect the whole strip instead of indivual ones.
  68. leds[x] = CRGB(r,g,b);
  69.  
  70. FastLED.show();
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement