Operation counter

Hi everybody!

I am trying to make an operation counter for my project. This counter should raise one number every time arduino is awake. My program saves data to datalogger shields SD-card every time arduino awakens. Then the datalogger shield turns the power to arduino off. I figured, why not calculate how many rows I have in my text-file and use that as a operation number as counter. Well it appears that this works, when I have only couple of tens or hundreds of rows, but when the counter is going over thousand or thousands, the function is starting to take too much time counting the rows.

Here is my function:

int lineNum= 0;
#define SD_CHIPSELECT   8  //digital pin 8 is CS-pin on my datalogger
void checkline(){
  if (!SD.begin(SD_CHIPSELECT)) {
    Serial.println(F("initialization failed! (No SD card)"));
    return;
  }


  float timenow = millis();
  myFile = SD.open("LOG.TXT");
  
  if (myFile) 
  {
    if (myFile) 
    {
    while (myFile.available()) {
        char ch = myFile.read();
        if(ch == '\n')
        {
          lineNum++;
        }
    }
    // close the file:
    myFile.close();
  }
    Serial.print(F("linecounter: "));
    Serial.println(lineNum);
    Serial.print(F("Counting lasted (s): "));
    Serial.println((millis() - timenow)/1000);
  } else {
    Serial.println(F("Error writing to LOG.TXT"));
  }

  
}

My question is this: is this a wise way of making this operation counter or is there a better way? I have read something about EEPROM memory, but there is this limit of how many times you can read or write on it, right? Around 100 thousand if I understood correctly. I think my purpose will use those 100k too fast.

Any suggestions will be helpful!

Thanks in advance, V

if you can read the data in the SD card then just designate a place in the text file that you store the counter. or have a separate file that keeps track of the number or if your data logger stores one line at a time in a text file, make the first number of the line the counter. read the first number of the last line and then make the next line the counter plus one. if there is no line (blank document) start at 1.

Thanks for the reply!

vini_i: read the first number of the last line and then make the next line the counter plus one. if there is no line (blank document) start at 1.

Is there a way to go straght to the last line or do I have to read the whole file first?

Well it appears that this works, when I have only couple of tens or hundreds of rows, but when the counter is going over thousand or thousands, the function is starting to take too much time counting the rows.

Change the lineNum data type to display more lines. As an int it will only count to 32767 and then goes negative to -32768. Change to unsigned int and the value goes from 0 to 65535. Change to unsigned long and it goes from 0 to 4294967295 and if you exceed this number of lines you might have run out of storage space.

The first question you should ask yourself is, if you need to go to the end of the File. Why you dont write the counter in an extra file? I think you have enough time to open an extra file before the arduino goes back to sleep, right?

open File. read First line with Counter Counter ++ delete everything in the file write Counter in the file close the file

Hope it will help you, Basti

bwirth2: The first question you should ask yourself is, if you need to go to the end of the File. Why you dont write the counter in an extra file? I think you have enough time to open an extra file before the arduino goes back to sleep, right?

open File. read First line with Counter Counter ++ delete everything in the file write Counter in the file close the file

Hope it will help you, Basti

Yes that is what I think I'm going to do. Thanks!

Riva: Change the lineNum data type to display more lines. As an int it will only count to 32767 and then goes negative to -32768. Change to unsigned int and the value goes from 0 to 65535. Change to unsigned long and it goes from 0 to 4294967295 and if you exceed this number of lines you might have run out of storage space.

Ow, I didn't know that. I had this problem earlier in different program. Thanks for enlightening!

No Problem :wink: