title: Listing the SD card files on the Arduino TFT Screen
body: <p>I'm experiencing a problem while trying to list files in an SD card:
  I have the following code:</p>
  
  <pre><code>#include &lt;SPI.h&gt;
  #include &lt;SD.h&gt;
  #include &lt;TFT.h&gt;  
  
  #define sd_cs  4
  #define lcd_cs 10
  #define dc     9
  #define rst    8  
  
  TFT TFTscreen = TFT(lcd_cs, dc, rst);
  
  PImage logo;
  
  void intro() {
      TFTscreen.begin();
      TFTscreen.background(255, 255, 255);
      TFTscreen.stroke(0, 0, 255);
      TFTscreen.println();
      TFTscreen.println("Welcome..");
      delay(500);
  }
  
  
  void draw(char *imageName)  {
    TFTscreen.begin();
    TFTscreen.background(255, 255, 255);
  
    // Load the image file.
    logo = TFTscreen.loadImage(imageName);
    if (!logo.isValid()) {
      Serial.println("error while loading the image");
    }
    if (logo.isValid() == false) {
      return;
    }
  
    Serial.println("drawing image");
  
    // draw the image to the screen
    TFTscreen.image(logo, 0, 0);
  }  
  
  void initSD() {    
     Serial.print("Initializing SD card...");
     if (!SD.begin(sd_cs)) {
          Serial.println("failed!");
          return;
     }
     Serial.println("OK!"); 
  
  }  
  
  void setup() 
  
      Serial.begin(9600);
  
      intro();
  
      initSD();
  
    draw("2.bmp");
  }
  
  void loop() { }
  </code></pre>
  
  <p>The above code works fine, the "Welcome" message is displayed on the screen, followed by the display of the "2.bmp" image as expected. The serial output prints:</p>
  
  <pre><code>IniInitializing SD card...OK!
  File size: 61496
  Image Offset: 54
  Header size: 40
  Bit Depth: 24
  Image size: 128x160
  drawing image
  </code></pre>
  
  <p>Now I tried to list the files on the root of the SD card:</p>
  
  <pre><code>File root;
  void listSD()  {
      root = SD.open("/");
      printDirectory(root);
  }
  
  void printDirectory(File dir) {
      while(true) {
  
          File entry =  dir.openNextFile();
          if (! entry) {
              dir.rewindDirectory();
              break;
          }
  
          Serial.println(entry.name());
     }
  }
  
  void setup() {
      Serial.begin(9600);
  
      intro();
  
      initSD();
      listSD(); //&lt;- this function here
  
     draw("2.bmp");
  </code></pre>
  
  <p>}</p>
  
  <p>But I get file not found error, right after the root folder is listed:</p>
  
  <pre><code>Initializing SD card...OK!
  1.BMP
  2.BMP
  loadImage: file not found: 2.bmp
  error while loading the image
  </code></pre>
  
  <p>Can you please tell me what could be wrong here ? I suspect the <code>root = SD.open("/");</code> somehow affects the <code>TFTscreen.loadImage(imageName);</code> but I have no idea how.</p>
  
  <p>I'm using an Arduino UNO R3, with a <a href="http://arduino.cc/en/Main/GTFT" rel="nofollow">TFT LCD</a> screen, IDE version is 1.5.5.</p>