Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <TimeLib.h>
  3. //#include <DateTime.h>
  4. #include <DS1307RTC.h>
  5.  
  6. void setup() {
  7.   Serial.begin(9600);
  8.   while (!Serial) ; // wait for serial
  9.   delay(200);
  10.   setSyncProvider(RTC.get); //pega do RTC
  11.   if(timeStatus()!= timeSet)
  12.      Serial.println("Unable to sync with the RTC");
  13.   else
  14.      Serial.println("RTC has set the system time");      
  15.   Serial.println("DS1307RTC Read Test");
  16.   Serial.println("-------------------");
  17. }
  18.  
  19. void loop() {
  20.   tmElements_t tm;
  21.  
  22.   if (RTC.read(tm)) {
  23.     Serial.print("Ok, Time = ");
  24.     print2digits(tm.Hour);
  25.     Serial.write(':');
  26.     print2digits(tm.Minute);
  27.     Serial.write(':');
  28.     print2digits(tm.Second);
  29.     Serial.print(", Date (D/M/Y) = ");
  30.     Serial.print(tm.Day);
  31.     Serial.write('/');
  32.     Serial.print(tm.Month);
  33.     Serial.write('/');
  34.     Serial.print(tmYearToCalendar(tm.Year));
  35.     Serial.println();
  36.   } else {
  37.     if (RTC.chipPresent()) {
  38.       Serial.println("The DS1307 is stopped.  Please run the SetTime");
  39.       Serial.println("example to initialize the time and begin running.");
  40.       Serial.println();
  41.     } else {
  42.       Serial.println("DS1307 read error!  Please check the circuitry.");
  43.       Serial.println();
  44.     }
  45.     delay(9000);
  46.   }
  47.   delay(1000);
  48. }
  49.  
  50. void print2digits(int number) {
  51.   if (number >= 0 && number < 10) {
  52.     Serial.write('0');
  53.   }
  54.   Serial.print(number);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement