# include <Adafruit_GFX.h>
//# include <Adafruit_TFTLCD.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>
# include <Fonts/FreeSansBold24pt7b.h>
# include <MCUFRIEND_kbv.h>
# include <TouchScreen.h>
//#define LCD_CS A3 // Chip select/ CS goes to analog 3
//#define LCD_RS A2 // Same as CD/ Register selection goes to analog 2
//#define LCD_WR A1 // LCD write and read goes to analog 1
//#define LCD_RD A0 // LCD Read goes to Analog 0
//#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
// Assign human-readable names to some common 16-bit colour values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// make an array for those color
int tftColours[] = {BLUE, RED, GREEN, CYAN, MAGENTA, YELLOW, WHITE};
// If SD card is not used, Pins 2,3,4,5 are free
//Adafruit_TFTLCD tft(LCD_CS, LCD_RS, LCD_WR, LCD_RD, LCD_RESET);
MCUFRIEND_kbv tft;
// Set up the touch screen element of the TFT screen
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
#define SENSITIVITY 300
#define MINPRESSURE 10
#define MAXPRESSURE 2000
#define TS_MINX 169
#define TS_MAXX 984
#define TS_MINY 179
#define TS_MAXY 966
boolean Start_touch_detector = true;
boolean MODE_touch_detector = true;
String MODE;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, SENSITIVITY);
// Define button array object
Adafruit_GFX_Button start_botton;
Adafruit_GFX_Button MODE_A;
Adafruit_GFX_Button MODE_B;
Adafruit_GFX_Button CUSTOMIZED_MODE;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("TFT LCD TEST");
Serial.print("TFT size is ");
Serial.print(tft.width());
Serial.print("x");
Serial.println(tft.height());
tft.reset();
uint16_t identifier = tft.readID();
Serial.print("The Chip ID is : ox");
Serial.println(identifier, HEX);
tft.begin(identifier);
tft.setRotation(1);
start_botton.initButton(&tft,360,280,100,70,WHITE,BLUE,WHITE,"START",2);
MODE_A.initButton(&tft,100,160,100,70,WHITE,RED,WHITE,"A",1);
MODE_B.initButton(&tft,380,160,100,70,WHITE,GREEN,WHITE,"B",1);
CUSTOMIZED_MODE.initButton(&tft,240,230,160,90,WHITE,YELLOW,WHITE,"CUS",1);
start_botton.drawButton();
}
void loop()
{
// put your main code here, to run repeatedly:
//tft.fillScreen(BLACK);
tft.fillRect(0,55,480,80,WHITE);
tft.setTextColor(BLUE);
tft.setFont(&FreeSansBold24pt7b);
tft.setTextSize(0);
tft.setCursor(10, 100);
tft.print("Select a mode!");
waitOneTouch();
Start_touch_detector = true;
//tft.reset();
//uint16_t identifier = tft.readID();
//tft.begin(identifier);
MODE = Mode_Selection();
MODE_touch_detector = true;
}
/*
TSPoint waitOneTouch() {
// Get a touch in the required button area (or anywhere if flag set)
TSPoint p;
do {
p = ts.getPoint();
pinMode(XM, OUTPUT); // Pins configures again for TFT control
pinMode(YP, OUTPUT);
} while (p.z < MINPRESSURE || p.z > MAXPRESSURE);
Serial.print("MAPPING p.x: ");
Serial.println(map(p.x, TS_MINX, TS_MAXX, 0, tft.height()));
Serial.print("MAPPING p.y: ");
Serial.println(map(p.y, TS_MINX, TS_MAXX, 0, tft.width()));
int y = tft.height()-map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
int x = tft.width()-map(p.y, TS_MINY, TS_MAXY, 0, tft.width());
Serial.print("Transformed x: ");
Serial.println(x);
Serial.print("Transformed y: ");
Serial.println(y);
}
*/
TSPoint waitOneTouch()
{
while(Start_touch_detector)
{
TSPoint p = ts.getPoint();
pinMode(XM,OUTPUT);
pinMode(YP,OUTPUT);
if(p.z > MINPRESSURE && p.z < MAXPRESSURE)
{
//Serial.println("entering");
int x = tft.width()-map(p.y, TS_MINY, TS_MAXY, 0, tft.width());
int y = tft.height()-map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
Serial.println(start_botton.contains(x,y));
delay(1000);
if(start_botton.contains(x,y))
{
Serial.print("MAPPING p.x: ");
Serial.println(map(p.x, TS_MINX, TS_MAXX, 0, tft.height()));
Serial.print("MAPPING p.y: ");
Serial.println(map(p.y, TS_MINX, TS_MAXX, 0, tft.width()));
Serial.print("Transformed x: ");
Serial.println(x);
Serial.print("Transformed y: ");
Serial.println(y);
Start_touch_detector = false;
}
}
}
}
String Mode_Selection()
{
String MODE_selection;
tft.fillScreen(BLACK);
MODE_A.drawButton();
MODE_B.drawButton();
CUSTOMIZED_MODE.drawButton();
while(MODE_touch_detector)
{
TSPoint p = ts.getPoint();
pinMode(XM,OUTPUT);
pinMode(YP,OUTPUT);
if(p.z > MINPRESSURE && p.z < MAXPRESSURE)
{
Serial.println("entering");
int x = tft.width()-map(p.y, TS_MINY, TS_MAXY, 0, tft.width());
int y = tft.height()-map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
if(MODE_A.contains(x,y))
{
MODE_selection = "MODE_A";
MODE_touch_detector = false;
}else if(MODE_B.contains(x,y))
{
MODE_selection = "MODE_B";
MODE_touch_detector = false;
}else if(CUSTOMIZED_MODE.contains(x,y))
{
MODE_selection = "CUSTOMIZED_MODE";
MODE_touch_detector = false;
}
if(!MODE_touch_detector)
{
tft.fillScreen(BLACK);
for (int cnt=0; cnt< 20; cnt++)
{
tft.setCursor(140, 100);
tft.setTextColor(getRandomColour());
tft.setFont(&FreeSansBold24pt7b);
tft.setTextSize(1);
tft.print(MODE_selection);
tft.println(" is ACTIVATED");
}
tft.fillScreen(BLACK);
}
}
}
}
unsigned int getRandomColour() {
static unsigned int prevColour = 99;
int colour;
do {
colour = random(0, sizeof tftColours / 2);
} while (colour == prevColour);
prevColour = colour;
return tftColours[colour];
}
I am confused about Why A,B, and CUS does not shown up in the middle of the button ? can anyone explain that for me ?