#include <Wire.h>
#include "LedControl.h"
#include "RTClib.h"

RTC_DS1307 rtc;
LedControl lc=LedControl(12,11,10,1);


void setup() {
  Serial.begin(57600);
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  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__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
    lc.shutdown(0,false);
    lc.setIntensity(0,8);
    lc.clearDisplay(0);
}



void loop() {
  DateTime now = rtc.now();
  
  if(now.second() < 30 || now.second() > 40)
    {
      lc.setDigit(0,0,now.second()%10,false);
      lc.setDigit(0,1,now.second()/10,false);
      lc.setChar(0,2,'-',false);
      lc.setDigit(0,3,now.minute()%10,false);
      lc.setDigit(0,4,now.minute()/10,false);
      lc.setChar(0,5,'-',false);
      lc.setDigit(0,6,now.hour()%10,false);
      lc.setDigit(0,7,now.hour()/10,false);
  
    }

  if (now.second() == 30 || now.second() == 40) 
    {
      lc.clearDisplay(0);
    }

  if(now.second() >= 31 && now.second() < 40)
    {
      lc.setDigit(0,6,now.day()%10,true);
      lc.setDigit(0,7,now.day()/10,false);
      lc.setDigit(0,4,now.month()%10,true);
      lc.setDigit(0,5,now.month()/10,false);
      lc.setDigit(0,0,(now.year()%1000)%10,false);
      lc.setDigit(0,1,(now.year()%1000)/10,false);
      lc.setDigit(0,2,(now.year()%1000)/100,false);
      lc.setDigit(0,3,now.year()/1000,false);  
    }
}
