// import used libraries
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
// initialize rtc module
RTC_DS3231 rtc;
// initialize tft screen
#define TFT_DC 2
#define TFT_CS 5
#define TFT_RST 4
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// define a list of 7 elements each up to 12 characters
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
//define the function for displaying the date
void displaydate() {
tft.setTextColor(ILI9341_WHITE);
tft.println("displaying today's date");
String date = "YYYY-MM-DD";
tft.setTextColor(ILI9341_GREEN);
tft.println(date);
}
// setup function
void setup () {
Serial.begin(9600);
delay(3000); // wait for console opening
// if the rtc module cannot be found it will display following error message
// in the IDE's serial
if (! rtc.begin()) {
Serial.println("Couldn't find RTC! MAKE SURE THE CIRCUIT IS CORRECT");
// after displaying the error message it gets stuck in an infinite loop
while (1);
}
// if the rtc loses power, it sets the time anew
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// startup of tft screen and parameter definition
tft.begin();
tft.setRotation(1); //makes the text oriented as it is in the simulation here
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(1.5);
}
void loop () {
DateTime now = rtc.now();
tft.print(now.year(), DEC);
tft.print('/');
tft.print(now.month(), DEC);
tft.print('/');
tft.print(now.day(), DEC);
tft.print(" (");
tft.print(daysOfTheWeek[now.dayOfTheWeek()]);
tft.print(") ");
tft.print(now.hour(), DEC);
tft.print(':');
tft.print(now.minute(), DEC);
tft.print(':');
tft.print(now.second(), DEC);
//tft.println();
delay(3000);
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
}