/*
CS - 15
RST - 4
DC - 2
MOSI - 23
SCK - 18
MISO - 19
SCL - 22
SDA - 21
*/
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
#include <Wire.h>
#define TFT_DC 2
#define TFT_CS 15
Adafruit_FT6206 ctp = Adafruit_FT6206();
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define BLACK 0X0000
#define BLUE 0X001F
#define RED 0XF800
#define GREEN 0X07E0
#define DARK_GREEN 0x0BA6
#define CYAN 0X07FF
#define MAGENTA 0XF81F
#define YELLOW 0XFFE0
#define WHITE 0XFFFF
//----------------------------------------Button location at point x and y
int BtnGreenX = 30;
int BtnGreenY = 30;
int BtnRedX = 200;
int BtnRedY = 30;
#define LED 33
void DrawButtonGreen(int xp, int yp)
{
tft.fillRoundRect(xp, yp, 84, 84, 10, BLACK);
tft.fillRoundRect(xp+2, yp+2, 80, 80, 10, GREEN);
tft.setCursor(xp+22, yp+32);
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.println("ON");
}
void DrawButtonRed(int xp, int yp)
{
tft.fillRoundRect(xp, yp, 84, 84, 10, BLACK);
tft.fillRoundRect(xp+2, yp+2, 80, 80, 10, RED);
tft.setCursor(xp+18, yp+32);
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.println("OFF");
}
void DrawButtonGreenPress(int xp, int yp)
{
tft.fillRoundRect(xp, yp, 84, 84, 10, BLACK);
}
void DrawButtonRedPress(int xp, int yp)
{
tft.fillRoundRect(xp, yp, 84, 84, 10, BLACK);
}
void setup()
{
Serial.begin(115200);
tft.begin();
tft.setRotation(3);
if(!ctp.begin(40))
{
while(1);
}
tft.fillScreen(BLACK); //--> TFT LCD background color
pinMode(13, OUTPUT);
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
DrawButtonGreen(BtnGreenX,BtnGreenY);
DrawButtonRed(BtnRedX, BtnRedY);
tft.setTextSize(3);
tft.setTextColor(BLUE, WHITE);
tft.setCursor(100, 160);
tft.print("LED OFF");
}
void loop()
{
int x;
int y;
if(!ctp.touched())
{
delay(10);
return;
}
TS_Point p = ctp.getPoint();
x = 320 - p.y; //Value start from High(320) to Low(0). Subtract High from touch point
y = p.x;
//----------------------------------------Condition to detect when the Green Button (Button to turn on LED) is touched and the command
if (x > BtnGreenX && x < (BtnGreenX+84) && y> BtnGreenY && y < (BtnGreenY+84))
{
tft.setTextSize(3);
tft.setTextColor(BLUE, WHITE);
tft.setCursor(100, 160);
tft.print("LED ON ");
digitalWrite(LED, HIGH);
DrawButtonGreenPress(BtnGreenX, BtnGreenY);
delay(100);
DrawButtonGreen(BtnGreenX,BtnGreenY);
}
//----------------------------------------Condition to detect when the Red Button (Button to turn off LED) is touched and the command
if (x > BtnRedX && x < (BtnRedX+84) && y > BtnRedY && y < (BtnRedY+84))
{
tft.setTextSize(3);
tft.setTextColor(BLUE, WHITE);
tft.setCursor(100, 160);
tft.print("LED OFF");
digitalWrite(LED, LOW);
DrawButtonRedPress(BtnRedX, BtnRedY);
delay(100);
DrawButtonRed(BtnRedX, BtnRedY);
}
}
Loading
ili9341-cap-touch
ili9341-cap-touch