-->
Page 1 of 1

Three problems in same code - SD.remove and a Serial error?

PostPosted: Thu May 09, 2019 2:25 pm
by Bonzo
I have posted some test code I am using below and the output. I have three problems which are possibly linked:
1/ The file is not deleted with SD.remove()
2/ I am getting random serial.prints from other switch statements. It was suggested to me this was a serial/buffer error but no way on how to fix it was suggested.
3/ Not printing the contents of the file

Can anyone shed some light on the problems?

Code: Select all#include <SPI.h>
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = D2;

String dataFile = "TESTDATA.TXT";

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  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.");
  }
}

void loop() {
  if (Serial.available() > 0) {
    char inByte = Serial.read();
    Serial.println("inByte = " + inByte);
    switch (inByte) {
      case 'D': { // Delete the data file
          Serial.println("Look for a file called " + dataFile + ", if found remove it");
          if (SD.exists(dataFile)) {
            Serial.println("File " + dataFile + " found");
            if (SD.remove(dataFile)) Serial.println("File successfully deleted");
            else Serial.println("Error - file not deleted");
          }
          if (!SD.exists(dataFile)); {
            Serial.println("Following confirmation checks, " + dataFile + " now deleted\r\n");
          }
          break;
        }
      case 'I': { // Display card data
         
          // 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(); 
          volumesize *= volume.clusterCount();
          volumesize *= 512;                           
          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);

          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);
          break;
        }
      case 'P': {// Print the file to serial
          Serial.println("Open a file called " + dataFile + " and read it");
          File testfile = SD.open(dataFile, FILE_READ);
          while (testfile.available()) {
            Serial.write(testfile.read());
          }
          Serial.println("\r\nCompleted reading from the file\r\n");
          testfile.close();
          break;
        }
      default:
        Serial.println("No command recived");
    }
  }
}

Some comments added here proceeded by *****
Initializing SD card...Wiring is correct and a card is present. *****FROM START UP AND OK

*****INPUT D INTO THE SERIAL MONITOR

found
Look for a file called TESTDATA.TXT, if found remove it
Following confirmation checks, TESTDATA.TXT now deleted


No command recived *****THIS IS THE OUTPUT FROM THE DEFAULT SWITCH COMMAND

*****INPUT I INTO THE SERIAL MONITOR WOOPS TOOK SOMETHING I SHOULDN'T HAVE OUT OF THE CODE BUT YOU SHOULD GET THE IDEA - THE FILE I WANTED TO DELETE IS STILL ON THE SD CARD
d

Volume type is FAT0

Volume size (bytes): 0
Volume size (Kbytes): 0
Volume size (Mbytes): 0

Files found on the card (name, date and size in bytes):

No command recived

*****INPUT P INTO THE SERIAL MONITOR

successfully deleted
Open a file called TESTDATA.TXT and read it

Completed reading from the file


No command recived