//https://sdevelectronics.blogspot.com/2020/04/tft-gui-design.html
//https://roboticadiy.com/arduino-12-hour-format-clock-with-tft-display/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <Wire.h>
#include "Sodaq_DS3231.h"
#include <SPI.h>
#include <MCUFRIEND_kbv.h>
//MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8
#define TFT_MISO 12
#define TFT_MOSI 11
#define TFT_CLK 13
/*TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF*/
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
char timeChar[100];
char dateChar[50];
uint32_t old_ts;
String dateString;
int minuteNow=0;
int minutePrevious=0;
int irPin1=7;
int count1=0;
int irPin2=6;
int count2=0;
boolean state = true;
void setup()
{
Serial.begin(9600);
tft.begin();
tft.setRotation(0);
//tft.fillScreen(ILI9341_BLACK);
Wire.begin();
rtc.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_WHITE);
//tft.fillRect(250 , 75 , 100, 100 ,ILI9341_BLUE );
tft.drawLine(195,30,195,360,ILI9341_RED );
tft.fillRect(5,27, 100,20 ,ILI9341_BLUE );
printText("Size 5.5: ", ILI9341_RED,8,30,2);
printText("Size 6.0: ", ILI9341_RED,8,55,2);
/*printText("Size 6.5: ", ILI9341_RED,8,80,2);
printText("Size 7.0: ", ILI9341_RED,8,105,2);
printText("Size 7.5: ", ILI9341_RED,8,130,2);
printText("Size 8.0: ", ILI9341_RED,8,155,2);
printText("Size 8.5: ", ILI9341_RED,8,180,2);
printText("Size 9.0: ", ILI9341_RED,8,205,2);*/
}
void loop()
{
DateTime now = rtc.now(); //get the current date-time
uint32_t ts = now.getEpoch();
if (old_ts == 0 || old_ts != ts) {
old_ts = ts;
minuteNow = now.minute();
if(minuteNow!=minutePrevious)
{
dateString = getDayOfWeek(now.dayOfWeek())+", ";
dateString = dateString+String(now.date())+"/"+String(now.month());
dateString= dateString+"/"+ String(now.year());
minutePrevious = minuteNow;
String hours = String(now.hour());
if(now.minute()<10)
{
hours = hours+":0"+String(now.minute());
}else
{
hours = hours+":"+String(now.minute());
}
hours.toCharArray(timeChar,100);
tft.fillRect(250,0,100,25,ILI9341_BLACK);
printText(timeChar, ILI9341_YELLOW,255,5,2);
dateString.toCharArray(dateChar,50);
printText(dateChar, ILI9341_GREEN,8,5,2);
}
}
delay(10);
if
(!digitalRead(irPin1) && state){
count1++;
state = false;
tft.fillRect(120,30,60,17,ILI9341_BLACK );
//tft.fillRect(120,30,60,17,ILI9341_WHITE );
tft.setCursor(120,30);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.print(count1);
tft.fillRect(250,30,100,20,ILI9341_BLACK);
printText(timeChar, ILI9341_RED,255,30,2);
delay(10);
}
if (digitalRead(irPin1))
{
state = true;
delay(10);
}
}
void setRTCTime()
{
DateTime dt(2018, 4, 2, 13, 13, 30, 1); // Year, Month, Day, Hour, Minutes, Seconds, Day of Week
rtc.setDateTime(dt); //Adjust date-time as defined 'dt' above
}
void printText(char *text, uint16_t color, int x, int y,int textSize)
{
tft.setCursor(x, y);
tft.setTextColor(color);
tft.setTextSize(textSize);
tft.setTextWrap(true);
tft.print(text);
}
String getDayOfWeek(int i)
{
switch(i)
{
case 1: return "Monday";break;
case 2: return "Tuesday";break;
case 3: return "Wednesday";break;
case 4: return "Thursday";break;
case 5: return "Friday";break;
case 6: return "Saturday";break;
case 7: return "Sunday";break;
default: return "Monday";break;
}
}
/*#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <DS1302.h>
// Init the DS1302
DS1302 rtc(24, 26, 28);
// Init a Time-data structure
Time t;
int activate;
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define GREY 0x8410
void setup()
{
Serial.begin(9600);
uint16_t ID = tft.readID();
Serial.print("TFT ID = 0x");
Serial.println(ID, HEX);
if (ID == 0xD3D3) ID = 0x9486; // write-only shield
tft.begin(ID);
tft.setRotation(1);
tft.fillScreen(BLACK);
// Set the clock to run-mode, and disable the write protection
rtc.halt(false);
rtc.writeProtect(false);
// The following lines can be commented out to use the values already stored in the DS1302
rtc.setDOW(WEDNESDAY); // Set Day-of-Week to TUESDAY
rtc.setTime(20, 58, 50); // Set the time to 24hr format it will automatically take time according to AM/PM
rtc.setDate(06, 05, 2020); // Set the date to May 7th, 2018
}
void loop()
{
// Get data from the DS1302
t = rtc.getTime();
tft.fillRect(170, 50, 50, 50, BLACK);
// update
if (t.sec<=1){
tft.fillScreen(BLACK);
}
// Day
tft.setCursor(50,180);
tft.setTextSize(4);
tft.setTextColor(CYAN);
tft.print(rtc.getDOWStr());
// Heading
tft.setCursor(50,10);
tft.setTextColor(MAGENTA);
tft.setTextSize(3);
tft.print("Digital Clock");
//time
tft.setCursor(30,60);
tft.setTextSize(4);
tft.setTextColor(GREEN);
if (t.hour>=12){
activate = 1;
t.hour = t.hour-12;
if (t.hour== 0) {
t.hour = 12; // Day 12 PM
}
if (t.hour<=9){
tft.print("0");
tft.print(t.hour);
tft.print(":");
}
else {
tft.print(t.hour);
tft.print(":");}
}
else {
activate = 0;
if(t.hour==0){
t.hour=12; // Night 12 AM
}
if (t.hour<=9){
tft.print("0");
tft.print(t.hour);
tft.print(":");
}
else {
tft.print(t.hour);
tft.print(":");}
}
if (t.min<=9){
tft.print("0");
tft.print(t.min);
tft.print(":");
}
else {
tft.print(t.min);
tft.print(":");}
if (t.sec<=9){
tft.print("0");
tft.print(t.sec);
tft.print(" ");
}
else {
tft.print(t.sec);
tft.print(" ");
}
//AM/PM
if (activate==1){
tft.print("PM");
}else{
tft.print("AM"); }
//Date
tft.setCursor(30,120);
tft.setTextColor(YELLOW);
if (t.date<=9){
tft.print("0");
tft.print(t.date);
tft.print(" ");
}
else {
tft.print(t.date);
tft.print(" ");}
switch (t.mon)
{
case 1:
tft.print("JAN");
break;
case 2:
tft.print("FEB");
break;
case 3:
tft.print("MAR");
break;
case 4:
tft.print("APR");
break;
case 5:
tft.print("MAY");
break;
case 6:
tft.print("JUN");
break;
case 7:
tft.print("JUL");
break;
case 8:
tft.print("AUG");
break;
case 9:
tft.print("SEP");
break;
case 10:
tft.print("OCT");
break;
case 11:
tft.print("NOV");
break;
case 12:
tft.print("DEC");
break;
}
tft.setCursor(200,120);
tft.print(t.year);
delay(1000);
}