Advertisement
nikolas77

horloge

Jun 22nd, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Date and time functions using a PCF8523 RTC connected via I2C and Wire lib
  2. #include "SPI.h"
  3. #include "Adafruit_GFX.h"
  4. #include "Adafruit_ILI9341.h"
  5.  
  6. #define TFT_DC 9
  7. #define TFT_CS 10
  8. #define TS_CS 8
  9. #define ILI9341_BLACK 0x2104
  10.  
  11. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TS_CS);
  12. #include "RTClib.h"
  13.  
  14. RTC_PCF8523 rtc;
  15.  
  16. char daysOfTheWeek[7][12] = {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
  17.  
  18. void setup () {
  19.  
  20.   tft.begin();
  21.   tft.setRotation(1);
  22.   tft.fillScreen(ILI9341_BLACK);
  23.  
  24. #ifndef ESP8266
  25.   while (!Serial); // wait for serial port to connect. Needed for native USB
  26. #endif
  27.  
  28.   if (! rtc.begin()) {
  29.     Serial.println("Couldn't find RTC");
  30.     Serial.flush();
  31.     abort();
  32.   }
  33.  
  34.   if (! rtc.initialized() || rtc.lostPower()) {
  35.     Serial.println("RTC is NOT initialized, let's set the time!");
  36.     // When time needs to be set on a new device, or after a power loss, the
  37.     // following line sets the RTC to the date & time this sketch was compiled
  38.     //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  39.     // This line sets the RTC with an explicit date & time, for example to set
  40.     // January 21, 2014 at 3am you would call:
  41.     // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  42.     //
  43.     // Note: allow 2 seconds after inserting battery or applying external power
  44.     // without battery before calling adjust(). This gives the PCF8523's
  45.     // crystal oscillator time to stabilize. If you call adjust() very quickly
  46.     // after the RTC is powered, lostPower() may still return true.
  47.   }
  48.  
  49.   // When time needs to be re-set on a previously configured device, the
  50.   // following line sets the RTC to the date & time this sketch was compiled
  51.    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  52.   // This line sets the RTC with an explicit date & time, for example to set
  53.   // January 21, 2014 at 3am you would call:
  54.   // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  55. }
  56.  
  57. void loop () {
  58.  
  59.     DateTime now = rtc.now();
  60.    
  61.     tft.fillRect (0, 0, 320, 80,ILI9341_BLACK);
  62.     tft.setCursor(50, 20);
  63.     tft.setTextColor(ILI9341_WHITE);
  64.     tft.setTextSize(5);
  65.     tft.print(now.hour(), DEC);
  66.     tft.print(':');
  67.     tft.print(now.minute(), DEC);
  68.     tft.print(':');
  69.     tft.print(now.second(), DEC);
  70.    
  71.     tft.fillRect (0, 100, 320, 70,ILI9341_BLACK);
  72.     tft.setCursor(50, 120);
  73.     tft.setTextSize(4);
  74.     tft.print(daysOfTheWeek[now.dayOfTheWeek()]);
  75.    
  76.     tft.fillRect (0, 180, 320, 70,ILI9341_BLACK);
  77.     tft.setCursor(50, 200);
  78.     tft.print(now.day(), DEC);
  79.     tft.print('/');
  80.     tft.print(now.month(), DEC);
  81.     tft.print('/');
  82.     tft.print(now.year(), DEC);
  83.    
  84.     delay(700);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement