/*
Red Button (4) not used in this case!
TFT Display:
Arduino MEga:
SCK = D13
MISO = D12
MOSI = D11
https://i.stack.imgur.com/RxKaD.png
*/
/*
https://wokwi.com/projects/344892587898831442 DHT Sensor
https://iotdk.intel.com/docs/master/upm/classupm_1_1_i_l_i9341.html
https://www.youtube.com/watch?v=beyDkTBhpgs
Making Alarm Time Changer
print Date l: 145
*/
#include <RTClib.h>
#include <U8g2lib.h>
#include <Bounce2.h>
#include <DHT.h>
#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_GFX.h>
byte button_pins[] = {23, 22, 24, 25}; // button pins, 23,22 = up/down, 24 = select, 25 = Home
#define NUMBUTTONS sizeof(button_pins)
Bounce * buttons = new Bounce[NUMBUTTONS]; // Button Bounce
#define DHTPIN 2 // Pin Connected to the DHT sensor
#define DHTTYPE DHT22
#define TFT_DC 49 // TFT Display
#define TFT_CS 53
U8X8_SSD1306_128X64_NONAME_HW_I2C display(U8X8_PIN_NONE); // Initialize OLED Display
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); // Initialize TFT Display
RTC_DS1307 rtc; // Initialize RTC Module
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT Sensor (DHT22 beeing the type of Sensor)
#define MENU_SIZE_1 5 // Menu 1
char *menu1[MENU_SIZE_1] = { "Home", "Change Time", "Option 3", "Option 4", "Option 5" };
#define MENU_SIZE_HOME 2 // Home
char *home[MENU_SIZE_HOME] = { "Menu 1", "Option 2"};
// Days and Months
char *monthsoftheyear[] = { "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"}; // Array of Months in the year
char *daysOfTheWeek[] = {"Sonntag", "Montag", "Dienstag", "Mittowch", "Donnerstag", "Freitag", "Samstag"}; // Array of days in the week
char *diffrentTimesofDay[] = {"Guten Morgen", "Guten Mittag", "Guten Nachmittag", "Guten Abend"}; // Array of Greetings // maby add , at the end
int currentPartOfTimeDurringDay;
String OPEN_MENU = "Home";
int changeAlarmTimeStep = 0;
int cursor = 0;
int TARGET_TIME[] = {9, 48, 0}; // Alarm Time
int targethour = TARGET_TIME[0];
int targetminute = TARGET_TIME[1];
int targetsecond = TARGET_TIME[2];
const int LOCKSCREEN_DELAY = 5; // Delay until the lockscreen comes back in in Minutes after last Button press
// Variables
int hour;
int minute;
int second;
int year;
int month;
int day;
int dayofweek;
int lastsecond;
int lastminute;
int lasthour;
int lastday;
int lastmonth;
int lastyear;
int lastsecondtwo = -1;
int CurrentTimecodecooldown = 0;
int CurrentTimecode = 0;
float humidity;
float temperature;
void setup() {
Serial.begin(9600); // Start Serial Monitor
for (int i = 0; i < NUMBUTTONS; i++) {
buttons[i].attach( button_pins[i], INPUT_PULLUP); // setup the bounce instance for the current button
buttons[i].interval(25); // interval in ms
}
if (!display.begin()) { // Start Display
Serial.println("Couldn't connect to Display");
Serial.flush();
abort(); // Stop the Program
} else {
display.setPowerSave(0);
display.setFont(u8x8_font_pxplusibmcgathin_f);
}
if (!rtc.begin()) { // Start Conection to RTC
Serial.println("Couldn't connect to RTC");
Serial.flush();
abort(); // Stop the Program
}
dht.begin();
tft.begin();
tft.setTextSize(2);
showLockScreen();
}
void loop() {
DateTime now = rtc.now(); // Setting up RTC
year = now.year();
month = now.month();
day = now.day();
hour = now.hour();
minute = now.minute();
second = now.second();
dayofweek = now.dayOfTheWeek();
dht_sensor();
CurrentTimecode = hour * 60 + minute;
if (lastday != day) {
if (OPEN_MENU == "Lockscreen") {
tft.setCursor(25, 175);
tft.print(daysOfTheWeek[dayofweek]); // print Day
tft.setCursor(25, 200);
tft.print(monthsoftheyear[month - 1]); // print month
// print date like hours
if (day < 10) {
tft.setCursor(25, 225);
tft.print('0');
tft.setCursor(35, 225);
tft.print(day);
} else {
tft.setCursor(25, 225);
tft.print(day);
}
if (month < 10){
tft.setCursor(55, 225);
tft.print('0');
tft.setCursor(65, 225);
tft.print(month);
}else{
tft.setCursor(55, 225);
tft.print(month);
}
tft.setCursor(85, 225);
tft.print(year);
tft.setCursor(47, 225);
tft.print(':');
tft.setCursor(77, 225);
tft.print(':');
}
lastday = day;
}
if (lasthour != hour) {
// Update time sector
if (CurrentTimecode > 0 && CurrentTimecode < 720) {
currentPartOfTimeDurringDay = 0;
} else if (CurrentTimecode >= 720 && CurrentTimecode < 809) {
currentPartOfTimeDurringDay = 1;
} else if (CurrentTimecode >= 810 && CurrentTimecode <= 1079) {
currentPartOfTimeDurringDay = 2;
} else { // if (CurrentTimecode > 1079)
currentPartOfTimeDurringDay = 3;
}
if (OPEN_MENU == "Lockscreen") {
tft.fillRect(25, 75, 22, 15, ILI9341_ORANGE);
if (hour < 10) {
tft.setCursor(25, 75);
tft.print('0');
tft.setCursor(37, 75);
tft.print(hour);
} else {
tft.setCursor(25, 75);
tft.print(hour);
}
tft.setCursor(10, 10);
tft.setTextColor(ILI9341_BLUE);
tft.setTextSize(3);
tft.print(diffrentTimesofDay[currentPartOfTimeDurringDay]);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(25, 175);
tft.print(daysOfTheWeek[dayofweek]);
tft.setCursor(25, 200);
tft.print(monthsoftheyear[month - 1]);
tft.setCursor(45, 75);
tft.print(':');
tft.setCursor(75, 75);
tft.print(':');
}
lasthour = hour;
}
if (lastminute != minute) {
if (OPEN_MENU == "Lockscreen") {
tft.fillRect(55, 75, 22, 15, ILI9341_ORANGE);
if (minute < 10) {
tft.setCursor(55, 75);
tft.print('0');
tft.setCursor(67, 75);
tft.print(minute);
} else {
tft.setCursor(55, 75);
tft.print(minute);
}
}
lastminute = minute;
}
if (lastsecond != second) {
if (OPEN_MENU == "Lockscreen") {
tft.fillRect(85, 75, 22, 15, ILI9341_ORANGE);
if (second < 10) {
tft.setCursor(85, 75);
tft.print('0');
tft.setCursor(97, 75);
tft.print(second);
} else {
tft.setCursor(85, 75);
tft.print(second);
}
tft.setCursor(25, 100);
tft.print("Hum:");
tft.setCursor(25, 125);
tft.print("Temp:");
tft.fillRect(125, 100, 58, 15, ILI9341_ORANGE);
tft.setCursor(125, 100);
tft.print(humidity);
tft.print('%');
tft.fillRect(125, 125, 58, 15, ILI9341_ORANGE);
tft.setCursor(125, 125);
tft.print(temperature);
tft.print('C');
}
lastsecond = second;
}
if (targethour == hour && targetminute == minute && targetsecond == second) {
Alarm();
}
for (int i = 0; i < NUMBUTTONS; i++) {
buttons[i].update(); // Update the Bounce instance
if ( buttons[i].fell() ) { // If it fell
if (OPEN_MENU == "Lockscreen") {
showHome(); // show Home if last window was Lockscreen
CurrentTimecodecooldown = CurrentTimecode + LOCKSCREEN_DELAY; // Update Delay
} else { // Do not execute the buttons when the menu is not showen
CurrentTimecodecooldown = CurrentTimecode + LOCKSCREEN_DELAY; // Update Delay
if (i == 3) {
if (OPEN_MENU != "Home") {
showHome(); // show Home if it is not already open
}
}
else if (i == 2) {
// goto select
button_select();
} else {
if (OPEN_MENU != "ChangeAlarmTime") {
// erase previous cursor:
display.setCursor(0, cursor);
display.print(' ');
}
if (i == 0) { // up
button_up();
}
else { // down
button_down();
}
if (OPEN_MENU != "ChangeAlarmTime") {
// show cursor at new line:
display.setCursor(0, cursor);
display.print('>');
}
}
}
} else if (CurrentTimecodecooldown <= CurrentTimecode && OPEN_MENU != "Lockscreen") {
showLockScreen(); // Show Lockscreen
}
}
}
void button_select() {
if ( OPEN_MENU == "Home") {
//execute choise
if (cursor == 0) {
showMenu1();
}
//print selection
display.clearLine(7);
display.setCursor(0, 7);
display.print(">>");
display.print(home[cursor]);
} else if (OPEN_MENU == "Menu_1") {
// execute choice
if (cursor == 0) {
showHome();
} else if (cursor == 1) {
ChangeAlarmTime();
}
display.clearLine(7);
display.setCursor(0, 7);
display.print(">>");
display.print(menu1[cursor]);
} else if (OPEN_MENU == "ChangeAlarmTime") {
changeAlarmTimeStep++;
if (changeAlarmTimeStep == 3) {
changeAlarmTimeStep = 0;
showMenu1();
cursor = 0;
}
}
}
void button_up() {
if (OPEN_MENU == "Home") {
cursor++;
if (cursor > (MENU_SIZE_HOME - 1)) cursor = 0;
} else if (OPEN_MENU == "Menu_1") {
cursor++;
if (cursor > (MENU_SIZE_1 - 1)) cursor = 0;
} else if (OPEN_MENU == "ChangeAlarmTime") {
if (changeAlarmTimeStep == 0) {
targethour++;
if (targethour == 25) {
targethour = 0;
}
} else if (changeAlarmTimeStep == 1) {
targetminute++;
if (targetminute == 60) {
targetminute = 0;
}
} else if (changeAlarmTimeStep == 2) {
targetsecond++;
if (targetsecond == 60) {
targetsecond = 0;
}
}
ChangeAlarmTime();
}
}
void button_down() {
if (OPEN_MENU == "Home") {
cursor--;
if (cursor < 0) cursor = (MENU_SIZE_HOME - 1);
} else if (OPEN_MENU == "Menu_1") {
cursor--;
if (cursor < 0) cursor = (MENU_SIZE_1 - 1);
} else if (OPEN_MENU == "ChangeAlarmTime") {
if (changeAlarmTimeStep == 0) {
targethour--;
if (targethour == 0) {
targethour = 24;
}
} else if (changeAlarmTimeStep == 1) {
targetminute--;
if (targetminute == 0) {
targetminute = 60;
}
} else if (changeAlarmTimeStep == 2) {
targetsecond--;
if (targetsecond == 0) {
targetsecond = 60;
}
}
ChangeAlarmTime();
}
}
void dht_sensor() {
if (lastsecondtwo = second - 2) {
temperature = dht.readTemperature();
humidity = dht.readHumidity();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
lastsecondtwo = second;
}
}
// Menus (OLED - Display)
void showMenu1() {
display.clearDisplay();
Serial.println("Opening Menu1");
OPEN_MENU = "Menu_1";
// show menu items:
for (int i = 0; i < MENU_SIZE_1; i++) {
display.drawString(2, i, menu1[i]);
}
display.setCursor(0, 0);
display.print('>');
}
void showHome() {
cursor = 0;
display.clearDisplay();
Serial.println("Opening Home");
OPEN_MENU = "Home";
// show menu items:
for (int i = 0; i < MENU_SIZE_HOME; i++) {
display.drawString(2, i, home[i]);
}
display.setCursor(0, 0);
display.print('>');
}
void showLockScreen() {
lasthour = -1;
lastminute = -1;
lastsecond = -1;
display.clearDisplay();
Serial.println("Opening Lockscreen");
OPEN_MENU = "Lockscreen";
cursor = 0;
display.setCursor(2, 2);
display.print("Hey,");
display.setCursor(2, 3);
display.print("How are You?");
}
void Alarm() {
Serial.print("Alarm");
display.clearDisplay();
OPEN_MENU = "Alarm";
}
void ChangeAlarmTime() {
Serial.println("Change Alarm Time");
OPEN_MENU = "ChangeAlarmTime";
display.clearDisplay();
if (targethour < 10) {// Alarm Time
display.setCursor(2, 2);
display.print('0');
display.setCursor(3, 2);
display.print(targethour);
} else {
display.setCursor(2, 2);
display.print(targethour);
}
display.setCursor(4, 2);
display.print(':');
if (targetminute < 10) {
display.setCursor(5, 2);
display.print('0');
display.setCursor(6, 2);
display.print(targetminute);
} else {
display.setCursor(5, 2);
display.print(targetminute);
}
display.setCursor(7, 2);
display.print(':');
if (targetsecond < 10) {
display.setCursor(8, 2);
display.print('0');
display.setCursor(9, 2);
display.print(targetsecond);
} else {
display.setCursor(8, 2);
display.print(targetsecond);
}
}