I have reached the point of needing advice and help.
I am designing a fuel flow meter/logger for a race car but am having trouble with the initial part of the project where I am trying to setup the TFT touchscreen pages (I am yet to concern myself with data logging or data acquisition).
I am using an Arduino Mega clone and a MCUFriend 3.2" TFT LCD for Arduino UNO in landscape. My current code sets up one page of the display with various buttons. At the moment only the first 2 buttons on the left of screen do anything and for testing just toggle the page heading between green and yellow font. The other buttons have not been programmed.
My code:
// include libraries
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>
// define constants for stage names
#define STAGE1 "Mooramb."
#define STAGE2 "Dev Hill"
#define STAGE3 "Big Nob"
#define STAGE4 "Smoking."
#define STAGE5 "Mansfie."
#define STAGE6 "Ned Kel."
#define STAGE7 "Mt Bull."
#define STAGE8 "Jamieson"
#define STAGE9 "Serenity"
#define STAGE10 "Transit"
// define colours by name
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// define button press pressures
#define MINPRESSURE 200
#define MAXPRESSURE 1000
MCUFRIEND_kbv tft;
// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
const int XP = 7, XM = A1, YP = A2, YM = 6; //320x480 ID=0x5310
const int TS_LEFT = 643, TS_RT = 519, TS_TOP = 427, TS_BOT = 567;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
// define some button objects
Adafruit_GFX_Button stage1_btn, stage2_btn, stage3_btn, stage4_btn, stage5_btn, stage6_btn, stage7_btn, stage8_btn, stage9_btn, stage10_btn;
// create variables for touch screen
int pixel_x, pixel_y; //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH); //because TFT control pins
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
pixel_x = map(p.y, TS_LEFT, TS_RT, 0, tft.width()); // NOTE THIS SECTION CHANGES DEPENDING ON IF PORT OR LANDSCAPE?????????
pixel_y = map(p.x, TS_TOP, TS_BOT, 0, tft.height()); // SEE CALIBRATION EXAMPLE SKETCH
}
return pressed;
}
void setup() {
Serial.begin(9600);
uint16_t ID = tft.readID();
if (ID == 0xD3D3) ID = 0x9486; // write-only shield
tft.begin(ID);
tft.setRotation(1); //Landscape
tft.fillScreen(BLACK);
stage1_btn.initButton(&tft, 191, 87, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
stage2_btn.initButton(&tft, 191, 139, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
stage3_btn.initButton(&tft, 191, 191, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
stage4_btn.initButton(&tft, 191, 243, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
stage5_btn.initButton(&tft, 191, 295, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
stage6_btn.initButton(&tft, 440, 87, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
stage7_btn.initButton(&tft, 440, 139, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
stage8_btn.initButton(&tft, 440, 191, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
stage9_btn.initButton(&tft, 440, 243, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
stage10_btn.initButton(&tft, 440, 295, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
stage1_btn.drawButton(false);
stage2_btn.drawButton(false);
stage3_btn.drawButton(false);
stage4_btn.drawButton(false);
stage5_btn.drawButton(false);
stage6_btn.drawButton(false);
stage7_btn.drawButton(false);
stage8_btn.drawButton(false);
stage9_btn.drawButton(false);
stage10_btn.drawButton(false);
/*tft.drawLine(2, 2, 479, 2, TFT_WHITE);
tft.drawLine(2, 319, 479, 319, TFT_WHITE);
tft.drawLine(2, 2, 2, 479, TFT_WHITE);
tft.drawLine(479, 2, 479, 319, TFT_WHITE);*/
// setup screen text elements
tft.setCursor(18, 6); tft.setTextColor(TFT_GREEN); tft.setTextSize(5); tft.println("STAGE SELECTION");
tft.setCursor(5, 76); tft.setTextColor(TFT_WHITE); tft.setTextSize(3); tft.println(STAGE1);
tft.setCursor(244, 76); tft.setTextColor(TFT_WHITE); tft.setTextSize(3); tft.println(STAGE6);
tft.setCursor(5, 128); tft.setTextColor(TFT_WHITE); tft.setTextSize(3); tft.println(STAGE2);
tft.setCursor(244, 128); tft.setTextColor(TFT_WHITE); tft.setTextSize(3); tft.println(STAGE7);
tft.setCursor(5, 180); tft.setTextColor(TFT_WHITE); tft.setTextSize(3); tft.println(STAGE3);
tft.setCursor(244, 180); tft.setTextColor(TFT_WHITE); tft.setTextSize(3); tft.println(STAGE8);
tft.setCursor(5, 232); tft.setTextColor(TFT_WHITE); tft.setTextSize(3); tft.println(STAGE4);
tft.setCursor(244, 232); tft.setTextColor(TFT_WHITE); tft.setTextSize(3); tft.println(STAGE9);
tft.setCursor(5, 284); tft.setTextColor(TFT_WHITE); tft.setTextSize(3); tft.println(STAGE5);
tft.setCursor(244, 284); tft.setTextColor(TFT_WHITE); tft.setTextSize(3); tft.println(STAGE10);
}
void loop() {
bool down = Touch_getXY();
stage1_btn.press(down && stage1_btn.contains(pixel_x, pixel_y));
stage2_btn.press(down && stage2_btn.contains(pixel_x, pixel_y));
if (stage1_btn.justReleased())
stage1_btn.drawButton();
if (stage2_btn.justReleased())
stage2_btn.drawButton();
if (stage1_btn.justPressed()) {
stage1_btn.drawButton(true);
tft.setCursor(18, 6); tft.setTextColor(TFT_YELLOW); tft.setTextSize(5); tft.println("STAGE SELECTION");
}
if (stage2_btn.justPressed()) {
stage2_btn.drawButton(true);
tft.setCursor(18, 6); tft.setTextColor(TFT_GREEN); tft.setTextSize(5); tft.println("STAGE SELECTION");
}
}
I did the development work on a UNO but could not get the touch sensitive areas for the 2 buttons to line up with those buttons. Both buttons work but the touch area is only the right hand edge of the buttons or thereabouts (depends on what the latest calibration values were that I entered). I used the mcufriend.kvb calibration example program and dutifully copied the output into my code. I could not get the calibration numbers to be repeatable though. I copied the data into the line at the top;
// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
const int XP = 7, XM = A1, YP = A2, YM = 6; //320x480 ID=0x5310
const int TS_LEFT = 643, TS_RT = 519, TS_TOP = 427, TS_BOT = 567;
Do I also need to change this area;
if (pressed) {
pixel_x = map(p.y, TS_LEFT, TS_RT, 0, tft.width()); // NOTE THIS SECTION CHANGES DEPENDING ON IF PORT OR LANDSCAPE?????????
pixel_y = map(p.x, TS_TOP, TS_BOT, 0, tft.height()); // SEE CALIBRATION EXAMPLE SKETCH
}
I have changed map(p.y, to map(p.x, and vice versa.
Can anybody suggest why it is not calibrating??? I have another identical screen that is cracked and that does the same.
Eventually realised that there were no digital pins spare for use with my sensors so I therefore moved to using the TFT on a Mega (still having the touch position issue).
Upon loading my code (above) I now do not have any touch functionality although the screen displays as it should. The mcufriend.kvb calibration example always shows “BROKEN TOUCHSCREEN”. The button_simple sketch displays but with no touch functionality. The example sketch graphictest_kvb works perfectly.
I really am at the limits of my ability here. I read in a thread that the data in the serial monitor after running the example sketch LCD_ID_readreg is of use in diagnostics so I have included it here;
I guess I mainly need help with getting the Mega to work with the TFT shield and then if I still have the touch alignment issue then help with that too. I think I have included all relevant info?