shdowmyst

ripple effect by suhajdab

Jul 22nd, 2016
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #define PIN 2
  3. #define Pixels 24
  4. #define BG 1
  5. Adafruit_NeoPixel strip = Adafruit_NeoPixel(Pixels, PIN, NEO_GRB + NEO_KHZ800);
  6.  
  7.  
  8. int color;
  9. int center = 0;
  10. int step = -1;
  11. int maxSteps = 16;
  12. float fadeRate = 0.6;
  13. int diff;
  14.  
  15. //background color
  16. uint32_t currentBg = random(256);
  17. uint32_t nextBg = currentBg;
  18.  
  19. void setup() {
  20.   strip.begin();
  21.   strip.show(); // Initialize all pixels to 'off'
  22. }
  23.  
  24. void loop () {
  25.   ripple();
  26. }
  27.  
  28. void ripple() {
  29.   if (BG){
  30.     if (currentBg == nextBg) {
  31.       nextBg = random(256);
  32.     }
  33.     else if (nextBg > currentBg) {
  34.       currentBg++;
  35.     } else {
  36.       currentBg--;
  37.     }
  38.     for(uint16_t l = 0; l < Pixels; l++) {
  39.       strip.setPixelColor(l, Wheel(currentBg, 0.1));
  40.     }
  41.   } else {
  42.     for(uint16_t l = 0; l < Pixels; l++) {
  43.       strip.setPixelColor(l, 0, 0, 0);
  44.     }
  45.   }
  46.  
  47.  
  48.   if (step == -1) {
  49.     center = random(Pixels);
  50.     color = random(256);
  51.     step = 0;
  52.   }
  53.  
  54.  
  55.  
  56.   if (step == 0) {
  57.     strip.setPixelColor(center, Wheel(color, 1));
  58.     step ++;
  59.   }
  60.   else {
  61.     if (step < maxSteps) {
  62.       strip.setPixelColor(wrap(center + step), Wheel(color, pow(fadeRate, step)));
  63.       strip.setPixelColor(wrap(center - step), Wheel(color, pow(fadeRate, step)));
  64.       if (step > 3) {
  65.         strip.setPixelColor(wrap(center + step - 3), Wheel(color, pow(fadeRate, step - 2)));
  66.         strip.setPixelColor(wrap(center - step + 3), Wheel(color, pow(fadeRate, step - 2)));
  67.       }
  68.       step ++;
  69.     }
  70.     else {
  71.       step = -1;
  72.     }
  73.   }
  74.  
  75.   strip.show();
  76.   delay(50);
  77. }
  78.  
  79.  
  80. int wrap(int step) {
  81.   if(step < 0) return Pixels + step;
  82.   if(step > Pixels - 1) return step - Pixels;
  83.   return step;
  84. }
  85.  
  86.  
  87.  
  88. // Input a value 0 to 255 to get a color value.
  89. // The colours are a transition r - g - b - back to r.
  90. uint32_t Wheel(byte WheelPos, float opacity) {
  91.  
  92.   if(WheelPos < 85) {
  93.     return strip.Color((WheelPos * 3) * opacity, (255 - WheelPos * 3) * opacity, 0);
  94.   }
  95.   else if(WheelPos < 170) {
  96.     WheelPos -= 85;
  97.     return strip.Color((255 - WheelPos * 3) * opacity, 0, (WheelPos * 3) * opacity);
  98.   }
  99.   else {
  100.     WheelPos -= 170;
  101.     return strip.Color(0, (WheelPos * 3) * opacity, (255 - WheelPos * 3) * opacity);
  102.   }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment