/ Cyberpunk_Spikes / Cyberpunk_Spikes.ino
Cyberpunk_Spikes.ino
  1  // SPDX-FileCopyrightText: 2017 Mikey Sklar for Adafruit Industries
  2  //
  3  // SPDX-License-Identifier: MIT
  4  
  5  #include <Adafruit_NeoPixel.h>
  6  
  7  #define PIN 1
  8  
  9  // Parameter 1 = number of pixels in strip
 10  // Parameter 2 = pin number (most are valid)
 11  // Parameter 3 = pixel type flags, add together as needed:
 12  //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
 13  //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
 14  //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
 15  //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
 16  Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);
 17  
 18  // Here is where you can put in your favorite colors that will appear!
 19  // just add new {nnn, nnn, nnn}, lines. They will be picked out randomly
 20  //                          R   G   B
 21  uint8_t myColors[][3] = {{232, 100, 255},   // purple
 22                           {200, 200, 20},   // yellow 
 23                           {30, 200, 200},   // blue
 24                            };
 25                                 
 26  // don't edit the line below
 27  #define FAVCOLORS sizeof(myColors) / 3
 28  
 29  void setup() {
 30    strip.begin();
 31    strip.setBrightness(40);
 32    strip.show(); // Initialize all pixels to 'off'
 33  }
 34  
 35  void loop() {
 36  flashRandom(5, 8);  // first number is 'wait' delay, shorter num == shorter twinkle
 37  flashRandom(5, 5);  // second number is how many neopixels to simultaneously light up
 38  flashRandom(5, 11);
 39  colorWipe(strip.Color(232, 100, 255), 50); // Red
 40  colorWipe(strip.Color(200, 200, 20), 50); // Green
 41  colorWipe(strip.Color(30, 200, 200), 50); // Blue
 42  rainbowCycle(20);
 43  }
 44  
 45  // Fill the dots one after the other with a color
 46  void colorWipe(uint32_t c, uint8_t wait) {
 47    for(uint16_t i=0; i<strip.numPixels(); i++) {
 48        strip.setPixelColor(i, c);
 49        strip.show();
 50        delay(wait);
 51    }
 52  }
 53  
 54  void rainbow(uint8_t wait) {
 55    uint16_t i, j;
 56  
 57    for(j=0; j<256; j++) {
 58      for(i=0; i<strip.numPixels(); i++) {
 59        strip.setPixelColor(i, Wheel((i+j) & 255));
 60      }
 61      strip.show();
 62      delay(wait);
 63    }
 64  }
 65  
 66  // Slightly different, this makes the rainbow equally distributed throughout
 67  void rainbowCycle(uint8_t wait) {
 68    uint16_t i, j;
 69  
 70    for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
 71      for(i=0; i< strip.numPixels(); i++) {
 72        strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
 73      }
 74      strip.show();
 75      delay(wait);
 76    }
 77  }
 78  
 79  // Input a value 0 to 255 to get a color value.
 80  // The colours are a transition r - g - b - back to r.
 81  uint32_t Wheel(byte WheelPos) {
 82    if(WheelPos < 85) {
 83     return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
 84    } else if(WheelPos < 170) {
 85     WheelPos -= 85;
 86     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
 87    } else {
 88     WheelPos -= 170;
 89     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
 90    }
 91  }
 92  
 93  void flashRandom(int wait, uint8_t howmany) {
 94   
 95    for(uint16_t i=0; i<howmany; i++) {
 96      // pick a random favorite color!
 97      int c = random(FAVCOLORS);
 98      int red = myColors[c][0];
 99      int green = myColors[c][1];
100      int blue = myColors[c][2]; 
101   
102      // get a random pixel from the list
103      int j = random(strip.numPixels());
104      
105      // now we will 'fade' it in 5 steps
106      for (int x=0; x < 5; x++) {
107        int r = red * (x+1); r /= 5;
108        int g = green * (x+1); g /= 5;
109        int b = blue * (x+1); b /= 5;
110        
111        strip.setPixelColor(j, strip.Color(r, g, b));
112        strip.show();
113        delay(wait);
114      }
115      // & fade out in 5 steps
116      for (int x=5; x >= 0; x--) {
117        int r = red * x; r /= 5;
118        int g = green * x; g /= 5;
119        int b = blue * x; b /= 5;
120        
121        strip.setPixelColor(j, strip.Color(r, g, b));
122        strip.show();
123        delay(wait);
124      }
125    }
126    // LEDs will be off when done (they are faded to 0)
127  }