SD Library cant detect file but displays it!

Hi

I'm, using the follwoing example code which is mostly based on the SD "Dump Info" sketch:

void initSD() {
  SPI.setDataMode(SPI_MODE0);       //change SPI MODE to work with SD

  Serial.print("\nInitializing SD card...");

    // we'll use the initialization code from the utility libraries
    // since we're just testing if the card is working!
    if (!card.init(SPI_HALF_SPEED, chipSelect)) {
      Serial.println("initialization failed. Things to check:");
      Serial.println("* is a card inserted?");
      Serial.println("* is your wiring correct?");
      Serial.println("* did you change the chipSelect pin to match your shield or module?");
      return;
    } else {
      Serial.println("Wiring is correct and a card is present.");
    }

    // print the type of card
    Serial.print("\nCard type: ");
    switch (card.type()) {
      case SD_CARD_TYPE_SD1:
        Serial.println("SD1");
        break;
      case SD_CARD_TYPE_SD2:
        Serial.println("SD2");
        break;
      case SD_CARD_TYPE_SDHC:
        Serial.println("SDHC");
        break;
      default:
        Serial.println("Unknown");
    }

    // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
    if (!volume.init(card)) {
      Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
      return;
    }


    // print the type and size of the first FAT-type volume
    uint32_t volumesize;
    Serial.print("\nVolume type is FAT");
    Serial.println(volume.fatType(), DEC);
    Serial.println();

    volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
    volumesize *= volume.clusterCount();       // we'll have a lot of clusters
    volumesize *= 512;                            // SD card blocks are always 512 bytes
    Serial.print("Volume size (bytes): ");
    Serial.println(volumesize);
    Serial.print("Volume size (Kbytes): ");
    volumesize /= 1024;
    Serial.println(volumesize);
    Serial.print("Volume size (Mbytes): ");
    volumesize /= 1024;
    Serial.println(volumesize);


    if(!SD.exists("singleLogs.CSV")){
    				Serial.println("FIle missing!");
    				singleLog = SD.open("singleLogs.CSV",FILE_WRITE);
     				singleLog.close();
    			}

    if(SD.exists("singleLogs.CSV")){
    				Serial.println("FIle there!!");
    				singleLog = SD.open("singleLogs.CSV");
    				singleLog.println("HI!");
    				singleLog.close();
    			}


    Serial.println("\nFiles found on the card (name, date and size in bytes): ");
    root.openRoot(volume);

    // list all files in the card with date and size
    root.ls(LS_R | LS_DATE | LS_SIZE);





SPI.setDataMode(SPI_MODE3);

}

I added the part where it checks if a file is there, if not, it would create it. But the Code is telling me always that the file is missing! And it does not create it!

I tried the following scenarios

  • Insert SD Card and run sketch (it should create file now)
  • Reset circuit, File was still reported missing
  • Plug SD back to Computer to check if file created = no
  • Create File on PC and plug SD back to Circuit
  • Reset circuit, File was still reported missing by (SD.exist)
  • File Shows up in the root.ls!

Here is the dump from the sketch:

Initializing SD card...Wiring is correct and a card is present.

Card type: SD2

Volume type is FAT16

Volume size (bytes): 2007629824
Volume size (Kbytes): 1960576
Volume size (Mbytes): 1914
FIle missing!

Files found on the card (name, date and size in bytes): 
SYSTEM~1/     2017-08-28 17:29:52
  INDEXE~1      2017-08-28 17:29:52 76
  WPSETT~1.DAT  2017-08-28 17:32:24 12
SINGLE~1.CSV  2018-05-21 19:20:28 0

I also tried to switch the file ending to lower case but that did not change anything.

What could be wrong here

(deleted)

spycatcher2k:
The SD id limited to 8.3 naming format - change the name to this and retry.

This might be a problem, but I've trying "TEST.TXT" now, but it still does not work.

(deleted)

spycatcher2k:
OK - post the FULL code, including any changes you made. As well as the output from the example supplied with the library.

The code is up in the first post, i just changed "singleLogs.CSV" to "TEST.TXT"

The actual write command does also not work (if i comment out the if-exist). Also tried another SD, no luck.

The code is up in the first post,

That is not the FULL code, and it isn't the code you are currently testing.

Post the FULL code, with setup() and loop(), as requested.

(deleted)