This is some code for a calculator (I haven't got code for the actual calculations yet), but every time I press one of the buttons, it prints it overtop of the last number. In other words, it isn't printing the numbers sequentially. Where is the code messed up?
It looks like the first line in the void loop is what is messing it up, but I don't know how to tell it to only set the cursor for the FIRST number.
#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 = 1; //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));
four_btn.press(down && four_btn.contains(pixel_x, pixel_y));
five_btn.press(down && five_btn.contains(pixel_x, pixel_y));
six_btn.press(down && six_btn.contains(pixel_x, pixel_y));
times_btn.press(down && times_btn.contains(pixel_x, pixel_y));
one_btn.press(down && one_btn.contains(pixel_x, pixel_y));
two_btn.press(down && two_btn.contains(pixel_x, pixel_y));
three_btn.press(down && three_btn.contains(pixel_x, pixel_y));
minus_btn.press(down && minus_btn.contains(pixel_x, pixel_y));
clear_btn.press(down && clear_btn.contains(pixel_x, pixel_y));
zero_btn.press(down && zero_btn.contains(pixel_x, pixel_y));
equals_btn.press(down && equals_btn.contains(pixel_x, pixel_y));
plus_btn.press(down && plus_btn.contains(pixel_x, pixel_y));
if (seven_btn.justPressed())
{
tft.print("7");
}
if (eight_btn.justPressed())
{
tft.print("8");
}
if (nine_btn.justPressed())
{
tft.print("9");
}
if (divide_btn.justPressed())
{
tft.print("/");
}
if (four_btn.justPressed())
{
tft.print("4");
}
if (five_btn.justPressed())
{
tft.print("5");
}
if (six_btn.justPressed())
{
tft.print("6");
}
if (times_btn.justPressed())
{
tft.print("x");
}
if (one_btn.justPressed())
{
tft.print("1");
}
if (two_btn.justPressed())
{
tft.print("2");
}
if (three_btn.justPressed())
{
tft.print("3");
}
if (minus_btn.justPressed())
{
tft.print("-");
}
if (clear_btn.justPressed())
{
tft.fillScreen(LIGHTGREY);
draw();
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);
}
if (zero_btn.justPressed())
{
tft.print("0");
}
if (equals_btn.justPressed())
{
tft.print("=");
}
if (plus_btn.justPressed())
{
tft.print("+");
}
}
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);
}
If you know how to fix this, please enlighten me!