// This program allows for several buttons to trigger specific colors.
// It also allows a button for rainbow and random effects.


#include <Adafruit_NeoPixel.h>

// Define the buttons and pin location
#define BUTTON_PIN_2   5  // Cycle/Random
#define BUTTON_PIN_3   6  // Red
#define BUTTON_PIN_4   7  // Green
#define BUTTON_PIN_5   8  // Blue
#define BUTTON_PIN_6   9  // Yellow
#define BUTTON_PIN_7   10  // Rainbow



#define PIXEL_PIN    3    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 30    // Number of LEDs in the Neopixel

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

// State variables and initial condition
bool oldState_2 = HIGH;
bool oldState_3 = HIGH;
bool oldState_4 = HIGH;
bool oldState_5 = HIGH;
bool oldState_6 = HIGH;
bool oldState_7 = HIGH;
int showType = 0;
int colorSpeed = 40; // Delay in pixel color wipe
int rainbowSpeed = .8; // Delay in rainbow wipe
int theaterSpeed = 100; // Delay in theater chase


void setup() {
  pinMode(BUTTON_PIN_2, INPUT_PULLUP);
  pinMode(BUTTON_PIN_3, INPUT_PULLUP);
  pinMode(BUTTON_PIN_4, INPUT_PULLUP);
  pinMode(BUTTON_PIN_5, INPUT_PULLUP);
  pinMode(BUTTON_PIN_6, INPUT_PULLUP);
  pinMode(BUTTON_PIN_7, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Get current button state.
  bool newState_2 = digitalRead(BUTTON_PIN_2);
  bool newState_3 = digitalRead(BUTTON_PIN_3);
  bool newState_4 = digitalRead(BUTTON_PIN_4);
  bool newState_5 = digitalRead(BUTTON_PIN_5);
  bool newState_6 = digitalRead(BUTTON_PIN_6);
  bool newState_7 = digitalRead(BUTTON_PIN_7);
 
  // Check if button 2 state changed from high to low (button press).
  if (newState_2 == LOW && oldState_2 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_2 = digitalRead(BUTTON_PIN_2);
    if (newState_2 == LOW) {
      showType++;
      if (showType > 3)
        showType=0;
      startShow(showType);}
    }
 
  // Check if button 3 state changed from high to low (button press).   
    else if (newState_3 == LOW && oldState_3 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_3 = digitalRead(BUTTON_PIN_3);
    if (newState_3 == LOW) {
      colorWipe(strip.Color(255, 0, 0), colorSpeed);  // Red
    }
    }

   // Check if button 4 state changed from high to low (button press).   
    else if (newState_4 == LOW && oldState_4 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_4 = digitalRead(BUTTON_PIN_4);
    if (newState_4 == LOW) {
      colorWipe(strip.Color(0, 128, 0), colorSpeed); // Green
    }
    }
   
    // Check if button 5 state changed from high to low (button press).
    else if (newState_5 == LOW && oldState_5 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_5 = digitalRead(BUTTON_PIN_5);
    if (newState_5 == LOW) {
      colorWipe(strip.Color(0, 0, 255), colorSpeed);  // Blue
    }
    }
   
     // Check if button 6 state changed from high to low (button press).
    else if (newState_6 == LOW && oldState_6 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_6 = digitalRead(BUTTON_PIN_6);
    if (newState_6 == LOW) {
      colorWipe(strip.Color(255, 255, 0), colorSpeed);  // Yellow
    }
    }

     // Check if button 7 state changed from high to low (button press).
    else if (newState_7 == LOW && oldState_7 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_7 = digitalRead(BUTTON_PIN_7);
    if (newState_7 == LOW) {
      rainbowCycle(rainbowSpeed);
    }
    }
   
 
  // Set the last button state to the old state.
  oldState_2 = newState_2;
  oldState_3 = newState_3;
  oldState_3 = newState_4;
  oldState_3 = newState_5;
  oldState_3 = newState_6;
  oldState_3 = newState_7;
}


//Cycle
void startShow(int i) {
  switch(i){
 
    case 0: theaterChase(strip.Color(127, 127, 127), theaterSpeed); // White
            break;
    case 1: theaterChase(strip.Color(127,   0,   0), theaterSpeed); // Red
            break;
    case 2: theaterChase(strip.Color(  0,   0, 127), theaterSpeed); // Blue
            break;
    case 3: theaterChase(strip.Color(  0,   127, 0), theaterSpeed); // Green
            break;
 
  }
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();
     
      delay(wait);
     
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}


// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else if(WheelPos < 170) {
    WheelPos -= 85;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
}