Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
7,954
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN 5 //digital pin 5
  4. #define ANALOG_PIN 1 //for generating random numbers
  5. #define NUM_LEDS 579
  6. #define BRIGHTNESS 255
  7. #define LED_TYPE WS2812
  8. #define COLOR_ORDER GRB
  9. CRGB leds[NUM_LEDS];
  10. CRGB colorsArray[NUM_LEDS];
  11. #define FRAMES_PER_SECOND 50 //it takes ~20ms to push all the data to all 579 LEDs so the hard limit is ~50fps
  12.  
  13. const int size = NUM_LEDS / 3; //size of the rainbow, given 3 rows
  14. int cycleCount = 0; //counts cycles to reset at the correct time
  15.  
  16. void setup() {
  17. delay(50); //because life sucks
  18. FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  19. FastLED.setBrightness( BRIGHTNESS );
  20. //Serial.begin(9600);
  21.  
  22. //initial bias is arbitary and then normalized to the rainbow size. More bias will make the respective section of the rainbow larger
  23. int BRsize = 100;
  24. int RGsize = 100;
  25. int GBsize = 80;
  26. int biasSum = BRsize + RGsize + GBsize;
  27. BRsize = size * (float(BRsize) / float(biasSum));
  28. RGsize = size * (float(RGsize) / float(biasSum));
  29. GBsize = size * (float(GBsize) / float(biasSum));
  30. biasSum = BRsize + RGsize + GBsize;
  31. //makes the sum of the bias the same size as the rainbow
  32. if (biasSum < size) {
  33. BRsize ++;
  34. biasSum ++;
  35. if (biasSum < size) {
  36. RGsize ++;
  37. biasSum ++;
  38. if (biasSum < size) {
  39. GBsize ++;
  40. biasSum ++;
  41. }
  42. }
  43. }
  44. else if (biasSum > size) {
  45. BRsize --;
  46. biasSum --;
  47. if (biasSum > size) {
  48. RGsize --;
  49. biasSum --;
  50. if (biasSum > size) {
  51. GBsize --;
  52. biasSum --;
  53. }
  54. }
  55. }
  56.  
  57. for (int i = 0; i < BRsize; i++) {
  58. colorsArray[i] = CRGB( int(round(255 * float(i) / float(BRsize))) ,
  59. 0 ,
  60. int(round(255 - 255 * float(i) / (BRsize) )));
  61.  
  62.  
  63. // Serial.println(int(round(255 - float(255) * pow(float(i), float(2)) / (float(BRsize), float(2)) )));
  64. // Serial.println
  65. // Serial.println(int(round(255 - 255 * float(i) / float(BRsize))));
  66. }
  67. for (int i = 0; i < RGsize; i++) {
  68. colorsArray[i + BRsize] = CRGB(int(round(255 - 255 * float(i) / float(RGsize))) , int(round(255 * (float(i) / float(RGsize)))) , 0 );
  69.  
  70. }
  71. for (int i = 0; i < GBsize; i++) {
  72. colorsArray[i + BRsize + RGsize] = CRGB(0, int(round(255 - 255 * float(i) / float(GBsize))) , int(round(255 * (float(i) / float(GBsize)))));
  73.  
  74. }
  75.  
  76. //colorsArray[0] = CRGB(255,255,255);
  77.  
  78. //copy first row onto next two rows
  79. for (int i = 0; i < NUM_LEDS / 3; i++) {
  80. colorsArray[i + NUM_LEDS / 3] = colorsArray[i];
  81. colorsArray[i + 2 * NUM_LEDS / 3] = colorsArray[i];
  82. }
  83. }
  84.  
  85. void loop() {
  86. unsigned long start = millis(); //start timer
  87. FastLED.show();
  88. int speed = 40; //speed of the rainbow
  89. speed = speed * size / 300; //makes the speed not dependent on the size of the rainbow
  90.  
  91. int ledOffset = cycleCount * speed / FRAMES_PER_SECOND ; //calculates how far the generated table should be offset from the starting position for the given cycle count
  92.  
  93. //writes the led array based on the pre-generated table
  94. for (int i = 0; i < NUM_LEDS; i++) {
  95. leds[i] = colorsArray[(i + ledOffset) % NUM_LEDS];
  96. }
  97.  
  98. cycleCount ++;
  99. //resets cycle count after one full cycle
  100. if (cycleCount == int( size * FRAMES_PER_SECOND / speed)) {
  101. cycleCount = 0;
  102. }
  103. unsigned long delta = millis() - start; //end of timer
  104.  
  105. if ( 1000 / FRAMES_PER_SECOND > delta) { //adds pause to ensure that fps is at or below the defined fps
  106. FastLED.delay(1000 / FRAMES_PER_SECOND - delta);
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement