#include <Wire.h>
#include <stdio.h>
#include <Adafruit_NeoPixel.h>
#include <RTClib.h>

RTC_DS1307 RTC;

// Define things here and set things up.
#define LED_Loop 60
#define PIN 2

// 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(LED_Loop, PIN, NEO_GRB + NEO_KHZ800);

int LED[LED_Loop];
int ONE = (LED_Loop / 12);
int TWO = (LED_Loop / 6);
int THREE = (LED_Loop / 4);
int FOUR = (LED_Loop / 3);
int FIFE = (THREE / 2.4);
int SIX = (THREE * 2);
int SEVEN = (FOUR + THREE);
int EIGTH = (LED_Loop / 1.5);
int NINE = (THREE * 3);
int TEN = (LED_Loop / 6);
int ELEVEN = (LED_Loop - 10);
int TWELVE = (LED_Loop - 1);

int HR_Fade = 7;
int MN_Fade;

long HR_Colour;
long SE_Colour = 0x2986cc;

long THIS_LED;
int Led_Flag;
int argh;

// trying this way to get colours working.
int HR_R;
int HR_G;
int HR_B;

int HR1_R = 0x55;
int HR1_G = 0;
int HR1_B = 0;

int HR2_R = 0x0D;
int HR2_G = 0;
int HR2_B = 0x0D;

int MN_R = 0;
int MN_G = 33;
int MN_B = 0;

//int SE_R = 0;
//int SE_G = 0;
//int SE_B = 0x55;

int hour_led;
int minute_led;
int second_led;
int new_minute;

//-----------------------------  Set up here -----------------------------//
void setup() {
  //  This is just to give you time to open the window and watch
  // put your setup code here, to run once:
  delay(2000);
  /*
    Serial.begin(9600);
    Serial.println("-------------------------------");
    Serial.println("Setting up");
  */
  Wire.begin();
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  strip.setBrightness(255);

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  /*
    Serial.println("Done setting up");
    Serial.println("-------------------------------");
  */
}

//-----------------------------  MAIN LOOP here -----------------------------//
void loop() {
  // Get time
  DateTime now = RTC.now();
  // 24 hour time change colour of hour hand.
  int hr = now.hour();
  if (hr < 12) {
    // HR_Colour = HR_Colour1;
    HR_R = HR1_R;
    HR_G = HR1_G;
    HR_B = HR1_B;
  } else {
    // HR_Colour = HR_Colour2;
    HR_R = HR2_R;
    HR_G = HR2_G;
    HR_B = HR2_B;
  }
  // This line is only incase any maths had to be done.
  int mins = now.minute();
  second_led = now.second();
  //  calculate leds
  hour_led = (((LED_Loop / 12) * hr) + (mins / (LED_Loop / 5))) % LED_Loop;
  if (hour_led == 60) {
    hour_led = 59;
  }
  minute_led = mins;

  // Show LEDs ------------------   Main loop here -----------------
  // Keep this at the top so it doesn't mess up any other settings when LEDs are
  // turned on.
  strip.setPixelColor(second_led - 1, SE_Colour / 2);
  strip.setPixelColor(second_led - 2, SE_Colour / 4);
  strip.setPixelColor(second_led - 3, SE_Colour / 8);
  strip.setPixelColor(second_led - 4, SE_Colour / 16);
  strip.setPixelColor(second_led - 5, 0);

  // show
  strip.setPixelColor (ONE, 0x6fa8dc);
  strip.setPixelColor (TWO, 0x6fa8dc);
  strip.setPixelColor (THREE, 0x3d85c6);
  strip.setPixelColor (FOUR, 0x6fa8dc);
  strip.setPixelColor (FIFE, 0x6fa8dc);
  strip.setPixelColor (SIX, 0x3d85c6);
  strip.setPixelColor (SEVEN, 0x6fa8dc);
  strip.setPixelColor (EIGTH, 0x6fa8dc);
  strip.setPixelColor (NINE, 0x3d85c6);
  strip.setPixelColor (TEN, 0x6fa8dc);
  strip.setPixelColor (ELEVEN, 0x6fa8dc);
  strip.setPixelColor (TWELVE, 0x3d85c6);

  //  Now start setting LEDs below here.
  if (second_led == 0) {
    strip.setPixelColor(LED_Loop - 1, SE_Colour / 2);
    strip.setPixelColor(LED_Loop - 2, SE_Colour / 4);
    strip.setPixelColor(LED_Loop - 3, SE_Colour / 8);
    strip.setPixelColor(LED_Loop - 4, SE_Colour / 16);
    strip.setPixelColor(LED_Loop - 5, 0);
    new_minute = 1;
  }
  if (second_led == 1) {
    strip.setPixelColor(second_led - 1, SE_Colour / 2);
    strip.setPixelColor(LED_Loop - 1, SE_Colour / 4);
    strip.setPixelColor(LED_Loop - 2, SE_Colour / 8);
    strip.setPixelColor(LED_Loop - 3, SE_Colour / 16);
    strip.setPixelColor(LED_Loop - 4, 0);
  }
  if (second_led == 2) {
    strip.setPixelColor(second_led - 1, SE_Colour / 2);
    strip.setPixelColor(second_led - 2, SE_Colour / 4);
    strip.setPixelColor(LED_Loop - 1, SE_Colour / 8);
    strip.setPixelColor(LED_Loop - 2, SE_Colour / 16);
    strip.setPixelColor(LED_Loop - 3, 0);
  }
  if (second_led == 3) {
    strip.setPixelColor(second_led - 1, SE_Colour / 2);
    strip.setPixelColor(second_led - 2, SE_Colour / 4);
    strip.setPixelColor(second_led - 3, SE_Colour / 8);
    strip.setPixelColor(LED_Loop - 1, SE_Colour / 16);
    strip.setPixelColor(LED_Loop - 2, 0);
  }
  if (second_led == 4) {
    strip.setPixelColor(second_led - 1, SE_Colour / 2);
    strip.setPixelColor(second_led - 2, SE_Colour / 4);
    strip.setPixelColor(second_led - 3, SE_Colour / 8);
    strip.setPixelColor(second_led - 4, SE_Colour / 16);
    strip.setPixelColor(LED_Loop - 1, 0);
  }
  /*
    if (Led_Flag == 0) {
    //
    Led_Flag = 1;
    THIS_LED = strip.getPixelColor(second_led);
    // This is where I am at.
    Serial.print(second_led);
    Serial.print(" ");
    Serial.println(THIS_LED);
    }
  */
  /*----------------  Draw SECOND HAND on clock ----------------*/
  strip.setPixelColor(second_led, SE_Colour);
  // strip.setPixelColor(second_led,SE_Colour+THIS_LED);
  // strip.setPixelColor(second_led-1,THIS_LED);
  if (new_minute == 1) {
    //new_minute = 0;
    //  strip.setPixelColor(minute_led-1,MN_Colour/50);
  }
  /*----------------  Draw MINUTE HAND on clock ----------------*/
  //strip.setPixelColor(minute_led,MN_Colour);
  //  MN_Fade for fading.
  strip.setPixelColor(minute_led, MN_R, MN_G, MN_B);
  strip.setPixelColor(minute_led + 1, MN_R, (MN_G * (second_led * 10 / 6) / 100), MN_B);
  strip.setPixelColor(minute_led - 1, MN_R, (MN_G * (100 - (second_led * 10 / 6)) / 100), MN_B);
  /*----------------  Draw HOUR HAND on clock ----------------*/
  strip.setPixelColor(hour_led, HR_R, HR_G, HR_B);
  //strip.setPixelColor((hour_led-1)%LED_Loop,HR_R/HR_Fade,HR_G,HR_B/HR_Fade);
  //strip.setPixelColor((hour_led+1)%LED_Loop,HR_R/HR_Fade,HR_G,HR_B/HR_Fade);
  /*
    strip.setPixelColor(hour_led,HR_Colour);
    strip.setPixelColor((hour_led-1)%LED_Loop,HR_Colour);
    strip.setPixelColor((hour_led+1)%LED_Loop,HR_Colour);
  */
  if (second_led > minute_led) {
    new_minute = 0;
  }
  /*
    if (second_led != argh)
    {
    Led_Flag = 0;
    argh = second_led;
    }
  */
  // show alarms
  strip.show();
  delay(400);
  // ------------------   End of Main loop here -----------------
}
/*
for (i = 0; i < 8; i++) {
  myRing.setPixelColor(i, R, G, B);
  myRing.show();
  delay(100);
} for (i = 0; i < 8; i++) {
  myRing.setPixelColor(i, 0, 0, 0);
  myRing.show();
  delay(100);
}
for (i = 0; i < 8; i++) {
  myRing.setPixelColor(i, R, G, B);
  myRing.setPixelColor(i - 1, 0, 0, 0);
  myRing.show();
  delay(100);
}
for (i = 0; i < 8; i++) {
  myRing.setPixelColor(i, R, G, B);
  if (i == 0)
    myRing.setPixelColor(7, 0, 0, 0);
  else
    myRing.setPixelColor(i - 1, 0, 0, 0);
  myRing.show();
  delay(100);
}
for (i = 0; i < 8; i++) {
  myRing.setPixelColor(i, R, G, B);
  myRing.setPixelColor((i - 1) & 0x7, 0, 0, 0);
  myRing.show();
  delay(100);
}
for (i = 7 ; i >= 0 ; i-- ) {
  myRing.setPixelColor(i, R, G, B);
  myRing.setPixelColor(i + 1, 0, 0, 0);
  myRing.show();
  delay(100);
}
for (i = 7; i >= 0; i--) {
  myRing.setPixelColor(i, R, G, B);
  myRing.setPixelColor((i + 1) & 0x7, 0, 0, 0);
  myRing.show();
  delay(100);
}
for (i = 0; i < 8; i++) {
  myRing.setPixelColor(i, R, G, B);
  myRing.setPixelColor((i - 1) & 0x7, 0, 0, 0);
  myRing.show();
  delay(100);
}
for (i = 0; i < 8; i++) {
  myRing.setPixelColor(xref[i], R, G, B);
  myRing.setPixelColor((xref[i - 1] ) & 0x7, 0, 0, 0);
  myRing.show();
  delay(100);
}
for (i = 1; i < 7; i++) {
  myRing.setPixelColor(xref[i], R, G, B);
  myRing.setPixelColor(xref[i - 1], 0, 0, 0);
  myRing.show();
  delay(100);
}
for (i = 4; i < 12; i++) {
  myRing.setPixelColor(xref[i],  255, 255, 255);
  myRing.setPixelColor(xref[i - 1], 127, 127, 127);
  myRing.setPixelColor(xref[i - 2], 63, 63, 63);
  myRing.setPixelColor(xref[i - 3], 31, 31, 31);
  myRing.setPixelColor(xref[i - 4],  0,  0,  0);
  myRing.show();
  delay(100);
}
// One ring; six pixels numbered 0 through 5// Three fields: previous, next, & indexconst
byte chains[6][3] = {  {5, 1, 0}, {0, 2, 1}, {1, 3, 2}, {2, 4, 3}, {3, 5, 4}, {4, 0, 5}};
#define pPrevious 0
// 'p' = pointer#define pNext     1
// 'p' = pointer#define dIndex    2
// 'd' = data#define sRing     0
// 1st element in ring
void controlRing() {
  loadRing (sRing, 255, 0, 0);
  myRing.show();
  delay(100);
  loadRing (sRing, 0, 255, 0);
  myRing.show();
  delay(100);
  loadRing (sRing, 0, 0, 255);
  myRing.show();
  delay(100);
} void loadRing (byte pRing, byte R, byte G, byte B) {
  byte tRing = pRing;
  do {
    myRing.setPixelColor (chains[tRing][dIndex], R, G, B);
    tRing = chains[tRing][pNext];
  } while (tRing != pRing)
  }
// Three rings, each with six pixels 0 through 5
// Three fields: previous, next, & indexconst
byte chains[18][3] = {
  {5, 1, 0},
  {0, 2, 1},
  {1, 3, 2},
  {2, 4, 3},
  {3, 5, 4},
  {4, 0, 5},
  {11, 7, 6},
  {6, 8, 7},
  {7, 9, 8},
  {8, 10, 9},
  {9, 11, 10},
  {10, 6, 11},
  {17, 13, 12},
  {12, 14, 13},
  {13, 15, 14},
  {14, 16, 15},
  {15, 17, 16},
  {16, 12, 17}
};
#define pPrevious 0
// 'p' = pointer#define pNext     1
// 'p' = pointer#define dIndex    2
// 'd' = data#define sRing1    0
// 1st element ring 1#define sRing2    6
// 1st element ring 2#define sRing3   12
// 1st element ring 3
void controlRings() {
  loadRings (sRing1, 255, 0, 0);
  loadRings (sRing2, 0, 255, 0);
  loadRings (sRing3, 0, 0, 255);
  myRings.show();
  delay(100);
} void loadRings (byte pRing, byte R, byte G, byte B) {
  byte tRing = pRing;
  do {
    myRings.setPixelColor (chains[tRing][dIndex], R, G, B);
    tRing = chains[tRing][pNext];
  } while (tRing != pRing)
  }
// Load the music color datavoid
loadNeoArray (byte sRing, byte xRing) {
  byte tsRing = sRing;
  // sRing = Original start position byte txRing = xRing;
  // xRing = Rotated start position
  do {
    rings.setPixelColor(chains[txRing][qIndex],
                        tColorsMusic[chains[tsRing][qBands]][iR],
                        tColorsMusic[chains[tsRing][qBands]][iG],
                        tColorsMusic[chains[tsRing][qBands]][iB]);
    tsRing = chains[tsRing][qClk];
    txRing = chains[txRing][qClk];
  } while (txRing != xRing);
}*/
GND5VSDASCLSQWRTCDS1307+