//***************************************************************
// Example of using an input (button) to trigger an update.
// Each press (or rather release in this case) of the button
// updates the display, increasing the number of lit pixels.
//
// After the last pixel is lit up, the next button press resets
// the strip (clears all pixels). Then it will start over again
// with the next button press.
//
// This example uses JChristensen's Button Library from:
//   https://github.com/JChristensen/JC_Button
//
// Open serial monitor to see output progress.
//
// Marc Miller, Feb 2017
//   Updated Jan 2020 - for JC_button library updates
//   Updated May 2022 - changed myButton.wasPressed to .wasReleased to
//                      make sure we only advace one pixel per press
//***************************************************************


#include "FastLED.h"
#define DATA_PIN    2
#define LED_TYPE    WS2811
#define COLOR_ORDER RGB
#define NUM_LEDS    21  // number of pixles in strip
#define BRIGHTNESS  100
#define counter
CRGB leds[NUM_LEDS];


// use JChristensen's Button library
#include "JC_Button.h"  
const uint8_t buttonPin = 8;  // digital pin used for debounced push button
Button myButton(buttonPin, 100, true, true);  // declare the button  (pin, dbTime, puEnable, invert)

const uint8_t buttonPin1 = 9;  // digital pin used for debounced push button
Button myButton1(buttonPin1, 100, true, true);  // declare the button  (pin, dbTime, puEnable, invert)


uint8_t pos = NUM_LEDS;  // set initial position
boolean input = false;  // set input status


//---------------------------------------------------------------
void setup() {
  Serial.begin(115200);  // Allows serial monitor output (check baud rate)
  delay(3000); // 3 second delay for recovery
  //FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(BRIGHTNESS);

  myButton.begin();  // initialize the button object
  startupBlink();  // blink display at startup
  Serial.println("Setup done. \n");
}


//---------------------------------------------------------------
void loop() {
  checkInput();  // calls funtion to check the input
 
  if (input == true) {
    pos++;  // Advance to next pixel
    if (pos > NUM_LEDS) {  // rollover back to start
      pos = 0;
      Serial.print("Position reset.  ");
    }
    if (pos == NUM_LEDS) {  // reset state reached, clear leds
      FastLED.clear();
      Serial.print("Reset, pixels cleared.  ");
    }
    else {
      if (pos == 20) {
        fill_solid(leds, NUM_LEDS, CRGB::Lime);
      }
      else {
        leds[pos] = (CRGB(221, 22, 22));  // turn on new pixel
      }
    }
  
    Serial.print("pos = "); Serial.println(pos);
  }
  
  checkInput2();  // calls funtion to check the second input
 
  if (input == true) {
     if (pos > 14) {  // reset state reached, clear leds
      FastLED.clear();
      Serial.print("Reset, pixels cleared.  ");

    for ( int i = 0; i < 14 ; i++){
      pos = i;
      Serial.print("pos = "); Serial.println(pos);
      leds[pos] = (CRGB(221, 22, 22));  // turn on new pixel
    }
     }
    pos = 13;  // Reset to position 13
    
    Serial.print("pos = "); Serial.println(pos);
  
  }
  FastLED.show();
}//end_main_loop




//---------------------------------------------------------------
void checkInput(){  // function to check input
  myButton.read();
  //if(myButton.wasPressed() ) {
  if(myButton.wasReleased() ) {
    Serial.print("Button pressed.  ");
    input = true;  // set input status to true
  } else {
    input = false;
  }
}//end_checkInput

//---------------------------------------------------------------
void checkInput2(){  // function to check input
  myButton1.read();
  //if(myButton.wasPressed() ) {
  if(myButton1.wasReleased() ) {
    Serial.print("Button pressed.  ");
    input = true;  // set input status to true
  } else {
    input = false;
  }
}//end_checkInput

//---------------------------------------------------------------
void startupBlink(){  // function to blink display upon startup
  fill_solid(leds, NUM_LEDS, CRGB(206, 51, 27));
  FastLED.show();
  delay(1000);
  fill_solid(leds, NUM_LEDS, CRGB(213, 213, 40));
  FastLED.show();
  delay(1000);
  FastLED.clear();
  FastLED.show();
}//end_startupBlink