Components and supplies
TFT display 3.95
Arduino Mega 2560
Apps and platforms
Arduino IDE
Project description
Code
TFT lcd code
c_cpp
if you are using another mcufriend lcd ( 320X480 ) then in the setup section , change the lcd ID
1/* 2 3 this is a project 4 where i have made custom 5 buttons used to change colour 6 of background 7 8*/ 9 10#include <Adafruit_GFX.h> // main graphics library 11#include <TouchScreen.h> // supporting library 12#include <MCUFRIEND_kbv.h> // main tft library 13 14const int XP=6,XM=A2,YP=A1,YM=7; //320x480 ID=0x9488 15int TS_LEFT=858,TS_RT=142,TS_TOP=757,TS_BOT=104; // values after doing lcd clibration. you can upload the clibration example from one of three libraries used here and then copy paste the values here 16 17 18#define LCD_CS A3 19#define LCD_CD A2 20#define LCD_WR A1 21#define LCD_RD A0 22// optional 23#define LCD_RESET A4 24 25MCUFRIEND_kbv tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); 26 27// initialising the colours used in code 28 29#define BLACK 0x0000 30#define BLUE 0x001F 31#define RED 0xF800 32#define GREEN 0x07E0 33#define CYAN 0x07FF 34#define MAGENTA 0xF81F 35#define YELLOW 0xFFE0 36#define WHITE 0xFFFF 37 38TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); 39 40void setup(){ 41 uint16_t ID = tft.readID(); 42 if (ID == 0xD3) ID = 0x9488; // if your LCD have another ID then paste it. tft.begin(ID); 43 tft.setRotation(1); 44 tft.fillScreen(BLACK); 45 tft.setTextColor(WHITE); 46 tft.setTextSize(3); 47 tft.fillRect(250 , 75 , 100, 100 , BLUE ); 48 tft.fillRect(140,75,100,100,RED); 49 tft.setTextSize(3); 50 tft.setTextColor(BLACK); 51 tft.setCursor(165,115); 52 tft.print("RED"); 53 tft.setCursor(265,115); 54 tft.print("BLUE"); 55 tft.setTextColor(WHITE); 56 tft.setCursor(200,235); 57 tft.print("BLACK"); 58 tft.drawRect(250 , 75 , 100, 100 , WHITE ); 59 tft.drawRect(140,75,100,100,WHITE); 60 tft.drawRect(195,200,100,100,WHITE); 61 62 63 } 64void loop(){ 65 int button_on = 0; 66 int pixel_x, pixel_y; 67 TSPoint p = ts.getPoint(); 68 if (p.z > ts.pressureThreshhold) { 69 70 pixel_x = map(p.y, TS_LEFT=757, TS_RT=104, 0, 480);// mapping the values 71 pixel_y = map(p.x, TS_TOP=142, TS_BOT=858, 0, 320); 72 73 // if the LCD sens the touch in following coordinates, then the condition will be executed 74 75 if(pixel_x>250 && pixel_x<350 && pixel_y>75 && pixel_y<175 ){ 76 77 pinMode(XM, OUTPUT); 78 pinMode(YP, OUTPUT); 79 80 tft.fillScreen(BLUE); 81 tft.drawRect(250 , 75 , 100, 100 , BLACK ); 82 tft.fillRect(140,75,100,100,RED); 83 tft.drawRect(251 , 76 , 98, 98 , WHITE ); 84 tft.drawRect(140,75,100,100,WHITE); 85 tft.fillRect(195,200,100,100,BLACK); 86 tft.drawRect(195,200,100,100,BLACK); 87 tft.drawRect(196,201,100,100,WHITE); 88 tft.setTextSize(3); 89 tft.setTextColor(BLACK); 90 tft.setCursor(165,115); 91 tft.print("RED"); 92 tft.setCursor(265,115); 93 tft.print("BLUE"); 94 tft.setTextColor(WHITE); 95 tft.setCursor(200,235); 96 tft.print("BLACK"); 97 98 } 99 100 if(pixel_x>140 && pixel_x<240 && pixel_y>75 && pixel_y<175 ){ 101 102 pinMode(XM, OUTPUT); 103 pinMode(YP, OUTPUT); 104 105 tft.fillScreen(RED); 106 tft.fillRect(250 , 75 , 100, 100 , BLUE ); 107 tft.drawRect(140,75,100,100,BLACK); 108 tft.drawRect(250 , 75 , 100, 100 , WHITE ); 109 tft.drawRect(141,76,98,98,WHITE); 110 tft.fillRect(195,200,100,100,BLACK); 111 tft.drawRect(195,200,100,100,BLACK); 112 tft.drawRect(196,201,100,100,WHITE); 113 tft.setTextSize(3); 114 tft.setTextColor(BLACK); 115 tft.setCursor(165,115); 116 tft.print("RED"); 117 tft.setCursor(265,115); 118 tft.print("BLUE"); 119 tft.setTextColor(WHITE); 120 tft.setCursor(200,235); 121 tft.print("BLACK"); 122 } 123 124 if(pixel_x>195 && pixel_x<295 && pixel_y>200 && pixel_y<300 ){ 125 126 pinMode(XM, OUTPUT); 127 pinMode(YP, OUTPUT); 128 129 tft.fillScreen(BLACK); 130 tft.fillRect(250 , 75 , 100, 100 , BLUE ); 131 tft.fillRect(140,75,100,100,RED); 132 tft.drawRect(195,200,100,100,WHITE); 133 tft.setTextSize(3); 134 tft.setTextColor(BLACK); 135 tft.setCursor(165,115); 136 tft.print("RED"); 137 tft.setCursor(265,115); 138 tft.print("BLUE"); 139 tft.setTextColor(WHITE); 140 tft.setCursor(200,235); 141 tft.print("BLACK"); 142 } 143 } 144} 145
Comments
Only logged in users can leave comments
sid58_23
0 Followers
•0 Projects
Table of contents
Intro
1
0