Can you display images on tft screen using a .txt file storing an array

I am running out of space in my program and my sd card refuses to read bitmaps but can read txt files. Is there a way I could read an array from a txt file and display it like you would with the drawRGBBitmap function?

If your SD card is reading anything at all, then it is extremely unlikely that it can't read a bitmap file.

The problem is either that the bitmap file is not written properly to the card (can you open it on your computer?), or the code reading the file is incorrect.

LlamaLeader:
I am running out of space in my program and my sd card refuses to read bitmaps but can read txt files. Is there a way I could read an array from a txt file and display it like you would with the drawRGBBitmap function?

sd card refuses to read bitmaps? Please post a copy of the program using code tags.

This is just test code I got from Adafruit. The image does display fine when I open it on my computer.

// Adafruit_ImageReader test for Adafruit ILI9341 TFT Shield for Arduino.
// Demonstrates loading images from SD card or flash memory to the screen,
// to RAM, and how to query image file dimensions.
// Requires three BMP files in root directory of SD card:
// purple.bmp, parrot.bmp and wales.bmp.
// As written, this uses the microcontroller's SPI interface for the screen
// (not 'bitbang') and must be wired to specific pins (e.g. for Arduino Uno,
// MOSI = pin 11, MISO = 12, SCK = 13). Other pins are configurable below.

#include <Adafruit_GFX.h>         // Core graphics library
#include <Adafruit_ILI9341.h>     // Hardware-specific library
#include <SdFat.h>                // SD card & FAT filesystem library
#include <Adafruit_SPIFlash.h>    // SPI / QSPI flash library
#include <Adafruit_ImageReader.h> // Image-reading functions

// Comment out the next line to load from SPI/QSPI flash instead of SD card:
#define USE_SD_CARD

// TFT display and SD card share the hardware SPI interface, using
// 'select' pins for each to identify the active device on the bus.

#define SD_CS   49 // SD card select pin
#define TFT_RST 8
#define TFT_DC 9
#define TFT_CS 10
#define TFT_MOSI 11
#define TFT_MISO 12
#define TFT_CLK 13

#if defined(USE_SD_CARD)
  SdFat                SD;         // SD card filesystem
  Adafruit_ImageReader reader(SD); // Image-reader object, pass in SD filesys
#else
  // SPI or QSPI flash filesystem (i.e. CIRCUITPY drive)
  #if defined(__SAMD51__) || defined(NRF52840_XXAA)
    Adafruit_FlashTransport_QSPI flashTransport(PIN_QSPI_SCK, PIN_QSPI_CS,
      PIN_QSPI_IO0, PIN_QSPI_IO1, PIN_QSPI_IO2, PIN_QSPI_IO3);
  #else
    #if (SPI_INTERFACES_COUNT == 1)
      Adafruit_FlashTransport_SPI flashTransport(SS, &SPI);
    #else
      Adafruit_FlashTransport_SPI flashTransport(SS1, &SPI1);
    #endif
  #endif
  Adafruit_SPIFlash    flash(&flashTransport);
  FatFileSystem        filesys;
  Adafruit_ImageReader reader(filesys); // Image-reader, pass in flash filesys
#endif

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
Adafruit_Image       img;        // An image loaded into RAM
int32_t              width  = 0, // BMP image dimensions
                     height = 0;

void setup(void) {

  ImageReturnCode stat; // Status from image-reading functions

  Serial.begin(9600);
#if !defined(ESP32)
  while(!Serial);       // Wait for Serial Monitor before continuing
#endif

  tft.begin();          // Initialize screen

  // The Adafruit_ImageReader constructor call (above, before setup())
  // accepts an uninitialized SdFat or FatFileSystem object. This MUST
  // BE INITIALIZED before using any of the image reader functions!
  Serial.print(F("Initializing filesystem..."));
#if defined(USE_SD_CARD)
  // SD card is pretty straightforward, a single call...
  if(!SD.begin(SD_CS, SD_SCK_MHZ(25))) { // ESP32 requires 25 MHz limit
    Serial.println(F("SD begin() failed"));
    for(;;); // Fatal error, do not continue
  }
#else
  // SPI or QSPI flash requires two steps, one to access the bare flash
  // memory itself, then the second to access the filesystem within...
  if(!flash.begin()) {
    Serial.println(F("flash begin() failed"));
    for(;;);
  }
  if(!filesys.begin(&flash)) {
    Serial.println(F("filesys begin() failed"));
    for(;;);
  }
#endif
  Serial.println(F("OK!"));

  // Fill screen blue. Not a required step, this just shows that we're
  // successfully communicating with the screen.
  tft.fillScreen(ILI9341_BLUE);

  // Load full-screen BMP file 'purple.bmp' at position (0,0) (top left).
  // Notice the 'reader' object performs this, with 'tft' as an argument.
  Serial.print(F("Loading purple.bmp to screen..."));
  stat = reader.drawBMP("/purple.bmp", tft, 0, 0);
  reader.printStatus(stat);   // How'd we do?

  // Query the dimensions of image 'parrot.bmp' WITHOUT loading to screen:
  Serial.print(F("Querying parrot.bmp image size..."));
  stat = reader.bmpDimensions("/parrot.bmp", &width, &height);
  reader.printStatus(stat);   // How'd we do?
  if(stat == IMAGE_SUCCESS) { // If it worked, print image size...
    Serial.print(F("Image dimensions: "));
    Serial.print(width);
    Serial.write('x');
    Serial.println(height);
  }

  // Load small BMP 'wales.bmp' into a GFX canvas in RAM. This should fail
  // gracefully on Arduino Uno and other small devices, meaning the image
  // will not load, but this won't make the program stop or crash, it just
  // continues on without it. Should work on Arduino Mega, Zero, etc.
  Serial.print(F("Loading wales.bmp to canvas..."));
  stat = reader.loadBMP("/wales.bmp", img);
  reader.printStatus(stat); // How'd we do?

  delay(2000); // Pause 2 seconds before moving on to loop()
}

void loop() {
  for(int r=0; r<4; r++) { // For each of 4 rotations...
    tft.setRotation(r);    // Set rotation
    tft.fillScreen(0);     // and clear screen

    // Load 4 copies of the 'parrot.bmp' image to the screen, some
    // partially off screen edges to demonstrate clipping. Globals
    // 'width' and 'height' were set by bmpDimensions() call in setup().
    for(int i=0; i<4; i++) {
      reader.drawBMP("/parrot.bmp", tft,
        (tft.width()  * i / 3) - (width  / 2),
        (tft.height() * i / 3) - (height / 2));
    }

    delay(1000); // Pause 1 sec.

    // Draw 50 Welsh dragon flags in random positions. This has no effect
    // on memory-constrained boards like the Arduino Uno, where the image
    // failed to load due to insufficient RAM, but it's NOT fatal.
    for(int i=0; i<50; i++) {
      // Rather than reader.drawBMP() (which works from SD card),
      // a different function is used for RAM-resident images:
      img.draw(tft,                                    // Pass in tft object
        (int16_t)random(-img.width() , tft.width()) ,  // Horiz pos.
        (int16_t)random(-img.height(), tft.height())); // Vert pos
      // Reiterating a prior point: img.draw() does nothing and returns
      // if the image failed to load. It's unfortunate but not disastrous.
    }

    delay(2000); // Pause 2 sec.
  }
}

This is what I get when I run it.

21:44:42.624 -> Initializing filesystem...OK!
21:44:46.072 -> Loading purple.bmp to screen...File not found.
21:44:46.107 -> Querying parrot.bmp image size...File not found.
21:44:46.154 -> Loading wales.bmp to canvas...File not found.

This is for a school project due Saturday so fast responses would be appreciated. Sorry if this is inconvenient.

"File not found" is miles away from not being able to read a bitmapped file.

Please show us the names of the files using your PC.

Paul

Here is the picture of the files on my sd card. They are copied directly from the library I am using in this code.

Now show us the loading of the .txt file and lets see what's different.

-jim lee

Ok, Now put the card back in the PC and look at the attributes on any of the three of the files. Click on details. What exactly does it show as "name"? Is it exactly what your Arduino program is looking for?

Paul

I can't send you the txt file loading because I am currently in school but I can in around 8 hours.

Here is the file attached of the bmp file and its name. It matches exactly to “/purple.bmp”.

Perhaps this is the source of your problem:

Requires three BMP files in root directory of SD card:

You do not have any directories in your SD card.

Try to create a directory and move your files to the directory. If that doesn’t work, format the SD card and create a directory and then add your three files to the directory.

Paul

So would I be using SD.mkdir() to make the directory? I thought the root directory would just be the basic no folder inside of the sd card. I can't find where it even initializes the SD card. Is there a way to read and print bitmaps from the SD card just using SD.h and Adafruit_GFX.h?

Broaden your horizons by studying this discussion. There is no directory/subdirectory unless you create them.

Much more to storing and finding data that one might think.

Paul

Paul_KD7HB:
Perhaps this is the source of your problem:

Requires three BMP files in root directory of SD card:

You do not have any directories in your SD card.

Try to create a directory and move your files to the directory. If that doesn't work, format the SD card and create a directory and then add your three files to the directory.

Paul

This is incorrect. An empty FAT volume always contains the 'root directory', and adding files to the 'root directory' always means just adding them to the volume without putting them in a sub-directory.

The 'root directory' does not mean "a sub-directory with the name 'root'".

Has anyone been able to load a bitmap onto an lcd screen from an SD card? If so how maybe I can replicate that.

Problem solved. My SD card was partitioned into two separate locations and the files were in the wrong one. I did not know of the existence of the second location.

LlamaLeader:
Problem solved. My SD card was partitioned into two separate locations and the files were in the wrong one. I did not know of the existence of the second location.

Nice going! What was the clue that made the partitions visible?

Paul