#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
// Color definitions
#define BLACK 0x0000
#define NAVY 0x000F
#define DARKGREEN 0x03E0
#define DARKCYAN 0x03EF
#define MAROON 0x7800
#define PURPLE 0x780F
#define OLIVE 0x7BE0
#define LIGHTGREY 0xC618
#define DARKGREY 0x7BEF
#define BLUE 0x001F
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define ORANGE 0xFD20
#define GREENYELLOW 0xAFE5
#define PINK 0xF81F
#define YP A2
#define XM A3
#define YM 8
#define XP 9
#define TS_MINX 130
#define TS_MAXX 905
#define TS_MINY 75
#define TS_MAXY 930
#define STATUS_X 10
#define STATUS_Y 65
int relay = 53;
bool light = false;
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
void setup(void) {
Serial.begin(9600);
tft.reset();
uint16_t identifier = 0x9486;
tft.begin(identifier);
tft.setRotation(0);
tft.fillScreen(BLACK);
tft.setCursor(20, 5);
tft.setTextColor(WHITE);
tft.setTextSize(4);
tft.println("Light Switch");
tft.fillRect(10, 45, 300, 5, WHITE);
tft.setCursor(90, 240);
tft.setTextColor(WHITE);
tft.setTextSize(8);
tft.println("OFF");
tft.drawRect(70, 220, 180, 100, WHITE);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
pinMode(53 , OUTPUT);
}
#define MINPRESSURE 10
#define MAXPRESSURE 1000
void loop(void) {
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint();
digitalWrite(13, LOW);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
{
p.x = p.x + p.y;
p.y = p.x - p.y;
p.x = p.x - p.y;
p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = tft.height() - (map(p.y, TS_MINY, TS_MAXY, tft.height(), 0));
}
if (p.z != 0) {
if (light == true) {
light = false;
tft.fillScreen(BLACK);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
}
else {
light = true;
tft.fillScreen(WHITE);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
}
}
if (light == true) {
tft.setCursor(20, 5);
tft.setTextColor(BLACK);
tft.setTextSize(4);
tft.println("Light Switch");
tft.fillRect(10, 45, 300, 5, BLACK);
tft.setCursor(115, 240);
tft.setTextColor(BLACK);
tft.setTextSize(8);
tft.println("ON");
tft.drawRect(100, 220, 120, 100, BLACK);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
digitalWrite(53, HIGH);
}
if (light == false) {
tft.setCursor(20, 5);
tft.setTextColor(WHITE);
tft.setTextSize(4);
tft.println("Light Switch");
tft.fillRect(10, 45, 300, 5, WHITE);
tft.setCursor(90, 240);
tft.setTextColor(WHITE);
tft.setTextSize(8);
tft.println("OFF");
tft.drawRect(70, 220, 180, 100, WHITE);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
digitalWrite(53, LOW);
}
delay(100);
}
Comments