Advertisement
StePe

Untitled

Jun 8th, 2022
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.03 KB | None | 0 0
  1. // A simple caleidoscope by Stefan Petrick
  2.  
  3. #include <FastLED.h>
  4.  
  5. #define WIDTH 16
  6. #define HEIGHT 16
  7. #define NUM_LEDS ((WIDTH) * (HEIGHT))
  8.  
  9. CRGB leds[256];
  10.  
  11. float rnd;
  12.  
  13. void setup() {
  14.  
  15.   Serial.begin(115200);
  16.   FastLED.addLeds<APA102, 11, 13, GBR, DATA_RATE_MHZ(12)>(leds, 256);
  17.  
  18.   randomSeed(analogRead(10));
  19.   rnd = random(1000000);  // just a different starting point for each program start
  20.  
  21. }
  22.  
  23. void loop() {
  24.  
  25.   for (int y = 0; y < 16; y++) {
  26.     for (int x = 0; x < 16; x++) {
  27.      
  28.       uint32_t time = millis();
  29.  
  30.       time = time * 10.5; // adjust global speed here
  31.       time = time + rnd;
  32.  
  33.       uint16_t a = inoise16((x * 22000) + (time * 23), (y * 22000) + (time * 21), 1);
  34.      
  35.       uint16_t b = inoise16((a * 4) + (x * 15000) + (time * 25), (y * 15000) + (time * 25), 1);
  36.  
  37.       uint16_t c = inoise16((b * 4) + (x * 6000) + (time * 24), (a * 4) + (y * 6000) + (time * 27), 7);
  38.  
  39.  
  40.       a = map(a, 20000, 65535 , 0, 250);
  41.       if (a > 250) a = 0;
  42.  
  43.       b = map(b, 20000, 65535 , 0, 250);
  44.       if (b > 250) b = 0;
  45.  
  46.  
  47.       c = map(c, 20000, 65535 , 0, 250);
  48.       if (c > 250) c = 0;
  49.  
  50.       //CRGB col1 = CRGB(a,abs(b-a)/3,a/5);
  51.       //CRGB col2 = CRGB(abs(a-b)/2,0,b/4);
  52.  
  53.       //CRGB col1 = CRGB(abs(a ), a / 4, c);
  54.       //CRGB col2 = CRGB(abs(b - a), b / 4, 0);
  55.      
  56.       CRGB col3 = CRGB(a, b/2, c/2);
  57.  
  58.       leds[XY(x, y)] = col3;
  59.  
  60.       // filter mask based on 3d noise layer
  61.       //leds[XY(x, y)].fadeLightBy(a);
  62.  
  63.       // linear filter to fade out upwards
  64.       //leds[XY(x, y)].fadeLightBy((y * 12));
  65.  
  66.     }
  67.   }
  68.  
  69.   // make a caleidoscope by copying one quadrant into the 2nd
  70.   for (int y = 0; y < 9; y++) {
  71.     for (int x = 0; x < 9; x++) {
  72.       leds[XY(16 - x, y)] = leds[XY(x, y)]; //left to right
  73.     }
  74.   }
  75.  
  76.   // copy upper half into lower half
  77.   for (int y = 0; y < 9; y++) {
  78.     for (int x = 0; x < 16; x++) {
  79.       leds[XY(x, 16 - y)] = leds[XY(x, y)];
  80.     }
  81.   }
  82.  
  83.   /*
  84.     // linear filter
  85.     for (int y = 0; y < 16; y++) {
  86.       for (int x = 0; x < 16; x++) {
  87.         leds[XY(x, y)].fadeLightBy(230 - (y * 12)); // geil!!
  88.       }
  89.     }
  90.   */
  91.  
  92.   // send current brightness to serial plotter
  93.   uint32_t bright = 0;
  94.   for (uint16_t i = 0; i < NUM_LEDS; i++) {
  95.     bright +=   leds[i].r;
  96.     bright +=   leds[i].g;
  97.     bright +=   leds[i].b;
  98.   }
  99.   EVERY_N_MILLIS(200) Serial.println(bright);
  100.  
  101.  
  102.  
  103.   adjust_gamma();
  104.  
  105.   FastLED.show();
  106.  
  107.   //EVERY_N_MILLIS(100) Serial.println(FastLED.getFPS());
  108. }
  109.  
  110. uint16_t XY(uint8_t x, uint8_t y) {
  111.   if (x >= WIDTH) return NUM_LEDS;
  112.   if (y >= HEIGHT) return NUM_LEDS;
  113.   if (y & 1)
  114.     return (y + 1) * WIDTH - 1 - x;
  115.   else
  116.     return y * WIDTH + x;
  117. }
  118.  
  119. void adjust_gamma()
  120. {
  121.   uint8_t min = 0;
  122.   for (uint16_t i = 0; i < NUM_LEDS; i++)
  123.   {
  124.     leds[i].r = dim8_video(leds[i].r);
  125.     leds[i].g = dim8_video(leds[i].g);
  126.     leds[i].b = dim8_video(leds[i].b);
  127.  
  128.     if (leds[i].r < min) leds[i].r = min;
  129.     if (leds[i].g < min) leds[i].g = min;
  130.     if (leds[i].b < min) leds[i].b = min;
  131.   }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement