#include <FastLED.h>
#include <Wire.h>
#include <Time.h>
#include "DS3231.h"

// This is not time, it is divided millis()
#define seconds() 		((unsigned long)millis()/1000)
#define minutes() 		((unsigned int)seconds()/60)
#define hours()		    ((unsigned int)minutes()/60)

#define DATA_PIN     8
#define NUM_LEDS    81
#define BRIGHTNESS  255
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 20

 
CRGBPalette16 myPal = RainbowColors_p;

  //  CloudColors_p
  //  LavaColors_p
  //  OceanColors_p
  //  ForestColors_p
  //  RainbowColors_p
  //  RainbowStripeColors_p
  //  PartyColors_p
  //  HeatColors_p


time_t t;
int i;

void setup() {
    // delay(2000); // POWER UP

    adjustTime();

    Serial.begin(9600);
    // Wire.begin(); // Serial
    // // DS3231
    // readDS3231time(&t_sec, &t_min, &t_hr, &t_wd, &t_dy, &t_mon, &t_yr);
    // setTime(t_hr, t_min, t_sec, t_dy, t_mon, 2000 + t_yr);

      // set the initial time here:
      //		if (yr < 1) {
      //			// DS3231  seconds, minutes, hours, wday, date, month, year
      //			setDS3231time( 0,      27,    23,    7,   12,     6,   16);
      //		}

    t = now();
    // DEBUG
    Serial.print("Date/Time: ");
    Serial.print(day(t), DEC);          Serial.print(".");
    Serial.print(month(t), DEC);        Serial.print(".");
    Serial.print(year(t) - 2000, DEC);  Serial.print(" ");
    Serial.print(hour(t), DEC);         Serial.print(":");
    Serial.print(minute(t), DEC);       Serial.println(".");

    FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
    FastLED.setBrightness(BRIGHTNESS);

    //   JUST TO SEE POSITIONS:
    // for (int i=0; i<NUM_LEDS ; i++ ) {
    //     leds[i] = ColorFromPalette (myPal,i*255/NUM_LEDS,255); 
    //     FastLED.delay(10);
    // }
    // FastLED.delay(1000);
}


/**    
 **
 *******/ 
void loop() {
    int         a = millis();
    static long last_a;
    static int left_inside_index;
    static int right_inside_index;
    static uint8_t color_index;

    // inside circle 0-16 and 40-56
    if (last_a < millis()-100) {
        last_a = millis();

        left_inside_index += left_inside_index >= 16 ? -left_inside_index : random16(2);
        // right_inside_index += right_inside_index >= 40 ? -random16(2) : 16;
        right_inside_index = left_inside_index + 40;
    }

    color_index = abs( (millis()%7200) * (double)255/3600 -255 );

    for( i = 0 ; i< NUM_LEDS ; i++ ) {
        leds[i] = ColorFromPalette(myPal,color_index,255);
    }

    // for (i = 0 ; i< (NUM_LEDS-1)/2 ; i++) {
    //     leds[i] = CRGB::Gray;
    // }
    // for (i = (NUM_LEDS-1)/2 ; i< (NUM_LEDS-1) ; i++ ) {
    //     leds[i] = CRGB::Gray;
    // }

    // Eyeballs
    for (i = 0 ; i< 16 ; i++) {
        leds[i] = 0;
    }
    for (i = 40 ; i< 56 ; i++) {
        leds[i] = 0;
    }
    leds[ left_inside_index ] = CRGB::Yellow;
    leds[ right_inside_index ] = CRGB::Yellow;

    // NOSE
    leds[NUM_LEDS-1] = CRGB::Red;

    FastLED.delay(1000/UPDATES_PER_SECOND);
}




// Update time from sensor.
void adjustTime() {
    byte t_sec, t_min, t_hr, t_wd, t_dy, t_mon, t_yr;

    Wire.begin();
    // DS3231
    readDS3231time(&t_sec, &t_min, &t_hr, &t_wd, &t_dy, &t_mon, &t_yr);
    setTime(t_hr, t_min, t_sec, t_dy, t_mon, 2000 + t_yr);
    Wire.end();
}

GND5VSDASCLSQWRTCDS1307+