// Declares the libraries to be included
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include <Arduino.h>
#include <Adafruit_FT6206.h>
#include <RTClib.h>
// Setup
const int delayValue = 25, refreshClockDivider = 1000,
barWidth = 45, barSpace = 10, interBar = barWidth + barSpace, barPosX = 15, barPosY = 100;
const int ledW = 10, ledB = 3, ledG = 5, ledR = 6; // Sets the pins to be used by the RGB LEDs
const float ntcBeta = 3950; // Sets the NTC Beta coefficient
// Declares supplemental colors to be used. Colors: binary RRRRR BBBBBB GGGGG to Decimal
const int DARKBLUE = 12, DARKGREEN = 768, DARKRED = 24576, LIGHTGRAY = 50712, DARKGRAY = 18985;
// Declares and initializes the operating variables
int valueW = 0, valueR = 0, valueG = 0, valueB = 0,
refreshClockCounter = 0, returnValue = 0,
alarmHour = 12, alarmMinute = 0;
boolean alarm = false;
// Defines the peripherals
#define TFT_CS 7
#define TFT_DC 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Adafruit_FT6206 ctp = Adafruit_FT6206();
RTC_DS1307 rtc;
void setup() {
// Initializes the LED PWM and RTC pins
pinMode(ledW, OUTPUT);
pinMode(ledR, OUTPUT);
pinMode(ledG, OUTPUT);
pinMode(ledB, OUTPUT);
pinMode(A0, INPUT);
// Initializes the LCD, touchscreen controller and RTC
tft.begin();
ctp.begin(40);
rtc.begin();
// Initializes the serial console
// Serial.begin(9600);
// Serial.println("Serial console started");
}
TS_Point readTouch(){
// Retrieves the touch point
TS_Point touchPoint = ctp.getPoint();
// Flips the coordinates to match the screen.
touchPoint.x = map(touchPoint.x, 0, 240, 240, 0);
touchPoint.y = map(touchPoint.y, 0, 320, 320, 0);
return touchPoint;
}
void refreshClock(){
DateTime now = rtc.now();
int analogValue = analogRead(A0);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / ntcBeta + 1.0 / 298.15) - 273.14;
int tempValue = celsius + 1;
tft.fillRect(0, 0, 240, 25, DARKGRAY);
tft.setCursor(10, 5);
tft.setTextSize(2);
if (now.hour() < 10) tft.print("0");
tft.print(now.hour());
tft.print(":");
if (now.minute() < 10) tft.print("0");
tft.print(now.minute());
tft.print(" ");
if (tempValue < 10) tft.print("0");
tft.print(tempValue);
tft.print("oC ");
if (alarm) tft.print("A");
if (refreshClockCounter == 1000) refreshClockCounter = 0; // To prevent overflow
}
int mainMenu(){
bool loopVar = true;
tft.setTextColor(LIGHTGRAY);
tft.setCursor(15, 35);
tft.setTextSize(3);
tft.print("Main Menu");
tft.setTextColor(ILI9341_WHITE);
tft.fillRect(15, 80, 210, 30, DARKGRAY);
tft.fillRect(15, 120, 210, 30, DARKGRAY);
tft.fillRect(15, 160, 210, 30, DARKGRAY);
tft.fillRect(15, 200, 210, 30, DARKGRAY);
tft.setCursor(20, 85);
tft.print("LED Control");
tft.setCursor(20, 125);
tft.print("Alarm Clock");
tft.setCursor(20, 165);
tft.print("Air Con");
tft.setCursor(20, 205);
tft.print("Scene Ctrl");
while(loopVar){
if ((refreshClockCounter % refreshClockDivider) == 0) refreshClock();
else refreshClockCounter++;
TS_Point touchPoint = readTouch();
// If touched
if (ctp.touched()){
if (touchPoint.x > 15 && touchPoint.x < 225){
if (touchPoint.y > 80 && touchPoint.y < 110) return 1;
if (touchPoint.y > 120 && touchPoint.y < 150) return 2;
}
}
delay(delayValue);
}
}
int alarmClock(){
bool loopVar = true;
tft.setTextColor(LIGHTGRAY);
tft.setCursor(15, 35);
tft.setTextSize(3);
tft.print("Alarm Clock");
tft.setCursor(15, 70);
if(alarm){
tft.setTextColor(ILI9341_GREEN);
tft.print("On ");
}
else {
tft.setTextColor(ILI9341_RED);
tft.print("Off ");
}
tft.setTextColor(ILI9341_WHITE);
if(alarmHour < 10) tft.print("0");
tft.print(alarmHour);
tft.print(":");
if(alarmMinute < 10) tft.print("0");
tft.print(alarmMinute);
tft.setCursor(20, 120);
tft.print("On");
tft.setCursor(20, 200);
tft.print("Off");
tft.setCursor(95, 120);
tft.print("H+");
tft.setCursor(95, 200);
tft.print("H-");
tft.setCursor(170, 120);
tft.print("M+");
tft.setCursor(170, 200);
tft.print("M-");
tft.drawRect(15, 115, 60, 60, DARKGRAY);
tft.drawRect(90, 115, 60, 60, DARKGRAY);
tft.drawRect(165, 115, 60, 60, DARKGRAY);
tft.drawRect(15, 195, 60, 60, DARKGRAY);
tft.drawRect(90, 195, 60, 60, DARKGRAY);
tft.drawRect(165, 195, 60, 60, DARKGRAY);
tft.fillRect(0, 295, 240, 25, DARKGRAY);
tft.setTextSize(2);
tft.setCursor(10, 300);
tft.print("< Back");
while(loopVar){
if ((refreshClockCounter % refreshClockDivider) == 0) refreshClock();
else refreshClockCounter++;
TS_Point touchPoint = readTouch();
// If touched
if (ctp.touched()){
if (touchPoint.y > 295){
if (touchPoint.x < 120 && touchPoint.x > 10)
loopVar = false;
}
}
delay(delayValue);
}
return 0;
}
int ledControl(){
bool loopVar = true;
tft.setTextColor(LIGHTGRAY);
tft.setCursor(15, 35);
tft.setTextSize(3);
tft.print("RGB Lighting");
tft.setTextColor(ILI9341_WHITE);
tft.fillRect(0, 295, 240, 25, DARKGRAY);
tft.setTextSize(2);
tft.setCursor(10, 300);
tft.print("< Back");
tft.drawRect(barPosX, barPosY - 1, barWidth, 129, ILI9341_RED);
tft.drawRect(barPosX + interBar, barPosY - 1, barWidth, 129, ILI9341_GREEN);
tft.drawRect(barPosX + (interBar * 2), barPosY - 1, barWidth, 129, ILI9341_BLUE);
tft.drawRect(barPosX + (interBar * 3), barPosY - 1, barWidth, 129, ILI9341_WHITE);
int barInitX = barPosX + barWidth;
while(loopVar){
if ((refreshClockCounter % refreshClockDivider) == 0) refreshClock();
else refreshClockCounter++;
TS_Point touchPoint = readTouch();
// If touched
if (ctp.touched()){
int barVal = (127 - (touchPoint.y - barPosY));
if (touchPoint.y < (barPosY + 128) && touchPoint.y > (barPosY - 1)){
if (touchPoint.x < barPosX + barWidth && touchPoint.x > barPosX ) valueR = barVal;
else if (touchPoint.x < barInitX + interBar && touchPoint.x > barPosX + interBar ) valueG = barVal;
else if (touchPoint.x < barInitX + (interBar * 2) && touchPoint.x > barPosX + (interBar * 2)) valueB = barVal;
else if (touchPoint.x < barInitX + (interBar * 3) && touchPoint.x > barPosX + (interBar * 3)) valueW = barVal;
setLeds();
}
if (touchPoint.y > 295){
if (touchPoint.x < 120 && touchPoint.x > 10)
loopVar = false;
}
}
// Delays a little bit
delay(delayValue);
}
return 0;
}
void setLeds(){
tft.fillRect(1 + barPosX, barPosY, barWidth - 2, 127, ILI9341_BLACK);
tft.fillRect(1 + barPosX + interBar, barPosY, barWidth - 2, 127, ILI9341_BLACK);
tft.fillRect(1 + barPosX + (interBar * 2), barPosY, barWidth - 2, 127, ILI9341_BLACK);
tft.fillRect(1 + barPosX + (interBar * 3), barPosY, barWidth - 2, 127, ILI9341_BLACK);
tft.fillRect(1 + barPosX, (barPosY + 127) - valueR, barWidth - 2, valueR, DARKRED);
tft.fillRect(1 + barPosX + interBar, (barPosY + 127) - valueG, barWidth - 2, valueG, DARKGREEN);
tft.fillRect(1 + barPosX + (interBar * 2), (barPosY + 127) - valueB, barWidth - 2, valueB, DARKBLUE);
tft.fillRect(1 + barPosX + (interBar * 3), (barPosY + 127) - valueW, barWidth - 2, valueW, LIGHTGRAY);
analogWrite(ledW, valueW * 2); // Disabled due to emulation issues
analogWrite(ledR, valueR * 2);
analogWrite(ledG, valueG * 2);
analogWrite(ledB, valueB * 2);
}
void loop() {
// Blanks the display
tft.fillScreen(ILI9341_BLACK);
if (returnValue == 0) returnValue = mainMenu();
else if (returnValue == 1) returnValue = ledControl();
else if (returnValue == 2) returnValue = alarmClock();
}