Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*______Import Libraries_______*/
- #include <Adafruit_GFX.h> // Core graphics library
- #include <MCUFRIEND_kbv.h> // Hardware-specific library
- #include <TouchScreen.h>
- /*______End of Libraries_______*/
- #define YP A1 // must be an analog pin, use "An" notation!
- #define XM A2 // must be an analog pin, use "An" notation!
- #define YM 7 // can be a digital pin
- #define XP 6 // can be a digital pin
- #define LCD_CS A3
- # define LCD_CD A2
- # define LCD_WR A1
- # define LCD_RD A0
- # define LCD_RESET A4
- # define WHITE 0x0000 //Black->White
- #define YELLOW 0x001F //Blue->Yellow
- #define CYAN 0xF800 //Red->Cyan
- #define PINK 0x07E0 //Green-> Pink
- #define RED 0x07FF //Cyan -> Red
- #define GREEN 0xF81F //Pink -> Green
- #define BLUE 0xFFE0 //Yellow->Blue
- #define BLACK 0xFFFF //White-> Black
- #define MINPRESSURE 2
- #define MAXPRESSURE 1000
- #define TS_MINX 0
- #define TS_MINY 0
- #define TS_MAXX 600
- #define TS_MAXY 600
- TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); //300 is the sensitivity
- MCUFRIEND_kbv tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); //Start communication with LCD
- String symbol[4][4] = {
- { "7", "8", "9", "/"},
- { "4", "5", "6", "*"},
- { "1", "2", "3", "-"},
- { "C", "0", "=", "+"}
- };
- int X, Y, action;
- double Num1, Num2, Number;
- bool lastEqual;
- void setup() {
- Serial.begin(9600); //Use serial monitor for debugging
- uint16_t ID = tft.readID();
- if (ID == 0xD3D3)
- ID = 0x9486; // write-only shield
- tft.reset(); //Always reset at start
- tft.begin(ID); // My LCD uses LIL9341 Interface driver IC
- tft.setRotation(0); // I just roated so that the power jack faces up - optional
- tft.fillScreen(WHITE);
- IntroScreen();
- draw_BoxNButtons();
- lastEqual = false;
- }
- void loop() {
- TSPoint p = waitTouch();
- X = p.x;
- Y = p.y;
- DetectButtons();
- 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() {
- Serial.print("X = ");
- Serial.println(X);
- Serial.print("Y = ");
- Serial.println(Y);
- if (X < 150 && X > 65) { //Detecting Buttons on Column 1
- if (Y > 350 && Y < 410) { //If cancel Button is pressed
- Serial.println("Button Cancel");
- Number = Num1 = Num2 = 0;
- action = -1;
- }
- else if (Y > 280 && Y < 335) { //If Button 1 is pressed
- Serial.println("Button 1");
- if (lastEqual) {
- lastEqual = false;
- Number = Num1 = Num2 = 0;
- }
- Number = (Number * 10) + 1;
- }
- else if (Y > 225 && Y < 270) { //If Button 4 is pressed
- Serial.println("Button 4");
- if (lastEqual) {
- lastEqual = false;
- Number = Num1 = Num2 = 0;
- }
- Number = (Number * 10) + 4;
- }
- else if (Y > 120 && Y < 200) { //If Button 7 is pressed
- Serial.println("Button 7");
- if (lastEqual) {
- lastEqual = false;
- Number = Num1 = Num2 = 0;
- }
- Number = (Number * 10) + 7;
- }
- }
- else if (X < 210 && X > 150) { //Detecting Buttons on Column 2
- if (Y > 350 && Y < 410) {
- Serial.println("Button 0"); //Button 0 is Pressed
- if (lastEqual) {
- lastEqual = false;
- Number = Num1 = Num2 = 0;
- }
- Number = (Number * 10) + 0;
- }
- else if (Y > 280 && Y < 335) {
- Serial.println("Button 2");
- if (lastEqual) {
- lastEqual = false;
- Number = Num1 = Num2 = 0;
- }
- Number = (Number * 10) + 2;
- }
- else if (Y > 225 && Y < 270) {
- Serial.println("Button 5");
- if (lastEqual) {
- lastEqual = false;
- Number = Num1 = Num2 = 0;
- }
- Number = (Number * 10) + 5;
- }
- else if (Y > 120 && Y < 200) {
- Serial.println("Button 8");
- if (lastEqual) {
- lastEqual = false;
- Number = Num1 = Num2 = 0;
- }
- Number = (Number * 10) + 8;
- }
- }
- else if (X < 280 && X > 210) { //Detecting Buttons on Column 3
- if (Y > 350 && Y < 410) {
- Serial.println("Button Equal");
- CalculateResult();
- lastEqual = true;
- }
- else if (Y > 280 && Y < 335) {
- Serial.println("Button 3");
- if (lastEqual) {
- lastEqual = false;
- Number = Num1 = Num2 = 0;
- }
- Number = (Number * 10) + 3;
- }
- else if (Y > 225 && Y < 270) {
- Serial.println("Button 6");
- if (lastEqual) {
- lastEqual = false;
- Number = Num1 = Num2 = 0;
- }
- Number = (Number * 10) + 6;
- }
- else if (Y > 120 && Y < 200) {
- Serial.println("Button 9");
- if (lastEqual) {
- lastEqual = false;
- Number = Num1 = Num2 = 0;
- }
- Number = (Number * 10) + 9;
- }
- }
- else if (X < 345 && X > 280) { //Detecting Buttons on Column 4
- tft.setCursor(200, 20);
- tft.setTextColor(RED);
- if (Y > 350 && Y < 410) {
- Serial.println("Addition");
- action = 0;
- tft.println('+');
- Num1 = Number;
- Number = 0;
- }
- else if (Y > 280 && Y < 335) {
- Serial.println("Subtraction");
- action = 1;
- tft.println('-');
- Num1 = Number;
- Number = 0;
- }
- else if (Y > 225 && Y < 270) {
- Serial.println("Multiplication");
- action = 2;
- tft.println('*');
- Num1 = Number;
- Number = 0;
- }
- else if (Y > 120 && Y < 200) {
- Serial.println("Division");
- action = 3;
- tft.println('/');
- Num1 = Number;
- Number = 0;
- }
- }
- X = Y = -1;
- delay(300);
- }
- void CalculateResult() {
- if (action == -1)
- Num1 = Number;
- Num2 = Number;
- if (action == -1) {
- Number = Num1;
- Num2 = 0;
- }
- else if (action == 0) {
- Number = Num1 + Num2;
- Num1 = Number;
- Num2 = 0;
- }
- else if (action == 1) {
- Number = Num1 - Num2;
- Num1 = Number;
- Num2 = 0;
- }
- else if (action == 2) {
- Number = Num1 * Num2;
- Num1 = Number;
- Num2 = 0;
- }
- else if (action == 3) {
- if(Num2 == 0){
- tft.println("Invalid");
- delay(1800);
- }
- else{
- Number = Num1 / Num2;
- }
- Num1 = Number;
- Num2 = 0;
- }
- action = -1;
- }
- void DisplayResult() {
- tft.fillRect(0, 0, 240, 80, CYAN); //clear result box
- tft.setCursor(10, 20);
- tft.setTextSize(4);
- tft.setTextColor(BLACK);
- tft.println(Number); //update new value
- }
- void IntroScreen() {
- tft.setCursor(55, 120);
- tft.setTextSize(3);
- tft.setTextColor(RED);
- tft.println("ARDUINO");
- tft.setCursor(30, 160);
- tft.println("CALCULATOR");
- tft.setCursor(30, 220);
- tft.setTextSize(2);
- tft.setTextColor(BLUE);
- tft.println(" CSED 19");
- delay(1800);
- }
- void draw_BoxNButtons() {
- //Draw the Result Box
- tft.fillRect(0, 0, 240, 80, CYAN);
- //Draw First Column
- tft.fillRect(0, 260, 60, 60, RED);
- tft.fillRect(0, 200, 60, 60, BLACK);
- tft.fillRect(0, 140, 60, 60, BLACK);
- tft.fillRect(0, 80, 60, 60, BLACK);
- //Draw Third Column
- tft.fillRect(120, 260, 60, 60, GREEN);
- tft.fillRect(120, 200, 60, 60, BLACK);
- tft.fillRect(120, 140, 60, 60, BLACK);
- tft.fillRect(120, 80, 60, 60, BLACK);
- //Draw Secound & Fourth Column
- for (int b = 260; b >= 80; b -= 60) {
- tft.fillRect(180, b, 60, 60, BLUE);
- tft.fillRect(60, b, 60, 60, BLACK);
- }
- //Draw Horizontal Lines
- for (int h = 80; h <= 320; h += 60)
- tft.drawFastHLine(0, h, 240, WHITE);
- //Draw Vertical Lines
- for (int v = 0; v <= 240; v += 60)
- tft.drawFastVLine(v, 80, 240, WHITE);
- //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(WHITE);
- tft.println(symbol[j][i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement