// Jam digital dan tanggal
// SMK Negri 9 Kota Bekasi
 
// Kode ke LCD 16x2
#include <LiquidCrystal.h>
// include Wire library code (needed for I2C protocol devices)
#include <Wire.h>
 
// Koneksi LCD ke pin Arduino
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
void setup() {
  pinMode(8, INPUT_PULLUP);                      // tombol pin 8
  pinMode(9, INPUT_PULLUP);                      // tombol pin 9
  // LCD Karakter 16x2 untuk baris dan kolom
  lcd.begin(16, 2);
  Wire.begin();                                  
}
 
char Time[]     = "TIME:  :  :  ";
char Calendar[] = "DATE:  -  -20  ";
byte i, second, minute, hour, date, month, year;
 
void DS1307_display(){
  // Convert BCD ke decimal
  second = (second >> 4) * 10 + (second & 0x0F);
  minute = (minute >> 4) * 10 + (minute & 0x0F);
  hour   = (hour >> 4)   * 10 + (hour & 0x0F);
  date   = (date >> 4)   * 10 + (date & 0x0F);
  month  = (month >> 4)  * 10 + (month & 0x0F);
  year   = (year >> 4)   * 10 + (year & 0x0F);
  // konversi akhir
  Time[12]     = second % 10 + 48;
  Time[11]     = second / 10 + 48;
  Time[9]      = minute % 10 + 48;
  Time[8]      = minute / 10 + 48;
  Time[6]      = hour   % 10 + 48;
  Time[5]      = hour   / 10 + 48;
  Calendar[14] = year   % 10 + 48;
  Calendar[13] = year   / 10 + 48;
  Calendar[9]  = month  % 10 + 48;
  Calendar[8]  = month  / 10 + 48;
  Calendar[6]  = date   % 10 + 48;
  Calendar[5]  = date   / 10 + 48;
  lcd.setCursor(0, 0);
  lcd.print(Time);                               // tampilan waktu
  lcd.setCursor(0, 1);
  lcd.print(Calendar);                           // tampilan kalender
}
void blink_parameter(){
  byte j = 0;
  while(j < 10 && digitalRead(8) && digitalRead(9)){
    j++;
    delay(25);
  }
}
byte edit(byte x, byte y, byte parameter){
  char text[3];
  while(!digitalRead(8));                        
  while(true){
    while(!digitalRead(9)){                      
      parameter++;
      if(i == 0 && parameter > 23)               
        parameter = 0;
      if(i == 1 && parameter > 59)               
        parameter = 0;
      if(i == 2 && parameter > 31)              
        parameter = 1;
      if(i == 3 && parameter > 12)            
        parameter = 1;
      if(i == 4 && parameter > 99)            
        parameter = 0;
      sprintf(text,"%02u", parameter);
      lcd.setCursor(x, y);
      lcd.print(text);
      delay(200);                                
    }
    lcd.setCursor(x, y);
    lcd.print("  ");                            
    blink_parameter();
    sprintf(text,"%02u", parameter);
    lcd.setCursor(x, y);
    lcd.print(text);
    blink_parameter();
    if(!digitalRead(8)){                        
      i++;                                       
      return parameter;                          
    }
  }
}
 
void loop() {
  if(!digitalRead(8)){                           // jika tombol (pin8) di pijit
      i = 0;
      hour   = edit(5, 0, hour);
      minute = edit(8, 0, minute);
      date   = edit(5, 1, date);
      month  = edit(8, 1, month);
      year   = edit(13, 1, year);
      // Convert decimal to BCD
      minute = ((minute / 10) << 4) + (minute % 10);
      hour = ((hour / 10) << 4) + (hour % 10);
      date = ((date / 10) << 4) + (date % 10);
      month = ((month / 10) << 4) + (month % 10);
      year = ((year / 10) << 4) + (year % 10);
      // End conversion
      // Write data to DS1307 RTC
      Wire.beginTransmission(0x68);               
      Wire.write(0);                              
      Wire.write(0);                             
      Wire.write(minute);                        
      Wire.write(hour);                           
      Wire.write(1);                             
      Wire.write(date);                         
      Wire.write(month);                          
      Wire.write(year);                           
      Wire.endTransmission();                     
      delay(200);                                 
    }
    Wire.beginTransmission(0x68);                
    Wire.write(0);                                
    Wire.endTransmission(false);                  
    Wire.requestFrom(0x68, 7);                    
    second = Wire.read();                         
    minute = Wire.read();                      
    hour   = Wire.read();                         
    Wire.read();                               
    date   = Wire.read();                       
    month  = Wire.read();                       
    year   = Wire.read();                      
    DS1307_display();                            
    delay(50);                                  
}
GND5VSDASCLSQWRTCDS1307+