/* This code is to fade on and off a string of NeoPixels based on a continuous time count. So the pixels show time, like an alarm clock system. ~ by Janaki Ranpura, Fab Lab Barcelona, 20170621. */

#include <Adafruit_NeoPixel.h>

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 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)
 Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, 6, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit... if you must, connect GND first.

// part Janaki wrote starts here:
//

// constants, things that don't change

const int blank = 0;         // calling "off" as blank
const int minBright = 5;
const int maxBright = 191;   //  (75% brightness; 100% is 255)

const int pixPin = 6;
const int motorPin = 7;


// variables, things that change

int pixState0 = blank;
int pixState1 = blank;
// int motorState = low;
long previousMillis = 0;

long onTime0 = 750;       // keep in mind these are NOT the intervals at which the pix pulse; they are just markers ... 
                          // it is still "delay" that is controlling the timing of the visualization
long offTime0 = 250;
long onTime1 = 750;
long offTime1 = 250;

// initializing
 
void setup() {
  strip.begin();
  strip.show(); // initialize all pixels to "off"
//  pinMode (motorPin, OUTPUT);
}


// what to repeat over and over and over ...

void loop() {  // all of the "or equal to" connectors in here create smooth fading up and down from the get-go
  uint16_t j; // declare the variable j
  unsigned long currentMillis = millis (); // check state of variable
  
  if ((pixState0 <= minBright) && (currentMillis - previousMillis >= offTime0)) 
  {   
      for (j <= minBright; j < maxBright; j++) {     // j = brightness of NeoPix
         strip.setPixelColor(0 ,0 ,0 ,j);  
         strip.setPixelColor(1 ,0 ,0 ,0);
         strip.setPixelColor(3 ,0 ,0 ,0);
         delay(20); // this is the code that elongates the period of brightening; it has to come BEFORE "show"
         strip.show();
         }

//        previousMillis=currentMillis; // remember the time -- NB if I uncomment this, nothing turns on
    }

  if (currentMillis - previousMillis >= onTime0) 
   {
       for (j = maxBright; j > minBright; j--) {     // j = brightness of NeoPix
         strip.setPixelColor(0 ,0 ,0 ,j);  
         delay(10); // this is the code that elongates the period of brightening; it has to come BEFORE "show"
         strip.show();

 //         for (j >= minBright; j < maxBright; j++) {     // j = brightness of NeoPix
 //           strip.setPixelColor(0 ,0 ,0 ,0);
 //           strip.setPixelColor(1 ,0 ,0 ,j);  
 //           delay(20); // this is the code that elongates the period of brightening; it has to come BEFORE "show"
 //           strip.show();
         
          }

          previousMillis=currentMillis; // remember the time
  }


   if (pixState1 <= minBright) /* && (currentMillis - previousMillis >= offTime1))  -- if I uncomment this, Pix1 pops up instead of fades up*/
  {     
      for (j <= minBright; j < maxBright; j++) {     // j = brightness of NeoPix
         strip.setPixelColor(1 ,0 ,0 ,j);  
         strip.setPixelColor(0 ,0 ,0 ,0);
         delay(20); // this is the code that elongates the period of brightening; it has to come BEFORE "show"
         strip.show();
         }

//         previousMillis=currentMillis; // remember the time
         
    }


   if ((pixState1 >= minBright) && (currentMillis - previousMillis >= onTime1)); 
   {
       for (j = maxBright; j > minBright; j--) {     // j = brightness of NeoPix
         strip.setPixelColor(1 ,0 ,0 ,j);
         strip.setPixelColor(0 ,0 ,0 ,0);  
         delay(10); // this is the code that elongates the period of brightening; it has to come BEFORE "show"
         strip.show();
          }

          previousMillis=currentMillis; // remember the time
  }
 
}

