Pages: [1]   Go Down
Author Topic: Using CardInfo example to find memory space of sd  (Read 722 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello to everyone I am a beginner in Arduino,

So I started doing some practice. I connected the device with an Adafruit shield and put a micro sd inside. Then I used the example cardInfo to test if it works. Now I want to find out the memory left in the sd card ( not the overall) but free memory in sd card. The problem is I don't know what command to use to take all the file sizes into one variable.

Thanks, Melis.
Logged

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

Code:
volumesize *= 1024;
  volumesize *= 1024;
  memory_left = volumesize- files ;
  Serial.println("Memory size remaining in SD card: ");
  Serial.print(memory_left);

the variable "files" is the problem . What command should I use to store the sum of the size of the files?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50902
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What command should I use to store the sum of the size of the files?
There isn't one command. You need to find all the files on the card, and get the size of each one. Add the individual sizes.
Logged

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

Yes but how do I get the individual size of each file? I think I have to use pointers but I am not sure how to do it!
Thanks by the way!
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50902
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Yes but how do I get the individual size of each file?
The size() method returns the number of bytes in the file.

Keep in mind that there is a difference between the number of bytes in the file and the number of bytes that the file makes unusable by other files on the SD card.
Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1673
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

To find the free space on an SD you need to account for files, directories, and allocation tables.

SD.h uses a very old version of SdFat and does not provide a way to do this.

New versions of SdFat have a function that returns the number of free clusters which gives total free space.

Here is an example SdFat sketch that prints the number of free clusters and the amount free space.

Code:
#include <SdFat.h>
// Default SD chip select pin.
const uint8_t sdChipSelect = SS;

// File system
SdFat sd;

void setup() {
  Serial.begin(9600);
  if (!sd.begin(sdChipSelect)) {
    Serial.println("sd.begin failed");
    while(1);
  }
  // Get the count of free clusters in the volume.
  uint32_t freeClusters = sd.vol()->freeClusterCount();
  Serial.print("Free clusters: ");
  Serial.println(freeClusters);
  
  // Calculate free space in KB.
  float freeKB = 0.512*freeClusters*sd.vol()->blocksPerCluster();
  Serial.print("Free space: ");
  Serial.print(freeKB);
  Serial.println(" KB (KB = 1000 bytes)");
}

void loop() {}
Here is the output for a 8 GB card.
Quote
Free clusters: 249037
Free space: 8160445.00 KB (KB = 1000 bytes)
Logged

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

Roger that PaulS and fat16lib I will give it a try ! If I make it work or stuck somewhere I'll post it here.

Thank you for your time guys!

Logged

Pages: [1]   Go Up
Jump to: