Go Down

Topic: SD card writing/reading (Read 350 times) previous topic - next topic

Crebsyn

So I have my program working but I am having issues with writing to the SD card and possibly reading. I have a couter running and want to write to the SD card every 20 counts. The issue is as the count gets larger the cycle time to write to the card increases too much and I will miss a cycle (I want to be under 120milliseconds to write to it). The SD card is a class 10. Is there a better way of doing this?
Code: [Select]
if ((RaceMode > 0) && (cycles % 20 == 0) && (cycles > 1)) {
    FileWrite ();
    }
    if ((RaceMode > 1) && (cycles % 20 == 1)) {
    FileRead ();
    }
void FileWrite() {

  char fileName[16];
  sprintf(fileName, "/%d/%04d.txt", RecDir, cycles);
  //Serial.println(fileName);
  myFile1 = SD.open(fileName, FILE_WRITE);
  if (myFile1) {
  TempTime1 = millis();
  if (cycles % 40 == 0) {
  Avgspeed = (1440000*rim/(((TempTime1)-(MenuTime + StartTime))-AvgTime));
  //Serial.println(Avgspeed);
  //Serial.print(" Avgspeed");
  //Serial.println ("  ");
  AvgTime = (TempTime1)-(MenuTime + StartTime);
  //Serial.print(TempTime);
  //Serial.println(" TempTime");
  myFile1.println(Avgspeed);
  }
  else {
    Timerec = ((TempTime1 -(MenuTime + StartTime)) /100);
   //Serial.println ("  ");
   //Serial.println(Timerec);
   //Serial.println(" Time Rec");
   //Serial.print(StartTime);
   //Serial.println (" StartTime");
   //Serial.print (MenuTime);
   //Serial.println (" Total Menu Time");
   //Serial.println (" ");
   myFile1.println(Timerec);
  }
  myFile1.close();
  //Serial.println("done.");
  }
  //serialTimer = millis();
  //Serial.println ("              Write");
  Debounce = 1;
}



void FileRead() {
  //Serial.println("R");
  if (cycles == 1) {
    B = (40);
  }
  else if (cycles % 40 == 1) {
    B = (cycles + 39);
  }
else {
   B = (cycles - 1);
}
//Serial.print(B);
//Serial.println("  B");
  char AvgSR[15];
  sprintf(AvgSR, "/%d/%05d.txt", ReadDir, B);
  filedata = 0;
  File myFile1 = SD.open(AvgSR);
    while (true)
        {
        if (myFile1.available() == 0)
            break;
        digit = myFile1.read();
        if (digit == '\n' || digit == '\r')
            break;
        filedata = filedata * 10 + (digit - '0');
        }
  myFile1.close();
  if (B % 40 == 0) {
  totalavg = filedata;
  //Serial.println ("  ");
  //Serial.print (totalavg);
  //Serial.println ("  totalavg");
  //Serial.println ("  ");
  }
  else {
    timeavg = filedata;
    //Serial.println ("  ");
    //Serial.print (timeavg);
    //Serial.println ("  timeavg");
    //Serial.println ("  ");
    //TimeCompareText();
  }
  Debounce = 1;
}

snax01

Your best bet would be to collect the data ever 20cycles and store it in an array...after a number of cycles then write the entire array.  It has to do with how an SD card works, they are slow at writing small bits of data, fastest writing times are when you write an entire block of data at once...I think its 512bytes but Im prob wrong so you should do some research on that.

Go Up