Advertisement
cisco404

C++ | Digital Clock with RTC

Apr 30th, 2020 (edited)
999
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | Source Code | 0 0
  1. #include <Arduino.h>
  2. #include <TimeLib.h>
  3. #include <Wire.h>
  4. #include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
  5. #include <LiquidCrystal.h>
  6. void digitalClockDisplay();
  7. void printDigits(int digits);
  8. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  9.  
  10. // -------------------------------------------
  11. // Digital Clock with RTC
  12. // www.ardukode.blogspot.com
  13. // -------------------------------------------
  14.  
  15. void setup()  {
  16.   lcd.begin(16,2);
  17.   while (!Serial);
  18.   delay(200);
  19. }
  20.  
  21. void loop() {
  22.   tmElements_t tm;
  23.   if (RTC.read(tm))
  24.   {
  25.     lcd.clear();
  26.     lcd.setCursor(0,0);
  27.     lcd.println("Tgl:");
  28.     printDigits(tm.Day);
  29.     lcd.print(".");
  30.     printDigits(tm.Month);
  31.     lcd.print(".");
  32.     lcd.print(tmYearToCalendar(tm.Year));
  33.  
  34.     lcd.setCursor(0,1);
  35.     lcd.print("Jam: ");
  36.     printDigits(tm.Hour);
  37.     lcd.print(":");
  38.     printDigits(tm.Minute);
  39.     lcd.print(":");
  40.     printDigits(tm.Second);
  41.   }
  42.   else
  43.   {
  44.     if (RTC.chipPresent())
  45.     {
  46.       lcd.print("DS1307 Stop");
  47.     }else
  48.     {
  49.       lcd.print("DS1307 Error");
  50.     }
  51.     delay(9000);
  52.   }
  53.   delay(1000);
  54. }
  55.  
  56. void printDigits(int digits){
  57.   // utility function for digital clock display: prints preceding colon and leading 0
  58.   if (digits >= 0 && digits < 10)
  59.   {
  60.     lcd.write('0');
  61.   }
  62.   lcd.print(digits);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement