Pages: [1]   Go Down
Author Topic: SD card logger shield, can read card ok - but I can't delete or write file ?  (Read 2397 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using Deek Robot Data logging shield v1.0 

I am confused because I can read the directory from the SD card without any problems, but I can not write or delete a file which for sure exists on the SD card.

Below is image of the actual card


I attach my sketch code below.

Many Thanks
Les


Code:
#include <SD.h>


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

File myFile;

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

  if (!card.init(SPI_HALF_SPEED, 10, 11, 12, 13)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card is 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);
 
  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);
 
 
 // to this point everything works fine and a directory listing is displayed
 
 
 //   Not sure why but this code below fails to write file example.txt
 
  myFile = SD.open("example.txt", FILE_WRITE);
  myFile.close();

  // Check to see if the file exists:
  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  }
  else {
    Serial.println("example.txt doesn't exist."); 
  }


 //   Not sure why but this code below fails to delete the file which exists on SD card
  Serial.println("Removing P1010085.JPG...");
  SD.remove("P1010085.JPG");

  if (SD.exists("P1010085.JPG")){
    Serial.println("P1010085.JPG exists.");
  }
  else {
    Serial.println("P1010085.JPG doesn't exist."); 
  }
}


void loop(void) {
}


* Capture.JPG (30.84 KB, 279x336 - viewed 131 times.)
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1273
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is the card in read-only mode?
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have tried the SD card in my camera and it is not set to read only mode as files can still be stored onto it.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



I attach Serial monitor output.


* Capture2.JPG (80.3 KB, 567x701 - viewed 271 times.)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have also just tried to re-format card using SDFormatter but that has made no difference.
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1273
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
//   Not sure why but this code below fails to delete the file which exists on SD card
  Serial.println("Removing P1010085.JPG...");
  SD.remove("P1010085.JPG");

  if (SD.exists("P1010085.JPG")){
    Serial.println("P1010085.JPG exists.");
  }
  else {
    Serial.println("P1010085.JPG doesn't exist."); 
  }
So is the problem that the file P1010085.JPG is still on the card after the code has run?

The software would appear to be consistent - you may not be able to remove() the file if it cannot recognise the name, which would appear to be the case if the file cannot be found. Otherwise the message is correct (you delete the file and then it cannot be found).

Not sure where to go from here as I don't use SD cards that much and they have always worked for me.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you take a look at the attached serial monitor screen grab.
You will see that the file named in the sketch to be deleted does exist, but it is not deleted for some reason.
It is shown in the directory listing before that segment of code executes
 
Similarly creating a new file fails.

Unexplainable reading the directory works fine  yet write and delete do not.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, Not sure if the image of the data logger is yours or one from the internet.
If it is yours and you are still having problems note the battery is in the wrong way around.
Normally it should not affect the card but, a lack of voltage on one of its pins maybe the cause of you not being able to write to the card.

Hope it helps. D
Logged

0
Offline Offline
Edison Member
*
Karma: 64
Posts: 1639
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can't mix the code that uses card, volume and root
Code:
Sd2Card card;
SdVolume volume;
SdFile root;

With code that uses SD

Code:
SD.remove("P1010085.JPG");

  if (SD.exists("P1010085.JPG")){
    Serial.println("P1010085.JPG exists.");
  }

SD has it's own instance of card, volume, and root.  This means there would be two cache buffers if you had initialized SD with an SD.begin() call. 

Fortunately you didn't call SD.begin() because even more mysterious things would have happened.
Logged

Pages: [1]   Go Up
Jump to: