ILI 9341 + SD card, file not found, listing files works

Hi,
I have ILI 9341 Touch LCD 2.8'' with SDCard. I am running the example:
tutorial

I connected Mega with Ili like this: diagram

I connected SD Card on my LCD to the project using the same pins and connections that I used for LCD, so for example CS and SD_CS are on pin 53.

Program starts and tries to refresh the screen there are two problems:

  1. The program says that there is no such file on the card, however listing all files shows something else..
  2. When I insert SD card Utouch library for handling the touch events always returns point 0,319, when I remove sc card, utouch work better and I am able to navigate my menu.

Initialising SD card...225term.bmp225term.raw225termi.bmp240Moon.bmp240Moon.raw284taz.bmp284taz.raw480Mouse.bmpInst_1.bmpInst_1.rawtest_dn.bmptest_up.bmptest_up.rawtest_ups.bmptest_ups.rawTiger.bmpTiger.rawHere we go...

File not found
0
299
File not found
File not found
0
File not found
File not found

Below is the sketch:

#include <Adafruit_GFX_AS.h>     // Core graphics library
#include <Adafruit_ILI9341_AS.h> // Hardware-specific library
//#include <SD.h>                // SD card library a bit slower and bigeger than SdFat
#include <SdFat.h>               // More compact and faster than SD
SdFat SD;                        // For SD compatibility
#include <SPI.h>                 // SPI libray obviously!
String sdata="";
 
SdFile file;
// These are the pins I use on an UNO, may need chaging for your setup
 
// Use hardware SPI lines
#define _sclk 52
#define _miso 50 // Needed for SD card, but does not need to be connected to TFT
#define _mosi 51 // Master Out Slave In to send commands/data to TFT and SD card

// TFT chip select and data/command line
#define _cs 53
#define _dc 49

// SD chip select
#define _sdcs 53

// TFT reset line, can be connected to Arduino reset
#define _rst 48

Adafruit_ILI9341_AS tft = Adafruit_ILI9341_AS(_cs, _dc, _rst); // Invoke custom library

// You can use MS Paint to pick colours off an image and see the RGB values
// This colour is off the mouse image.
#define ILI9341_GREY 0xCE9A // Light grey

// These are used when calling drawBMP() function, see examples in loop()
#define BU_BMP 1 // Temporarily flip the TFT coords for standard Bottom-Up bit maps
#define TD_BMP 0 // Draw inverted Top-Down bitmaps in standard coord frame

uint32_t drawTime = 0; // Variable to save draw times for testing
 

void setup()
{
  Serial.begin(9600); // For debug messages
/********************* Add these Two Lines **********************/
pinMode(10, OUTPUT); // change this to 53 on a mega  // don't follow this!!
digitalWrite(10, HIGH); // Add this line
/***************************************************************/

  Serial.print(F("Initialising SD card..."));
  if (!SD.begin(_sdcs, SPI_FULL_SPEED)) {  // sdFat library allows speed setting, e.g. SPI_HALF_SPEED
    //if (!SD.begin(_sdcs)) {              // Only needed when standard SD library is used
    Serial.println(F("failed!"));
    //return;
  }
while (file.openNext(SD.vwd(), O_READ)) {
      file.printName(&Serial);
      file.close();
     
    }
  tft.init(); // Initialise the display (various parameters configured)

  Serial.println(F("Here we go..."));
 Serial.println(sdata);
  // Set text foreground and background colours
  tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
}
 
void loop()
{
 
  tft.setRotation(1);
  tft.fillScreen(ILI9341_GREY);
  
  // Draws raw image with top left corner pixel at x,y = 0,0
  drawRAW("Inst_1.raw", 0, 0, 320, 240);
  // Using a bmp will be 50% slower and draws from bottom up
  //drawBMP("Inst_1.bmp", 0, 0, BU_BMP);
  
  // Show draw time (time measuring lines can be commented out in function if not needed)
  int xpos = 0;
  xpos += tft.drawNumber(drawTime, xpos, 0, 2);
  tft.drawString("ms to draw", xpos, 0, 2);
      Serial.println(drawTime);
  delay(4000);

  // See how long slear screen takes
  unsigned long timenow = millis();
  tft.fillScreen(ILI9341_BLACK);
  timenow = millis()-timenow;
  Serial.println(timenow);

  // Draw bmp image top left corner at x,y = 40,0 which centres the 240 pixel wide bitmap
  // Image must fit (one day I will add clipping... but it will slow things down)
  drawBMP("240Moon.bmp", 40, 0, BU_BMP);
  xpos = 0;
  xpos += tft.drawNumber(drawTime, xpos, 0, 2);
  tft.drawString("ms to draw", xpos, 0, 2);
  delay(1000);

  // Now draw the raw image for speed comparison
  tft.fillScreen(ILI9341_BLACK);
  // Draw image top left corner at x,y = 40,0 which centres the 240 pixel wide bitmap
  // Image must fit (one day I will add clipping... but it will slow things down)
  drawRAW("240Moon.raw", 40, 0, 240, 240);
  xpos = 0;
  xpos += tft.drawNumber(drawTime, xpos, 0, 2);
  tft.drawString("ms to draw", xpos, 0, 2);
  Serial.println(drawTime);
  delay(1000);
 
}


/***************************************************************************************
** Function name:           drawBMP
** Descriptions:            draw a BMP format bitmap to the screen
***************************************************************************************/

 
#define BUFF_SIZE 80

void drawBMP(char *filename, int x, int y, boolean flip) {
  if ((x >= tft.width()) || (y >= tft.height())) return;
  File     bmpFile;
  int16_t  bmpWidth, bmpHeight;   // Image W+H in pixels
  //uint8_t  bmpDepth;            // Bit depth (must be 24) but we dont use this
  uint32_t bmpImageoffset;        // Start address of image data in file
  uint32_t rowSize;               // Not always = bmpWidth; may have padding
  uint8_t  sdbuffer[3 * BUFF_SIZE];    // SD read pixel buffer (8 bits each R+G+B per pixel)
  uint16_t tftbuffer[BUFF_SIZE];       // TFT pixel out buffer (16-bit per pixel)
  uint8_t  sd_ptr = sizeof(sdbuffer); // sdbuffer pointer (so BUFF_SIZE must be less than 86)
  boolean  goodBmp = false;            // Flag set to true on valid header parse
  int16_t  w, h, row, col;             // to store width, height, row and column
  //uint8_t  r, g, b;   // brg encoding line concatenated for speed so not used
  uint8_t rotation;     // to restore rotation
  uint8_t  tft_ptr = 0;  // buffer pointer

  // Check file exists and open it
  if ((bmpFile = SD.open(filename)) == NULL) {
    Serial.println(F("File not found")); // Can comment out if not needed
    return;
  }
(......................)
 
}
 
void drawRAW(char *filename, int16_t x, int16_t y, int16_t rawWidth, int16_t rawHeight) {
  File     rawFile;
  uint8_t  sdbuffer[2 * BUFF_SIZE];   // SD read pixel buffer (16 bits per pixel)

  // Check file exists and open it
  if ((rawFile = SD.open(filename)) == NULL) {
    Serial.println(F("File not found"));
    return;
  }
(..............)
  
  rawFile.close();
}