//load required libraries
#include "Adafruit_GFX.h"
#include "Adafruit_TFTLCD.h"
#include "TouchScreen.h"
#include <OneWire.h>
#include <DallasTemperature.h>
//set touchscreen input pins
#define YP A3
#define XW A2
#define YW 9
#define XP 8
#define TS_MINX 100
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
TouchScreen ts = TouchScreen(XP, YP, XW, YW, 300);
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
#define MINPRESSURE 10
#define MAXPRESSURE 1000
//Set up TFT colours
#define BLACK 0x0000
#define BLUE tft.colour565(50, 50, 255);
#define DARKBLUE 0x0010
#define VIOLET 0x8888
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define GREY tft.colour565(64, 64, 64);
#define GOLD 0xFEA0
#define BROWN 0xA145
#define SILVER 0xC618
#define LIME 0x07E0
#define PINK tft.colour565(255, 0, 255);
//Set temperature probe input
#define ONE_WIRE_BUS 22
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//Set varibles to run menu codes
int currentpage = 0; //0=homescreen, 1=waterchange menu, 2=temperature sensors.....
void setup()
{
// initialising touch screen display
tft.reset();
tft.begin(tft.readID());
Serial.begin(9600);
Serial.println();
Serial.print("reading id....0x");
delay(500);
Serial.println(tft.readID(), HEX);
tft.fillScreen(BLACK); //fill screen with colour
tft.setRotation(1); //horizontal orientation
tft.setTextSize(3); //set size of font
tft.setTextColor(WHITE); //set colour of text
Serial.println("Dallas Temperature IC Control Library Demo");
sensors.begin();
}
//Draw menu screen
void drawHome()
{
tft.fillScreen(BLACK);
tft.fillRoundRect(60, 180, 200, 40, 8, RED); // sensor menu
tft.fillRoundRect(60, 130, 200, 40, 8, RED); //waterchange menu
tft.setCursor(60, 20);
tft.setTextSize(2);
tft.setFont();
tft.setTextColor(BLACK);
tft.setCursor(65, 195);
tft.print(" Monitoring");
tft.setCursor(105, 145);
tft.print(" Waterchange");
delay(500);
}
// Draw Waterchange Menu
void drawWC()
tft.fillScreen(BLACK);
tft.fillRoundRect(60, 100, 100, 40, 8, BLUE); // WC 10%
tft.setCursor(20, 20);
tft.setTextSize(2);
tft.setFont();
tft.setTextColor(WHITE);
tft.setCursor(65, 115);
tft.print(" WC 10%");
tft.fillRoundRect(60, 150, 100, 40, 8, BLUE); // WC 20%
tft.setCursor(20, 20);
tft.setTextSize(2);
tft.setFont();
tft.setTextColor(WHITE);
tft.setCursor(65, 165);
tft.print(" WC 20%");
tft.fillRoundRect(60, 200, 100, 40, 8, BLUE); // WC 50%
tft.setCursor(20, 20);
tft.setTextSize(2);
tft.setFont();
tft.setTextColor(WHITE);
tft.setCursor(65, 215);
tft.print(" WC 50%");
tft.fillRoundRect(180, 100, 100, 40, 8, DARKBLUE); // Top up
tft.setCursor(20, 20);
tft.setTextSize(2);
tft.setFont();
tft.setTextColor(WHITE);
tft.setCursor(185, 115);
tft.print(" Top-up");
tft.fillRoundRect(180, 150, 100, 40, 8, DARKBLUE); // Drain
tft.setTextSize(2);
tft.setFont();
tft.setTextColor(WHITE);
tft.setCursor(185, 165);
tft.print(" Drain");
tft.fillRoundRect(870, 900, 50, 30, 8, RED); //Return Button
tft.setCursor(885, 915);
tft.setTextColor(BLACK);
tft.print("<-");
delay(300);
{
//Draw water monitoring menu
void drawMonitor();
tft.fillScreen(BLACK);
tft.fillRoundRect(60, 60, 50, 200, 8, BLUE); // Q1
tft.setCursor(20, 20);
tft.setTextSize(2);
tft.setFont();
tft.setTextColor(WHITE);
tft.setCursor(65, 115);
tft.print(" Q1");
tft.fillRoundRect(120, 60, 50, 200, 8, BLUE); // Q2
tft.setCursor(20, 20);
tft.setTextSize(2);
tft.setFont();
tft.setTextColor(WHITE);
tft.setCursor(125, 115);
tft.print(" Q2");
tft.fillRoundRect(180, 60, 50, 200, 8, BLUE); // Q3
tft.setCursor(20, 20);
tft.setTextSize(2);
tft.setFont();
tft.setTextColor(WHITE);
tft.setCursor(185, 115);
tft.print(" Q3");
tft.fillRoundRect(240, 60, 50, 200, 8, BLUE); // Q4
tft.setCursor(20, 20);
tft.setTextSize(2);
tft.setFont();
tft.setTextColor(WHITE);
tft.setCursor(245, 115);
tft.print(" Q4");
tft.fillRoundRect(870, 900, 50, 30, 8, RED); //Return Button
tft.setCursor(885, 915);
tft.setTextColor(BLACK);
tft.print("<-");
delay(300);
}
void loop()
{
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint(); //read touchscreen coordinates
digitalWrite(13, LOW);
pinMode(XW, OUTPUT);
pinMode(YP, OUTPUT);
if (currentpage == 0)
{
if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
{
if (p.x > 563 && p.x < 683 && p.y > 275 && p.y < 750) // check if waterchange menu button is pressed
{
Serial.println("Waterchange Menu Selected");
tft.fillRoundRect(60, 130, 200, 40, 8, WHITE);
delay(100);
tft.fillRoundRect(60, 130, 200, 40, 8, RED);
currentpage = 1; //run waterchange menu code
drawWC(); //open waterchange menu
}
if (p.x > 736 && p.x < 855 && p.y > 255 && p.y < 725) // check if monitoring button is pressed
{
Serial.println("Water Sensing");
tft.fillRoundRect(60, 180, 200, 40, 8, WHITE);
delay(100);
tft.fillRoundRect(60, 180, 200, 40, 8, RED);
currentpage = 2; //run water monitoring menu code
drawMonitor(); //open water monitoring menu
}
}
}
if currentpage == 1 //waterchange menu
{
}
if currentpage == 2 // water monitoring menu code
{
while (currentpage==2)
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
/********************************************************************/
sensors.requestTemperatures(); // Send the command to get temperature readings
/********************************************************************/
float TQ1 = (sensors.getTempCByIndex(0)); // set up variable to read temperature of tank Q1
tft.setCursor(65,135);
tft.print(TQ1);
delay(10000);
}
}