I'm making a calculator on a tft screen, but only the 9 button works.
I have set up the 7, 8, 9 buttons but only the nine one works.
Why is that?
#include "Adafruit_GFX.h" // Hardware-specific library
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#define BLACK 0x0000 /* 0, 0, 0 */
#define NAVY 0x000F /* 0, 0, 128 */
#define DARKGREEN 0x03E0 /* 0, 128, 0 */
#define DARKCYAN 0x03EF /* 0, 128, 128 */
#define MAROON 0x7800 /* 128, 0, 0 */
#define PURPLE 0x780F /* 128, 0, 128 */
#define OLIVE 0x7BE0 /* 128, 128, 0 */
#define LIGHTGREY 0xC618 /* 192, 192, 192 */
#define DARKGREY 0x7BEF /* 128, 128, 128 */
#define BLUE 0x001F /* 0, 0, 255 */
#define GREEN 0x07E0 /* 0, 255, 0 */
#define CYAN 0x07FF /* 0, 255, 255 */
#define RED 0xF800 /* 255, 0, 0 */
#define MAGENTA 0xF81F /* 255, 0, 255 */
#define YELLOW 0xFFE0 /* 255, 255, 0 */
#define WHITE 0xFFFF /* 255, 255, 255 */
#define ORANGE 0xFD20 /* 255, 165, 0 */
#define GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define PINK 0xF81F
#define DARKBLUE 0x0010
#define VIOLET 0x8888
#define GOLD 0xFEA0
#define BROWN 0xA145
#define SILVER 0xC618
#define LIME 0x07E0
#define ORANGERED 0xFA20
#include <XPT2046_Touchscreen.h> // Hardware SPI library
char *name = "Shield XPT2046 Calibration"; //edit name of shield
const int TS_LANDSCAPE = 0; //XPT2046_TouchScreen.h
const int TS_LEFT = 261, TS_RT = 3770, TS_TOP = 3869, TS_BOT = 296;
#define XPT_CS 53 // MEGA2560 SHIELD
#define XPT_IRQ 255 // XPT2046 library does not like IRQ
XPT2046_Touchscreen ts(XPT_CS, XPT_IRQ);
int pixel_x, pixel_y; //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
bool pressed = ts.touched();
if (pressed) {
TS_Point p = ts.getPoint();
if (TS_LANDSCAPE) mapxy(p.y, p.x);
else mapxy(p.x, p.y);
}
return pressed;
}
void Touch_init(void)
{
ts.begin();
}
void mapxy(int x, int y) //maps ADC to pixel_x, pixel_y
{
int aspect = tft.getRotation(); //LANDSCAPE
int tft_width = tft.width();
int tft_height = tft.height();
switch (aspect & 3) {
case 0: //PORTRAIT
pixel_x = map(x, TS_LEFT, TS_RT, 0, tft_width);
pixel_y = map(y, TS_TOP, TS_BOT, 0, tft_height);
break;
case 1: //LANDSCAPE
pixel_x = map(y, TS_TOP, TS_BOT, 0, tft_width);
pixel_y = map(x, TS_RT, TS_LEFT, 0, tft_height);
break;
case 2: //PORTRAIT REV
pixel_x = map(x, TS_RT, TS_LEFT, 0, tft_width);
pixel_y = map(y, TS_BOT, TS_TOP, 0, tft_height);
break;
case 3: //LANDSCAPE REV
pixel_x = map(y, TS_BOT, TS_TOP, 0, tft_width);
pixel_y = map(x, TS_LEFT, TS_RT, 0, tft_height);
break;
}
}
Adafruit_GFX_Button seven_btn, eight_btn, nine_btn, divide_btn, four_btn, five_btn, six_btn, times_btn, one_btn, two_btn, three_btn, minus_btn, clear_btn, zero_btn, equals_btn, plus_btn;
void setup()
{
Serial.begin(9600);
Serial.println("Calculator");
tft.begin(0x9488);
Touch_init();
tft.setRotation(0);
tft.fillScreen(BLACK);
tft.fillRoundRect(0, 0, 320, 480, 40, CYAN);
drawintro();
delay(5000);
tft.fillScreen(LIGHTGREY);
seven_btn.initButton(&tft, 40, 165, 70, 70, BLACK, WHITE, BLACK, "7", 3);
eight_btn.initButton(&tft, 120, 165, 70, 70, BLACK, WHITE, BLACK, "8", 3);
nine_btn.initButton(&tft, 200, 165, 70, 70, BLACK, WHITE, BLACK, "9", 3);
divide_btn.initButton(&tft, 280, 165, 70, 70, BLACK, BLUE, WHITE, "/", 3);
four_btn.initButton(&tft, 40, 245, 70, 70, BLACK, WHITE, BLACK, "4", 3);
five_btn.initButton(&tft, 120, 245, 70, 70, BLACK, WHITE, BLACK, "5", 3);
six_btn.initButton(&tft, 200, 245, 70, 70, BLACK, WHITE, BLACK, "6", 3);
times_btn.initButton(&tft, 280, 245, 70, 70, BLACK, BLUE, WHITE, "X", 3);
one_btn.initButton(&tft, 40, 325, 70, 70, BLACK, WHITE, BLACK, "1", 3);
two_btn.initButton(&tft, 120, 325, 70, 70, BLACK, WHITE, BLACK, "2", 3);
three_btn.initButton(&tft, 200, 325, 70, 70, BLACK, WHITE, BLACK, "3", 3);
minus_btn.initButton(&tft, 280, 325, 70, 70, BLACK, BLUE, WHITE, "-", 3);
clear_btn.initButton(&tft, 40, 405, 70, 70, BLACK, RED, BLACK, "CE", 3);
zero_btn.initButton(&tft, 120, 405, 70, 70, BLACK, WHITE, BLACK, "0", 3);
equals_btn.initButton(&tft, 200, 405, 70, 70, BLACK, GREEN, BLACK, "=", 3);
plus_btn.initButton(&tft, 280, 405, 70, 70, BLACK, BLUE, WHITE, "+", 3);
seven_btn.drawButton(false);
eight_btn.drawButton(false);
nine_btn.drawButton(false);
divide_btn.drawButton(false);
four_btn.drawButton(false);
five_btn.drawButton(false);
six_btn.drawButton(false);
times_btn.drawButton(false);
one_btn.drawButton(false);
two_btn.drawButton(false);
three_btn.drawButton(false);
minus_btn.drawButton(false);
clear_btn.drawButton(false);
zero_btn.drawButton(false);
equals_btn.drawButton(false);
plus_btn.drawButton(false);
draw();
}
void loop() {
tft.setCursor(25, 63);
tft.setTextColor(BLACK);
bool down = Touch_getXY();
seven_btn.press(down && seven_btn.contains(pixel_x, pixel_y));
eight_btn.press(down && eight_btn.contains(pixel_x, pixel_y));
nine_btn.press(down && nine_btn.contains(pixel_x, pixel_y));
divide_btn.press(down && divide_btn.contains(pixel_x, pixel_y));
if (seven_btn.justReleased()) {
seven_btn.drawButton();
eight_btn.drawButton();
nine_btn.drawButton();
divide_btn.drawButton();
}
if (eight_btn.justReleased()) {
eight_btn.drawButton();
seven_btn.drawButton();
nine_btn.drawButton();
divide_btn.drawButton();
}
if (nine_btn.justReleased()) {
seven_btn.drawButton();
nine_btn.drawButton();
eight_btn.drawButton();
divide_btn.drawButton();
}
if (divide_btn.justReleased()) {
seven_btn.drawButton();
nine_btn.drawButton();
divide_btn.drawButton();
eight_btn.drawButton();
}
if (seven_btn.justPressed())
{
tft.print("7");
}
if (eight_btn.justPressed())
{
tft.print("8");
}
if (nine_btn.justPressed())
{
tft.print("9");
}
}
void drawintro()
{
tft.setTextSize(3);
tft.setTextColor(BLUE);
tft.setCursor(100, 100);
tft.print("Arduino");
tft.setCursor(75, 140);
tft.setTextColor(YELLOW);
tft.print("CALCULATOR");
tft.setCursor(90, 250);
tft.setTextColor(WHITE);
tft.print("Touch To");
tft.setCursor(90, 280);
tft.print("Continue");
tft.setCursor(100, 50);
tft.fillRoundRect(62, 40, 200, 40, 8, YELLOW);
tft.setTextColor(RED);
tft.print("");
tft.fillRoundRect(72, 215, 180, 5, 8, GREEN);
}
void draw()
{
tft.fillRoundRect(5, 5, 310, 115, 8, WHITE);
tft.drawRoundRect(5, 5, 310, 115, 8, BLACK);
}