Guest User

Untitled

a guest
Nov 17th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. // Date and time functions using a DS3231 RTC connected via I2C and Wire lib
  2. #include <Wire.h>
  3. #include "RTClib.h"
  4.  
  5. RTC_DS3231 rtc;
  6.  
  7. char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  8.  
  9.  
  10. #include <SPI.h>
  11. #include <Adafruit_GFX.h>
  12. #include <Adafruit_ILI9340.h>
  13.  
  14. #if defined(__SAM3X8E__)
  15.     #undef __FlashStringHelper::F(string_literal)
  16.     #define F(string_literal) string_literal
  17. #endif
  18.  
  19. #define _sclk 14
  20. #define _miso 12
  21. #define _mosi 13
  22. #define _cs 15
  23. #define _dc 2
  24. #define _rst 4
  25.  
  26. Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _rst);
  27.  
  28. void setup () {
  29.    
  30.  
  31. #ifndef ESP8266
  32.   while (!Serial); // for Leonardo/Micro/Zero
  33. #endif
  34.  
  35.   Serial.begin(9600);
  36.  
  37.   delay(3000); // wait for console opening
  38.  
  39.   if (! rtc.begin()) {
  40.     Serial.println("Couldn't find RTC");
  41.     while (1);
  42.   }
  43.  
  44.   if (rtc.lostPower()) {
  45.     Serial.println("RTC lost power, lets set the time!");
  46.     // following line sets the RTC to the date & time this sketch was compiled
  47.     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  48.     // This line sets the RTC with an explicit date & time, for example to set
  49.     // January 21, 2014 at 3am you would call:
  50.     // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  51.   }
  52.  
  53. //HIER GAAT HET FOUT.
  54.  
  55.  //tft.begin();
  56.  //tft.fillScreen(ILI9340_BLACK);
  57. }
  58.  
  59. void loop () {
  60.     DateTime now = rtc.now();
  61.    
  62.     Serial.print(now.year(), DEC);
  63.     Serial.print('/');
  64.     Serial.print(now.month(), DEC);
  65.     Serial.print('/');
  66.     Serial.print(now.day(), DEC);
  67.     Serial.print(" (");
  68.     Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  69.     Serial.print(") ");
  70.     Serial.print(now.hour(), DEC);
  71.     Serial.print(':');
  72.     Serial.print(now.minute(), DEC);
  73.     Serial.print(':');
  74.     Serial.print(now.second(), DEC);
  75.     Serial.println();
  76.    
  77.     Serial.print(" since midnight 1/1/1970 = ");
  78.     Serial.print(now.unixtime());
  79.     Serial.print("s = ");
  80.     Serial.print(now.unixtime() / 86400L);
  81.     Serial.println("d");
  82.    
  83.     // calculate a date which is 7 days and 30 seconds into the future
  84.     DateTime future (now + TimeSpan(7,12,30,6));
  85.    
  86.     Serial.print(" now + 7d + 30s: ");
  87.     Serial.print(future.year(), DEC);
  88.     Serial.print('/');
  89.     Serial.print(future.month(), DEC);
  90.     Serial.print('/');
  91.     Serial.print(future.day(), DEC);
  92.     Serial.print(' ');
  93.     Serial.print(future.hour(), DEC);
  94.     Serial.print(':');
  95.     Serial.print(future.minute(), DEC);
  96.     Serial.print(':');
  97.     Serial.print(future.second(), DEC);
  98.     Serial.println();
  99.    
  100.     Serial.println();
  101.  
  102.  
  103.     delay(3000);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment