Go Down

Topic: I don't know why it stops after three hours. (Read 864 times) previous topic - next topic

narutorobot

Code: [Select]

#include <DueTimer.h>
#include <SD.h>

void setup(){
  Timer0.attachInterrupt(CHECK_SDCARD).start(5000000);
}

void CHECK_SDCARD(){
  if (!SD.begin(chipSelect)) {

  }
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {

  }
  if (!volume.init(card)) {

  }

  uint32_t volumesize;

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize /= 2;                           // SD card blocks are always 512 bytes (2 blocks are 1KB)

  root = SD.open("/");
  float all_files_size = printDirectory(root, 0);
  //SerialUSB.println(all_files_size);
  bool removeFlag = false;
  if(all_files_size > ((float)volumesize-15000000)){ //
   removeFlag = true;
  }
  if(removeFlag){
    target_num = checkMin(ename_int, 10);
    memset(ename_int, 0, sizeof(ename_int));
    target_num = 0;
  }
  Flag08 = true;
}

inline float printDirectory(File dir, int numTabs) {
  float SD_LIMIT_SIZE = 0;
  byte i = 0;
  while (true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      break;
    }

    String ename = String(entry.name());

    if(ename.substring(4,8).toInt() - setvalue >= 0){
       ename_int[i] = ename.substring(0,8).toInt();
       i++;
    }
    

    if (entry.isDirectory()) {
      printDirectory(entry, numTabs + 1);
    } else {
      SD_LIMIT_SIZE = SD_LIMIT_SIZE + (double)entry.size()/1024.0;
    }
    entry.close();
  }
  return SD_LIMIT_SIZE;
}

uint32_t checkMin(uint32_t * array, int size)
{
  uint32_t minimum = array[0];
  for (int i = 0; i < size; i++)
  {
    if (array[i] == 0){
      
    }else if (array[i] < minimum) minimum = array[i];
  }
  return minimum;
}




My Arduino Due check the SDCARD capacity every 5 seconds
When all file sizes exceeded 15GB, the delete flag was enabled.
But exactly this code stops after about three hours.
(It works with other code.)
I thought it was other code problem at first, but if I remove the code above
It worked normally for 24 hours.

I'm currently using the SD basic library
But, I will change to sdfat library.

It's my final goal
my module will create a folder like ..
201908/201909/201910 ..... / 202001/202002 ...
And If it does not have enough space, it will delete the oldest folder.


please, give advice.

Delta_G

Is it wise to do that from an interrupt context?  I think I would just watch millis and call the function every five seconds before I added extra complexity of a library and an interrupt. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Go Up