In this Project, we will see about how to make Calculator with Arduino & TFT Touch Display.
Circuit Diagram:
Just Install TFT Touch Display Shield to Arduino
Arduino Code:
tft_calculator_code.ino
#include <Adafruit_GFX.h> #include <MCUFRIEND_kbv.h> MCUFRIEND_kbv tft; #include <TouchScreen.h> const int XP = 7, XM = A1, YP = A2, YM = 6; //ID=0x7789 #define BLACK 0x0000 #define RED 0xF800 #define CYAN 0x07FF #define PINK 0xF81F #define YELLOW 0xFFE0 #define WHITE 0xFFFF #define MINPRESSURE 10 #define MAXPRESSURE 1000 #define TS_MINX 199 #define TS_MINY 947 #define TS_MAXX 913 #define TS_MAXY 191 TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); //300 is the sensitivity String symbol[4][4] = { { "7", "8", "9", "/" }, { "4", "5", "6", "*" }, { "1", "2", "3", "-" }, { "C", "0", "=", "+" } }; int X, Y; long Num1, Num2, Number; char action; boolean result = false; void setup() { Serial.begin(9600); //Use serial monitor for debugging tft.reset(); tft.begin(0x7789); //Change ID, it varies with other TFT Displays tft.setRotation(0); // Portrait tft.fillScreen(BLACK); IntroScreen(); delay(3000); draw_BoxNButtons(); } void loop() { TSPoint p = waitTouch(); Y = p.y; X = p.x; DetectButtons(); if (result == true) CalculateResult(); DisplayResult(); delay(300); } TSPoint waitTouch() { TSPoint p; do { p = ts.getPoint(); pinMode(XM, OUTPUT); pinMode(YP, OUTPUT); } while ((p.z < MINPRESSURE ) || (p.z > MAXPRESSURE)); p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240); p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);; return p; } void DetectButtons() { if (X < 60 && X > 0) { //Detecting Buttons on Column 1 if (Y > 260 && Y < 320) { //If cancel Button is pressed Serial.println ("Button Cancel"); Number = Num1 = Num2 = 0; result = false; } if (Y > 200 && Y < 260) { //If Button 1 is pressed Serial.println ("Button 1"); if (Number == 0) Number = 1; else Number = (Number * 10) + 1; //Pressed twice } if (Y > 140 && Y < 200) { //If Button 4 is pressed Serial.println ("Button 4"); if (Number == 0) Number = 4; else Number = (Number * 10) + 4; //Pressed twice } if (Y > 80 && Y < 140) { //If Button 7 is pressed Serial.println ("Button 7"); if (Number == 0) Number = 7; else Number = (Number * 10) + 7; //Pressed twice } } if (X < 120 && X > 60) { //Detecting Buttons on Column 2 if (Y > 260 && Y < 320) { Serial.println ("Button 0"); //Button 0 is Pressed if (Number == 0) Number = 0; else Number = (Number * 10) + 0; //Pressed twice } if (Y > 200 && Y < 260) { Serial.println ("Button 2"); if (Number == 0) Number = 2; else Number = (Number * 10) + 2; //Pressed twice } if (Y > 140 && Y < 200) { Serial.println ("Button 5"); if (Number == 0) Number = 5; else Number = (Number * 10) + 5; //Pressed twic } if (Y > 80 && Y < 140) { Serial.println ("Button 8"); if (Number == 0) Number = 8; else Number = (Number * 10) + 8; //Pressed twic } } if (X < 180 && X > 120) { //Detecting Buttons on Column 3 if (Y > 260 && Y < 320) { Serial.println ("Button Equal"); Num2 = Number; result = true; } if (Y > 200 && Y < 260) { Serial.println ("Button 3"); if (Number == 0) Number = 3; else Number = (Number * 10) + 3; //Pressed twice } if (Y > 140 && Y < 200) { Serial.println ("Button 6"); if (Number == 0) Number = 6; else Number = (Number * 10) + 6; //Pressed twice } if (Y > 80 && Y < 140) { Serial.println ("Button 9"); if (Number == 0) Number = 9; else Number = (Number * 10) + 9; //Pressed twice } } if (X < 240 && X > 180) { //Detecting Buttons on Column 3 Num1 = Number; Number = 0; tft.setCursor(200, 20); tft.setTextColor(CYAN); if (Y > 260 && Y < 320) { Serial.println ("Addition"); action = 1; tft.println('+'); } if (Y > 200 && Y < 260) { Serial.println ("Subtraction"); action = 2; tft.println('-'); } if (Y > 140 && Y < 200) { Serial.println ("Multiplication"); action = 3; tft.println('*'); } if (Y > 80 && Y < 140) { Serial.println ("Division"); action = 4; tft.println('/'); } delay(300); } } void CalculateResult() { if (action == 1) Number = Num1 + Num2; if (action == 2) Number = Num1 - Num2; if (action == 3) Number = Num1 * Num2; if (action == 4) Number = Num1 / Num2; } void DisplayResult() { tft.fillRect(0, 0, 240, 80, RED); //clear result box tft.setCursor(10, 20); tft.setTextSize(4); tft.setTextColor(WHITE); tft.println(Number); //update new value } void IntroScreen() { tft.setCursor (55, 90); tft.setTextSize (3); tft.setTextColor(YELLOW); tft.println("ARDUINO"); tft.setCursor (30, 130); tft.println("CALCULATOR"); tft.setCursor (60, 190); tft.setTextSize (2); tft.setTextColor(WHITE); tft.println("modified by"); tft.setCursor (40, 220); tft.setTextSize (4); tft.setTextColor(CYAN); tft.println("STAYSAY"); } void draw_BoxNButtons() { //Draw the Result Box tft.fillRect(0, 0, 240, 80, RED); //Draw First Column tft.fillRect (0, 260, 60, 60, CYAN); //c tft.fillRect (0, 200, 60, 60, WHITE); //1 tft.fillRect (0, 140, 60, 60, WHITE); //4 tft.fillRect (0, 80, 60, 60, WHITE); //7 //Draw Third Column tft.fillRect (120, 260, 60, 60, PINK); //= tft.fillRect (120, 200, 60, 60, WHITE); //3 tft.fillRect (120, 140, 60, 60, WHITE); //6 tft.fillRect (120, 80, 60, 60, WHITE); //9 //Draw Secound & Fourth Column for (int b = 260; b >= 80; b -= 60) { tft.fillRect (180, b, 60, 60, YELLOW); //4th column tft.fillRect (60, b, 60, 60, WHITE); //2nd column } //Draw Horizontal Lines for (int h = 80; h <= 320; h += 60) tft.drawFastHLine(0, h, 240, BLACK); //Draw Vertical Lines for (int v = 0; v <= 240; v += 60) tft.drawFastVLine(v, 80, 240, BLACK); //Display keypad lables for (int j = 0; j < 4; j++) { for (int i = 0; i < 4; i++) { tft.setCursor(22 + (60 * i), 100 + (60 * j)); tft.setTextSize(3); tft.setTextColor(BLACK); tft.println(symbol[j][i]); } } }
Libraries Used:
1. MCUFRIEND_kbv - https://github.com/prenticedavid/MCUFRIEND_kbv
2. Adafruit-GFX-Library - https://github.com/adafruit/Adafruit-GFX-Library
3. Adafruit_TouchScreen - https://github.com/adafruit/Adafruit_TouchScreen
Youtube Video Tutorial:
Download our official Android App in PlayStore. Click Here
You can get the all the required files (like Circuit Diagram, Arduino.ino file, Libraries Used, and others) for the project in ZIP format and much more...
No comments:
Post a Comment