Advertisement
Guest User

teste sketch

a guest
May 16th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1.  
  2. // CONNECTIONS:
  3. // DS3231 SDA --> SDA
  4. // DS3231 SCL --> SCL
  5. // DS3231 VCC --> 3.3v or 5v
  6. // DS3231 GND --> GND
  7.  
  8. /* for software wire use below
  9. #include <SoftwareWire.h>  // must be included here so that Arduino library object file references work
  10. #include <RtcDS3231.h>
  11.  
  12. SoftwareWire myWire(SDA, SCL);
  13. RtcDS3231<SoftwareWire> Rtc(myWire);
  14.  for software wire use above */
  15.  
  16. /* for normal hardware wire use below */
  17. #include <Wire.h> // must be included here so that Arduino library object file references work
  18. #include <RtcDS3231.h>
  19. RtcDS3231<TwoWire> Rtc(Wire);
  20. /* for normal hardware wire use above */
  21. long n;
  22.  
  23. #define countof(a) (sizeof(a) / sizeof(a[0]))
  24.  
  25. void printDateTime(const RtcDateTime& dt)
  26. {
  27.     char datestring[20];
  28.  
  29.     snprintf_P(datestring,
  30.             countof(datestring),
  31.             PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
  32.             dt.Month(),
  33.             dt.Day(),
  34.             dt.Year(),
  35.             dt.Hour(),
  36.             dt.Minute(),
  37.             dt.Second() );
  38.     Serial.print(datestring);
  39. }
  40.  
  41. void resetAlarm(RtcDateTime time) {
  42.   RtcDateTime alarm =(time + 30);
  43.  
  44.   DS3231AlarmOne alarm1(
  45.       alarm.Day(),
  46.       alarm.Hour(),
  47.       alarm.Minute(),
  48.       alarm.Second(),
  49.       DS3231AlarmOneControl_MinutesSecondsMatch);
  50.   Serial.println("RTC to trigger at : ");
  51.   printDateTime(alarm);
  52.   Rtc.SetAlarmOne(alarm1);
  53.   delay(500);
  54.   Rtc.LatchAlarmsTriggeredFlags();
  55.   Serial.println("----");
  56. }
  57.  
  58. void setup ()
  59. {
  60.     Serial.begin(9600);
  61.  
  62.     Serial.print("compiled: ");
  63.     Serial.print(__DATE__);
  64.     Serial.println(__TIME__);
  65.  
  66.     //--------RTC SETUP ------------
  67.     // if you are using ESP-01 then uncomment the line below to reset the pins to
  68.     // the available pins for SDA, SCL
  69.     // Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
  70.    
  71.     Rtc.Begin();
  72.  
  73.     RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
  74.     printDateTime(compiled);
  75.     Serial.println();
  76.  
  77.     if (!Rtc.IsDateTimeValid())
  78.     {
  79.         // Common Cuases:
  80.         //    1) first time you ran and the device wasn't running yet
  81.         //    2) the battery on the device is low or even missing
  82.  
  83.         Serial.println("RTC lost confidence in the DateTime!");
  84.  
  85.         // following line sets the RTC to the date & time this sketch was compiled
  86.         // it will also reset the valid flag internally unless the Rtc device is
  87.         // having an issue
  88.  
  89.         Rtc.SetDateTime(compiled);
  90.     }
  91.  
  92.     if (!Rtc.GetIsRunning())
  93.     {
  94.         Serial.println("RTC was not actively running, starting now");
  95.         Rtc.SetIsRunning(true);
  96.     }
  97.  
  98.     RtcDateTime now = Rtc.GetDateTime();
  99.     if (now < compiled)
  100.     {
  101.         Serial.println("RTC is older than compile time!  (Updating DateTime)");
  102.         Rtc.SetDateTime(compiled);
  103.     }
  104.     else if (now > compiled)
  105.     {
  106.         Serial.println("RTC is newer than compile time. (this is expected)");
  107.     }
  108.     else if (now == compiled)
  109.     {
  110.         Serial.println("RTC is the same as compile time! (not expected but all is fine)");
  111.     }
  112.  
  113.     // never assume the Rtc was last configured by you, so
  114.     // just clear them to your needed state
  115.     Rtc.Enable32kHzPin(false);
  116.     Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeAlarmOne);
  117.  
  118.     n = millis();
  119. }
  120.  
  121. int count = 0;
  122. void loop ()
  123. {
  124.  
  125.     if (count >= 10) {
  126.       resetAlarm(Rtc.GetDateTime());
  127.     }
  128.  
  129.     if (!Rtc.IsDateTimeValid())
  130.     {
  131.         // Common Cuases:
  132.         //    1) the battery on the device is low or even missing and the power line was disconnected
  133.         Serial.println("RTC lost confidence in the DateTime!");
  134.     }
  135.  
  136.     RtcDateTime now = Rtc.GetDateTime();
  137.     printDateTime(now);
  138.     Serial.println();
  139.  
  140.     RtcTemperature temp = Rtc.GetTemperature();
  141.     Serial.print(temp.AsFloatDegC());
  142.     Serial.println("C");
  143.  
  144.     delay(1000); // ten seconds
  145.  
  146.  
  147.  
  148.     count++;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement