// A basic everyday NeoPixel strip test program.

// NEOPIXEL BEST PRACTICES for most reliable operation:
// - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
// - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
// - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
// - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
//   connect GROUND (-) first, then +, then data.
// - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
//   a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
// (Skipping these may work OK on your workbench but can fail in the field)

#include <Adafruit_NeoPixel.h>

// Which pin on the Arduino is connected to the NeoPixels?
#define LED_PIN     13

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 60

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

#define BUTTON_PIN  A0
#define POT_PIN  A1

// setup() function -- runs once at startup --------------------------------

  unsigned int effect_number = 0;

void setup() {

  pinMode(BUTTON_PIN, INPUT_PULLUP);

  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)

 // Serial.begin(9600);

}


// loop() function -- runs repeatedly as long as board is on ---------------

void loop() {


   
  if(digitalRead(BUTTON_PIN)==LOW) effect_number++;
 // Serial.println(effect_number);

  switch(effect_number) {
  
   // Fill along the length of the strip in various colors...
  case 1:
  colorWipe(strip.Color(255,   255,   255), 50); // White 
  break; 
  case 2:
  colorWipe(strip.Color(255,   0,   0), 50); // Red
  break;
  case 3:
  colorWipe(strip.Color(  0, 255,   0), 50); // Green
  break;
  case 4:
  colorWipe(strip.Color(  0,   0, 255), 50); // Blue
  break;

  // Do a theater marquee effect in various colors...
  case 5:
  theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness
  break;
  case 6:
  theaterChase(strip.Color(127,   0,   0), 50); // Red, half brightness
  break;
  case 7:
  theaterChase(strip.Color(  0,   0, 127), 50); // Blue, half brightness
  break;

  
    // Flowing rainbow cycle along the whole strip
  case 8:
  rainbow(10);
  break;

 /* 
  case 9:
   // Rainbow-enhanced theaterChase variant
   theaterChaseRainbow(50); 
  break;
  */
  
  default: 
  effect_number = 0;
  colorWipe(strip.Color(  0,   0,  0), 50);   // Off
  }

}


// Some functions of our own for creating animated effects -----------------

// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {
  for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
}

// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
// between frames.
void theaterChase(uint32_t color, int wait) {
  for(int a=0; a<10; a++) {  // Repeat 10 times...
    for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...
      strip.clear();         //   Set all pixels in RAM to 0 (off)
      // 'c' counts up from 'b' to end of strip in steps of 3...
      for(int c=b; c<strip.numPixels(); c += 3) {
        strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
      }
      strip.show(); // Update strip with new contents
      delay(wait);  // Pause for a moment
    }
  }
}

// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(int wait) {
  // Hue of first pixel runs 5 complete loops through the color wheel.
  // Color wheel has a range of 65536 but it's OK if we roll over, so
  // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
  // means we'll make 5*65536/256 = 1280 passes through this loop:
  for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
    // strip.rainbow() can take a single argument (first pixel hue) or
    // optionally a few extras: number of rainbow repetitions (default 1),
    // saturation and value (brightness) (both 0-255, similar to the
    // ColorHSV() function, default 255), and a true/false flag for whether
    // to apply gamma correction to provide 'truer' colors (default true).
    strip.rainbow(firstPixelHue);
    // Above line is equivalent to:
    // strip.rainbow(firstPixelHue, 1, 255, 255, true);
    strip.show(); // Update strip with new contents
    delay(wait);  // Pause for a moment
  }
}

// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
void theaterChaseRainbow(int wait) {
  int firstPixelHue = 0;     // First pixel starts at red (hue 0)
  for(int a=0; a<30; a++) {  // Repeat 30 times...
    for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...
      strip.clear();         //   Set all pixels in RAM to 0 (off)
      // 'c' counts up from 'b' to end of strip in increments of 3...
      for(int c=b; c<strip.numPixels(); c += 3) {
        // hue of pixel 'c' is offset by an amount to make one full
        // revolution of the color wheel (range 65536) along the length
        // of the strip (strip.numPixels() steps):
        int      hue   = firstPixelHue + c * 65536L / strip.numPixels();
        uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
        strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
      }
      strip.show();                // Update strip with new contents
      delay(wait);                 // Pause for a moment
      firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
    }
  }
}


/*            Chip ATMega328
       Arduino Uno R3 stackable header
                    _______
    Digital:  _____/       \__  Analog:
      txd ->-|D00 >RXD      A5|-
      rxd -<-|D01 <TXD      A4|-
            -|D02        A3 17|-
            -|D03~       A2 16|-
            -|D04        A1 15|-
            -|D05~       A0 14|-<- Button
            -|D06~            | Power:      
            -|D07          Vin|-
             |             GND|--- Button
            -|D08          GND|- 
            -|D09~         +5V|-
            -|D10~       +3.3V|-
            -|D11~         Res|-
            -|D12        IOREF|-
 NeoPixel -<-|D13 LED     --- |
      Gnd ---|GND             | 
            -|AREF            |
            -|SCL/PC5 (A5)    |
            -|SDA/PC4 (A4)    |
             |________________|
*/

/*
           NeoPixel chain                  
            _____1____     _____2____         ____60____
           |          |   |          |       |          |
    GND ---| GND  GND |---| GND  GND |- ... -| GND  GND |-
D13->-470R-| >DIN >DO |->-| >DIN >DO |- ... -| >DIN >DO |-
  VCC +5V -| +5V  +5V |---| +5V  +5V |- ... -| +5V  +5V |-
C 1000uF   |__________|   |__________|       |__________|

*/