/*
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 SOFTBLUE 0x6C9C
#define RED 0XF800
#define SOFTRED 0XF40C
#define GREEN 0X07E0
#define SOFTGREEN 0X05D0
#define DARK_GREEN 0x0BA6
#define CYAN 0X07FF
#define MAGENTA 0XF81F
#define YELLOW 0XFFE0
#define WHITE 0XFFFF
int x, y;
char currentPage = 0;
char selectedUnit = 0;
#define triggerPin 33
#define echoPin 32
long duration;
int distanceCm, distanceInch;
int previousCm, previousInch;
#define ledRed 25
#define ledGreen 26
#define ledBlue 27
int xR=38;
int xG=38;
int xB=38;
void setup()
{
Serial.begin(115200);
tft.begin();
tft.setRotation(3);
if(!ctp.begin(40))
{
while(1);
}
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledBlue, OUTPUT);
drawHomeScreen();
//drawDistanceSensor();
//drawLEDcontrol();
currentPage = '0';
selectedUnit = '0';
}
void loop()
{
if(!ctp.touched())
{
delay(10);
return;
}
// Home Screen
if(currentPage == '0')
{
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;
if((x>=35) && (x<=305) && (y>=90) && (y<=140))
{
drawFrame(35, 90, 305, 140); // Custom Function -Highlighs the buttons when it's pressed
currentPage = '1'; // Indicates that we are the first example
tft.fillScreen(BLACK);
drawDistanceSensor(); // It is called only once, because in the next iteration of the loop, this above if statement will be false so this funtion won't be called. This function will draw the graphics of the first example.
}
// If we press the RGB LED Control Button
if((x>=35) && (x<=305) && (y>=150) && (y<=200))
{
drawFrame(35, 150, 305, 200);
currentPage = '2';
tft.fillScreen(BLACK);
drawLEDcontrol();
}
}
// Distance Sensor
if(currentPage == '1')
{
getDistance(); // Gets distance from the sensor and this function is repeatedly called while we are at the first example in order to print the lasest results from the distance sensor
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;
// If we press the Centimeters Button
if((x>=10) && (x<=90) &&(y>=135) && (y<=162))
{
//drawFrame(10, 135, 90, 162);
selectedUnit = '0';
}
// If we press the Inches Button
if((x>=10) && (x<=90) &&(y>=173) && (y<=201))
{
//drawFrame(10, 173, 90, 201);
selectedUnit = '1';
}
//Back Button
if((x>=10) && (x<=60) &&(y>=10) && (y<=36))
{
drawFrame(10, 10, 60, 36);
currentPage = '0';
tft.fillScreen(BLACK);
drawHomeScreen();
}
}
if(currentPage == '2')
{
setLedColor();
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;
//Back button
if((x>=10) && (x<=60) &&(y>=10) && (y<=36))
{
drawFrame(10, 10, 60, 36);
currentPage = '0';
tft.fillScreen(BLACK);
drawHomeScreen();
// Turns the LED off
analogWrite(ledRed, 0);
analogWrite(ledGreen, 0);
analogWrite(ledBlue, 0);
}
}
}
void drawFrame(int x1, int y1, int x2, int y2)
{
tft.drawRoundRect(x1, y1, x2-x1, y2-y1, 3, RED);
while(ctp.touched())
{
;;
}
tft.drawRoundRect(x1, y1, x2-x1, y2-y1, 3, WHITE);
}
void drawHomeScreen()
{
tft.fillScreen(BLACK);
tft.setTextColor(YELLOW);
tft.setTextSize(1);
tft.setCursor(105, 15);
tft.print("Arduino TFT Menu");
tft.drawLine(1, 28, 320, 28, RED); //(x, y, width, height, color)
tft.setTextColor(BLUE);
tft.setTextSize(2);
tft.setCursor(90, 40);
tft.print("SELECT EXAMPLE");
tft.fillRoundRect(35, 90, 270, 50, 3, DARK_GREEN); //(x, y, width, height, radius, color)
tft.drawRoundRect(35, 90, 270, 50, 3, CYAN);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(90, 108);
tft.print("DISTANCE SENSOR");
tft.fillRoundRect(35, 150, 270, 50, 3, DARK_GREEN);
tft.drawRoundRect(35, 150, 270, 50, 3, CYAN);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(90, 170);
tft.print("RGB LED CONTROL");
}
void drawDistanceSensor()
{
tft.fillScreen(BLACK);
tft.fillRoundRect(10, 10, 50, 26, 3, SOFTBLUE);
tft.drawRoundRect(10, 10, 50, 26, 3, WHITE);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(18, 15);
tft.print("<-");
tft.setTextSize(1);
tft.setCursor(75, 18);
tft.print("Back To Main Menu");
tft.setTextSize(2);
tft.setCursor(90,55);
tft.print("Ultrasonic Sensor");
tft.drawLine(0, 90, 319, 90, RED);
tft.setTextSize(1);
tft.setCursor(15, 114);
tft.print("Select Unit");
tft.setTextSize(2);
tft.setCursor(150,110);
tft.print("Distance:");
tft.fillRoundRect(10, 135, 80, 27, 3, SOFTGREEN);
tft.drawRoundRect(10, 135, 80, 27, 3, WHITE);
tft.setCursor(33, 140);
tft.print("cm");
tft.fillRoundRect(10, 173, 80, 27, 3, MAGENTA);
tft.drawRoundRect(10, 173, 80, 27, 3, WHITE);
tft.setCursor(22,180);
tft.print("inch");
}
void getDistance()
{
//*****clear previous tex with black
tft.setTextSize(4);
tft.setTextColor(BLACK);
tft.setCursor(150,145);
tft.print(previousCm);
tft.setTextSize(2);
tft.setCursor(255,158);
tft.print("cm ");
tft.setTextSize(4);
tft.setTextColor(BLACK);
tft.setCursor(150,145);
tft.print(previousInch);
tft.setTextSize(2);
tft.setCursor(255, 158);
tft.print("inch ");
//**********************************
// Clears the trigPin
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distanceCm= duration*0.034/2;
distanceInch= distanceCm/2.53;
previousCm = distanceCm;
previousInch = distanceInch;
//cm
if(selectedUnit == '0' )
{
tft.setTextColor(SOFTGREEN);
tft.setTextSize(4);
tft.setCursor(150, 145);
tft.print(distanceCm);
tft.setTextSize(2);
tft.setCursor(255,158);
tft.print("cm ");
}
//inch
if(selectedUnit == '1' )
{
tft.setTextColor(MAGENTA);
tft.setTextSize(4);
tft.setCursor(150,145);
tft.print(distanceInch);
tft.setTextSize(2);
tft.setCursor(255, 158);
tft.print("inch ");
}
delay(10);
}
void drawLEDcontrol()
{
tft.fillScreen(BLACK);
tft.fillRoundRect(10, 10, 50, 26, 3, SOFTBLUE);
tft.drawRoundRect(10, 10, 50, 26, 3, WHITE);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(18, 15);
tft.print("<-");
tft.setTextSize(1);
tft.setCursor(75, 18);
tft.print("Back To Main Menu");
tft.setTextSize(2);
tft.setCursor(90,45);
tft.print("RGB LED Control");
tft.drawLine(0, 75, 319, 75, RED);
tft.setCursor(10,95);
tft.print("LED Color:");
tft.setCursor(15, 135);
tft.print("R");
tft.setCursor(15, 165);
tft.print("G");
tft.setCursor(15, 195);
tft.print("B");
tft.drawRect(30, 138, 280, 10, RED);
tft.drawRect(30, 168, 280, 10, GREEN);
tft.drawRect(30, 198, 280, 10, BLUE);
}
void setLedColor()
{
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;
// Area of the Red color slider
if((y>=130) && (y<=156))
{
xR=x; // Stores the X value where the screen has been pressed in to variable xR
if (xR<=38)
{ // Confines the area of the slider to be above 38 pixels
xR=38;
}
if(xR>=300)
{ /// Confines the area of the slider to be under 310 pixels
xR=300;
}
}
// Area of the Green color slider
if((y>=160) && (y<=186))
{
xG=x;
if(xG<=38)
{
xG=38;
}
if(xG>=300)
{
xG=300;
}
}
// Area of the Blue color slider
if((y>=190) && (y<=216))
{
xB=x;
if(xB<=38)
{
xB=38;
}
if(xB>=300)
{
xB=300;
}
}
// Maps the values of the X - Axis from 38 to 0 and 310 to 255, because we need values from 0 to 255 for turning on the led
int xRC = map(xR,38,300,0,255);
int xGC = map(xG,38,300,0,255);
int xBC = map(xB,38,300,0,255);
// Sends PWM signal to the pins of the led
analogWrite(ledRed, xRC);
analogWrite(ledGreen, xGC);
analogWrite(ledBlue, xBC);
//Color Combination Box
tft.fillRoundRect(175, 87, 310-175, 119-87, 3, tft.color565(xRC, xGC, xBC));
//Red slider
tft.fillRect(31, 139, xR-31, 8, tft.color565(xRC,0,0)); //shade of red
tft.fillRect(xR, 139, 4, 8, WHITE); //white rectangle with thickness of 4 pixel
tft.fillRect((xR+5), 139, 310-xR-6, 8, BLACK); //black start after white
//Green slider
tft.fillRect(31, 169, xG-31, 8, tft.color565(0,xGC,0)); //shade of green
tft.fillRect(xG, 169, 4, 8, WHITE); //white rectangle with thickness of 4 pixel
tft.fillRect((xG+5), 169, 310-xG-6, 8, BLACK); //black start after white
//Blue slider
tft.fillRect(31, 199, xB-31, 8, tft.color565(0,0,xBC)); //shade of blue
tft.fillRect(xB, 199, 4, 8, WHITE); //white rectangle with thickness of 4 pixel
tft.fillRect((xB+5), 199, 310-xB-6, 8, BLACK); //black start after white
} Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ili9341-cap-touch
ili9341-cap-touch