// include the library
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16,2);
RTC_DS1307 clock;
byte hora, minuto, segundo, ds, dia, mes, x = 0;
uint16_t ano;


byte ES[8] =
{
        B00111,
        B01111,
        B11111,
        B11111,
        B11111,
        B11111,
        B11111,
        B11111
};
byte MS[8] =
{
        B11111,
        B11111,
        B11111,
        B00000,
        B00000,
        B00000,
        B11111,
        B11111
};
byte DS[8] = 
{
        B11100,
        B11110,
        B11111,
        B11111,
        B11111,
        B11111,
        B11111,
        B11111
};
byte EI[8] =
{
        B11111,
        B11111,
        B11111,
        B11111,
        B11111,
        B11111,
        B01111,
        B00111
};
byte MI[8] =
{
        B00000,
        B00000,
        B00000,
        B00000,
        B00000,
        B11111,
        B11111,
        B11111
};
byte DI[8] =
{
        B11111,
        B11111,
        B11111,
        B11111,
        B11111,
        B11111,
        B11110,
        B11100
};

byte MSA[8] =
{
        B11111,
        B11111,
        B11111,
        B00000,
        B00000,
        B00000,
        B00000,
        B00000
};

byte BLOCO[8] =
{
        B11111,
        B11111,
        B11111,
        B11111,
        B11111,
        B11111,
        B11111,
        B11111
};


void setup()
{
  clock.begin();
  // sets the LCD's rows and colums:
  lcd.begin(20, 4);
  // assignes each segment a write number
  lcd.createChar(1,ES);
  lcd.createChar(2,MS);
  lcd.createChar(3,DS);
  lcd.createChar(4,EI);
  lcd.createChar(5,MI);
  lcd.createChar(6,DI);
  lcd.createChar(7,MSA);
  lcd.createChar(8,BLOCO);
    
  pinMode(8,INPUT_PULLUP);
  pinMode(9,INPUT_PULLUP);
  pinMode(10,INPUT_PULLUP);  
}

void numero0()
{ 
  lcd.setCursor(x,0); 
  lcd.write(1);  
  lcd.write(7); 
  lcd.write(3);
  lcd.setCursor(x,1); 
  lcd.write(4);  
  lcd.write(5);  
  lcd.write(6);
}

void numero1()
{
  lcd.setCursor(x,0);
  lcd.write(7);
  lcd.write(3);
  lcd.write(32);
  lcd.setCursor(x,1);
  lcd.write(5);
  lcd.write(255);
  lcd.write(5);
}

void numero2()
{
  lcd.setCursor(x,0);
  lcd.write(2);
  lcd.write(2);
  lcd.write(3);
  lcd.setCursor(x,1);
  lcd.write(4);
  lcd.write(5);
  lcd.write(5);
}

void numero3()
{
  lcd.setCursor(x,0);
  lcd.write(2);
  lcd.write(2);
  lcd.write(3);
  lcd.setCursor(x,1);
  lcd.write(5);
  lcd.write(5);
  lcd.write(6); 
}

void numero4()
{
  lcd.setCursor(x,0);
  lcd.write(255);
  lcd.write(5);
  lcd.write(255);
  lcd.setCursor(x,1);
  lcd.write(32);
  lcd.write(32);
  lcd.write(255);
}

void numero5()
{
  lcd.setCursor(x,0);
  lcd.write(1);
  lcd.write(2);
  lcd.write(2);
  lcd.setCursor(x,1);
  lcd.write(5);
  lcd.write(5);
  lcd.write(6);
}

void numero6()
{
  lcd.setCursor(x,0);
  lcd.write(1);
  lcd.write(2);
  lcd.write(2);
  lcd.setCursor(x,1);
  lcd.write(4);
  lcd.write(5);
  lcd.write(6);
}

void numero7()
{
  lcd.setCursor(x,0);
  lcd.write(7);
  lcd.write(7);
  lcd.write(3);
  lcd.setCursor(x,1);
  lcd.write(32);
  lcd.write(1);
  lcd.write(32);
}

void numero8()
{
  lcd.setCursor(x,0); 
  lcd.write(1);  
  lcd.write(2); 
  lcd.write(3);
  lcd.setCursor(x,1); 
  lcd.write(4);  
  lcd.write(5);  
  lcd.write(6);
}

void numero9()
{
  lcd.setCursor(x,0); 
  lcd.write(1);  
  lcd.write(2); 
  lcd.write(3);
  lcd.setCursor(x,1); 
  lcd.write(32);  
  lcd.write(32);  
  lcd.write(255);
}

void doisP()
{
  lcd.setCursor(x,0);
  lcd.write(58);
  lcd.setCursor(x,1);
  lcd.write(58);
  delay(500);
  lcd.setCursor(x,0);
  lcd.write(32);
  lcd.setCursor(x,1);
  lcd.write(32);
  delay(500);
}

void mostrar(int num) 
{
  switch(num) 
  {
    case 0: numero0(); break;
    case 1: numero1(); break;
    case 2: numero2(); break;
    case 3: numero3(); break;
    case 4: numero4(); break;
    case 5: numero5(); break;
    case 6: numero6(); break;
    case 7: numero7(); break;
    case 8: numero8(); break;
    case 9: numero9(); break;
  }      
}  

void loop()
{
  DateTime t = clock.now();
  hora    = t.hour();
  minuto  = t.minute();
  segundo = t.second();  
  dia     = t.day();
  mes     = t.month();
  ano     = t.year();
  ds      = t.dayOfTheWeek();

  if(!digitalRead(8))      // acerta a hora
  {
    hora++;
    if(hora > 23) hora = 0;
    clock.adjust(DateTime(t.year(), t.month(), t.day(), hora, minuto, segundo));
    while(!digitalRead(8));
  }

  if(!digitalRead(9))     // acerta o minuto
  {
    minuto++;
    if(minuto > 59) minuto = 0;
    clock.adjust(DateTime(t.year(), t.month(), t.day(), hora, minuto, segundo));
    //while(!digitalRead(8));
  }

  lcd.setCursor(0,3);
  lcd.print(dia / 10);
  lcd.print(dia % 10);
  lcd.print("/");
  lcd.print(mes / 10);
  lcd.print(mes % 10);
  lcd.print("/");
  lcd.print(ano / 10);
  lcd.print(ano % 10);
  

  
  lcd.setCursor(12,3);
  switch(t.dayOfTheWeek())
  {
    case 0: lcd.print(" DOMINGO"); break;
    case 1: lcd.print(" SEGUNDA"); break;
    case 2: lcd.print("   TERCA");   break;
    case 3: lcd.print("  QUARTA");  break;
    case 4: lcd.print("  QUINTA");  break;
    case 5: lcd.print("   SEXTA");   break;
    case 6: lcd.print("  SABADO");  break;
  }
    
  x = 0;
  mostrar(hora / 10);
  x = 4;
  mostrar(hora % 10);

  x = 8;
  doisP();

  x = 10;
  mostrar(minuto / 10);
  x = 14;
  mostrar(minuto % 10);

  lcd.setCursor(18,1);
  lcd.print(segundo / 10);
  lcd.print(segundo % 10);  
  
  lcd.setCursor(0,2);
  lcd.print("--------------------");


} 
GND5VSDASCLSQWRTCDS1307+