#include <Adafruit_NeoPixel.h>

#define COLORSTEPS 24
#define PIN D2
#define NUMPIXELS 12

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int ledstate[NUMPIXELS];

void setup() {
  Serial.begin(115200);
  delay(150);
  Serial.println("PIXELS");
  
  pixels.begin(); // initialize NeoPixel library.

  pixels.setBrightness(40);
  for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
    pixels.rainbow(firstPixelHue);
    pixels.show();
    delay(10);
  }
    
  //rainbow();
}

void rainbow() {
  pixels.setBrightness(10);

  for (int i = 0; i < NUMPIXELS; i++) {
    ledstate[i] = 0;
  }

  int increment = 65536 / COLORSTEPS;

  while (ledstate[NUMPIXELS-1] < 65536) {

    // find first inactive pixel, if any
    int ix = NUMPIXELS-1;
    while (ix > 0  and ledstate[ix-1] == 0) {
      ix--;
    }

    for (int iy = ix; iy >= 0; iy--) {
      ledstate[iy] = ledstate[iy] + increment;
    }

    for (int i = 0; i < NUMPIXELS; i++) {
      if (ledstate[i] < 65536 and i <= ix) {
        pixels.setPixelColor(i, pixels.gamma32(pixels.ColorHSV(ledstate[i])));
      }
      else {
        pixels.setPixelColor(i, 0, 0, 0);
      }
    }

    pixels.show();

    delay(50);
  }

  delay(1000);

  pixels.setPixelColor(1, pixels.Color(1, 1, 1));
  pixels.setPixelColor(2, pixels.Color(31, 31, 31));
  pixels.setPixelColor(3, pixels.Color(63, 63, 63));
  pixels.setPixelColor(4, pixels.Color(95, 95, 95));
  pixels.setPixelColor(5, pixels.Color(127, 127, 127));
  pixels.setPixelColor(6, pixels.Color(159, 159, 159));
  pixels.setPixelColor(7, pixels.Color(191, 191, 191));
  pixels.setPixelColor(8, pixels.Color(223, 223, 223));
  pixels.setPixelColor(9, pixels.Color(255, 255, 255));
  pixels.show();

  delay(3000);

  for (int i = 30; i < 256; i++) {
    for (int j = 0; j < NUMPIXELS; j++) {
      pixels.setPixelColor(j, pixels.Color(i, i, i));
    }
    pixels.show();
    delay(5);
  }

  delay(3000);
  pixels.fill(pixels.Color(217, 101, 0), 0, NUMPIXELS);
//  for (int i = 0; i < NUMPIXELS; i++) {
//    pixels.setPixelColor(i, pixels.Color(255, 234, 0));
//  }
  pixels.show();

  delay(2000);
  pixels.setBrightness(20);
  pixels.show();

  delay(2000);
  pixels.setBrightness(10);
  pixels.show();

  delay(2000);
  pixels.setBrightness(5);
  pixels.show();

  delay(2000);
  pixels.setBrightness(1);
  pixels.show();  
}

void loop() {
//  pixels.setBrightness(20);
//  
//  pixels.fill(pixels.Color(255,0,0), 0, 0);
//  pixels.show();
//  delay(3000);
//  
//  pixels.fill(pixels.Color(0,255,0), 0, 0);
//  pixels.show();
//  delay(3000);
//  
//  pixels.fill(pixels.Color(0,0,255), 0, 0);
//  pixels.show();
//  delay(3000);
//  
//  pixels.fill(pixels.Color(255,255,0), 0, 0);
//  pixels.show();
//  delay(3000);
//  
//  pixels.fill(pixels.Color(0,255,255), 0, 0);
//  pixels.show();
//  delay(3000);
//  
//  pixels.fill(pixels.Color(255,0,255), 0, 0);
//  pixels.show();
//  delay(3000);
//  
//  pixels.fill(pixels.Color(255,255,255), 0, 0);
//  pixels.show();
//  delay(3000);
}
