TFT touch screen issues when used with a Mega2560

This is a strange one for me.

I have a 2.4" 320x240 TFT touchscreen SPI based module purchased from E-bay. It uses the ILI9341 chipset and I'm using the Adafruit_ILI9341.h and Adafruit_GFX.h library files.

The screen happily displays text on the screen but I wanted to try out an option of using the touch sensor part to make menu selection easier. I've connected up the five touch screen pins to the MEGA and then defined them within the code. I'm using the URTouch library and using one of the demo files obtained from the net (suitably edited to reflect the pins used for the mega connections) gave the thing a try. I don't get any response and the XY co-ordinates are always -1.

I tried different pins for the five sensor connection and changed the definitions in the code - still no joy. So I got out my UNO, breadboarded the screen and hooked it it, using the same pin definitions (pins 3-7) for the sensor. Loaded up the demo (which was by default written for the UNO) and it worked. I could draw on the screen using a stylus without any issues.

I saved the code as a new file and then changed the pin definition for the SPI to match the hardware pins used on the MEGA - uploaded and tested. the demo message was displayed correctly, but again the touch part refused to work.

A friend sent me a sample code he used on his UNO. I loaded that in to my UNO and hooked up the screen... worked fine. Saved a copy and changed the pin definitions to match the five wires used (again pins 3-7) and uploaded to the MEGA - the text got displayed as it should, but the touch screen failed to respond.

I've hooked up a logic analyser and can confirm that the t_CS line has a train of pulses when the screen is touched, so it's sending something back to the MEGA -

I'm at a loss. I've also tried different jumper wires, and tried it with and without resistors in series - no matter what I try it works on the UNO but not on the MEGA -

Any ideas.

Here's the code

#include "SPI.h"					// For tft screen and touch control
#include "Adafruit_GFX.h"			// your existing Adafruit Screen libraries
#include "Adafruit_ILI9341.h"
#include "URTouch.h"                //  LIBRARY DOWNLOAD HERE - 

// pinouts for TFT Screen using MEGA

#define TFT_DC 46              
#define TFT_CS 49            
#define TFT_RST 48
#define TFT_MISO 50         
#define TFT_MOSI 51           
#define TFT_CLK 52            

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
//  Use hardware SPI (on Mega2560, #53, #46, #51, #52, #48, #50)

#define t_SCK 3              
#define t_CS 4                
#define t_MOSI 5              
#define t_MISO 6             
#define t_IRQ 7   

// Assign human-readable names to some common 16-bit color values:
#define	BLACK   0x0000
#define	BLUE    0x001F
#define	RED     0xF800
#define	GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

URTouch ts(t_SCK, t_CS, t_MOSI, t_MISO, t_IRQ);

//  variables for Touch Screen, originally local, but made Global
int x;
int y;

void setup()

// Start Touch Screen


// Start Display

tft.setRotation(1);        // Rotation to match 2.4" screen with xy 00 co-ordinate of touch panel
tft.fillScreen(GREEN);    // blank the screen

//welcone screen

tft.setCursor(38, 80);
tft.setTextColor(BLUE	);    tft.setTextSize(3 );
tft.println (" ");
tft.println ("    Touch Demo");


    tft.fillRoundRect(30,196,100, 40,10, BLUE);  // (x,y,width,height,radius,color)
    tft.drawRoundRect(30,196,100,40,10, WHITE);
    tft.fillRoundRect(180,196,100, 40,10, BLUE);  //(x,y,width,height,radius,color)
    tft.drawRoundRect(180,196,100,40,10, WHITE);
    tft.fillRoundRect(30,150,100, 40,10, BLUE);  //(x,y,width,height,radius,color)
    tft.drawRoundRect(30,150,100,40,10, WHITE);

    tft.fillRoundRect(180,150,100, 40,10, BLUE);  // (x,y,width,height,radius,color)
    tft.drawRoundRect(180,150,100,40,10, WHITE);

    tft.setCursor(58, 164);
    tft.setTextColor(WHITE);    tft.setTextSize(2 );
    tft.println ("Feed");
    tft.setCursor(196, 164);
    tft.println ("Lights");
    tft.setCursor(60, 208);
    tft.println ("ATU");
    tft.setCursor(202, 208);
    tft.println ("Doser");

void loop()


void TouchScreen() {
	while (ts.dataAvailable())
		x = ts.getX();
		y = ts.getY();

		//  Feed Delay Touch Routine
		if  ((x>=30) && (x<=100) && (y>=160) && (y<=180))    // IF X and Y are touched in these areas , then print the new screen
			tft.fillScreen(YELLOW);    // blank the screen
			tft.setCursor(60, 60);                           //  TFT PRINT     Vertical,  Horzizonal   240 x 320
			tft.setTextColor(BLUE );    tft.setTextSize(3 );
			tft.println ("FEED DELAY");
			tft.setCursor(56, 120);
			tft.print ("for ");
			tft.println ("3 mins");