Problem with 2.8 inch Touch ILI9341 Display on MEGA

Hi,
this display works very well with Arduino UNO, but not with MEGA (no touch function).
Any ideas what's wrong?

Thanks, and best regards,

Matthias

Display type:
https://www.amazon.de/ILI9341-Display-Seriell-Arduino-Raspberry/dp/B07YTWRZGR

here the code:

 #include "SPI.h"
 #include "Adafruit_GFX.h"
 #include "Adafruit_ILI9341.h"
 #include <XPT2046_Touchscreen.h>
 #include <SdFat.h>                
 #include <Adafruit_ImageReader.h>
 #define IMAGE_PATH "/test.bmp"
 #define TFT_DC 47    // Uno PIN 9
 #define TFT_CS 53    // Uno PIN 10
 #define TFT_RST 46   // Uno PIN 8
 #define CS_PIN 42    // Uno PIN 7
 #define TIRQ_PIN 43  // Uno PIN 2
 #define SD_CS 27     // Uno PIN 4
 #define SCREEN_WIDTH 320
 #define SCREEN_HEIGHT 240
 #define TOUCH_MIN_X 330
 #define TOUCH_MAX_X 3800
 #define TOUCH_MIN_Y 231
 #define TOUCH_MAX_Y 3800
 #define BUTTON_WIDTH 120
 #define BUTTON_HEIGHT 50
 #define BUTTON_SPACING 20
 #define BUTTON_X1 ((SCREEN_WIDTH- (2 * BUTTON_WIDTH + BUTTON_SPACING)) / 2)
 #define BUTTON_X2 (BUTTON_X1 + BUTTON_WIDTH + BUTTON_SPACING)
 #define BUTTON_Y ((SCREEN_HEIGHT- BUTTON_HEIGHT) / 2)
 // Initialize tft display
 Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
 // Initialize touchscreen
 XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);
 SdFat SD;    
// SD card filesystem
 Adafruit_ImageReader reader(SD); // Image-reader object, pass in SD filesys
 int32_t width = 0, // BMP image dimensions
 height = 0;
 ImageReturnCode stat;

 void drawButton(int x, const char* message) {
  tft.fillRoundRect(x, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT, 5, ILI9341_RED);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(2);
  tft.setCursor(x + 15, BUTTON_Y + 15);
  tft.println(message);
 }

 void DisplayImage() {
  tft.fillScreen(ILI9341_WHITE);
  Serial.println(F("Loading image to screen..."));
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(2);
  tft.setCursor(10 , 5);
  tft.println(F("List of files on the SD.\n"));
  tft.println(SD.ls(&tft,LS_R));
  stat = reader.drawBMP(IMAGE_PATH, tft, 0, 50);
  reader.printStatus(stat);
  TS_Point touchPoint = ts.getPoint();
  while(touchPoint.x == ts.getPoint().x);
  loadMainScreen();
 }

 bool isTouched(int x, int y) {
  TS_Point touchPoint = ts.getPoint();
  delay(100);
  int mappedX = map(touchPoint.x, TOUCH_MIN_X, TOUCH_MAX_X, 0, SCREEN_WIDTH);
  int mappedY = map(touchPoint.y, TOUCH_MIN_Y, TOUCH_MAX_Y, 0, SCREEN_HEIGHT);
  return touchPoint.z >= 100 && mappedX >= x && mappedX <= x + BUTTON_WIDTH && mappedY >= BUTTON_Y && mappedY <= BUTTON_Y + BUTTON_HEIGHT;
 }
 void loadMainScreen() {
  tft.fillScreen(ILI9341_WHITE); // Set the background color to white
  drawButton(BUTTON_X1, "Display");
  drawButton(BUTTON_X2, "SD Card");
 }
 void testDisplay() {
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(2);
  tft.setCursor(BUTTON_X1 + 10 , BUTTON_Y + 80);
  tft.println("Hello World");
 }
 void setup() {
  Serial.begin(9600);
  tft.begin();
  tft.setRotation(3); // Set the screen rotation
  loadMainScreen();
 
  ts.begin();
  if(!SD.begin(SD_CS, SD_SCK_MHZ(25)))  {
    Serial.println(F("SD begin() failed"));
  }
 }

 void loop() {
  bool touchedButton1 = isTouched(BUTTON_X1, BUTTON_Y);
  bool touchedButton2 = isTouched(BUTTON_X2, BUTTON_Y);
  if (touchedButton1) {
    Serial.println("Button 1 was pressed.");
    testDisplay();
    delay(3000);
    loadMainScreen();
 
  } else if (touchedButton2) {
    DisplayImage();
    Serial.println("Button 2 was pressed.");
  }
 }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.