#include <Time.h>
#include <DS1307RTC.h>
char REG [8];
tmElements_t te;
int c,reset=1;
void setup() {
  pinMode(13, OUTPUT); 
  pinMode(11, OUTPUT); 
  pinMode(8, OUTPUT);  
  pinMode(5, OUTPUT);  
  pinMode(3, INPUT);   
  Serial.begin(57600);
  
  
  
  
  
  
  
  
  
}
void loop()
{ 
if(digitalRead(3))                    
  {RTC.read(te);
   SetShadowTime(te.Hour,te.Minute,te.Second,2);        
   delay(900);
   reset=1;
  }
else                                   
  {wait1();
   reset=1;
  }
                                         
  for(int j = 0; j<8 ; j++) SetUpLightByMask(j,0); 
   
 
}
void SetUpLightByMask(int RegNum, char LightMask) 
{
  
  digitalWrite(5, LOW);
  digitalWrite(11, LOW);
  shiftOut(8, 13, MSBFIRST, LightMask);
  shiftOut(8, 13, LSBFIRST, RegNum);
  digitalWrite(11, HIGH);
  digitalWrite(5, HIGH);
}
void digitalClockDisplay() {  
  RTC.read(te);
  Serial.print(te.Hour);
  Serial.print(" : ");
  Serial.print(te.Minute);
  Serial.print(" :");
  Serial.print(te.Second);
  Serial.print(" ");
  Serial.print(te.Day);
  Serial.print(" ");
  Serial.print(te.Month);
  Serial.print(" ");
  Serial.print(tmYearToCalendar(te.Year));
  Serial.println();
}
void SetShadowTime(int Hours, int Minutes, int Seconds, int param){   
  int h,hshift,m,s;
  for(int j = 0; j<8 ; j++) REG[j] = 0;
  if(Hours >= 12) Hours -= 12;
  h = Hours + 6;
  if(h >= 12) h -= 12;
    
  hshift = (int) Minutes / 12;
  
  REG[(int)(((h*5)+hshift)/8)] = REG[(int)(((h*5)+hshift)/8)] | 1<<(((h*5)+hshift)%8);
             
   
  if(param == 1)
  {m = Minutes + 30;
   if(m >= 60) m -= 60;
   REG[(int)(m/8)] = REG[(int)(m/8)] | 1<<(m%8);
   }
  if(param == 2)
  {m = Minutes + 30;
   if(m >= 60) m -= 60;
   REG[(int)(m/8)] = REG[(int)(m/8)] | 1<<(m%8);
     
   s = Seconds + 30;
   if(s >= 60) s -= 60;
   REG[(int)(s/8)] = REG[(int)(s/8)] | 1<<(s%8);
  }
  for(int j = 0; j<8 ; j++) SetUpLightByMask(j,REG[j]);
  }
  
void wait1() 
{for(int a = 0; a < 8; a++)
    {c=0;
     for(int b = 0; b < 8; b++)
      {c = c << 1;
       c = c | 1;
       SetUpLightByMask(a, c);  
       delay(10);   
      }
   }
  for(int a = 0; a < 8; a++)
    {c=255;
      for(int b = 0; b < 8; b++)
      {c = c << 1;
       SetUpLightByMask(a, c);
       delay(10);     
      }
    }  
}