Data log system with interrupt

Hey Folks,

So I am building this data logging system which writes values 2 values to an sd card every 5 minutes. Basically it has a millis based timer in the loop:

void loop()
{
  currentMillis = millis();
  if(currentMillis - previousMillis >= interval)
  {
    //Do stuff
    previousMillis = currentMillis;
    
    takeSample();
  }
}

This is whats inside the takeSample() function:

oid takeSample(void)
{
  sampleCount++;

  if(sampleCount < 10) sampleCountSTR = "S-000" + String(sampleCount);
  else if(sampleCount < 100) sampleCountSTR = "S-00" + String(sampleCount);
  else if(sampleCount < 1000) sampleCountSTR = "S-0" + String(sampleCount);
  else if(sampleCount < 10000) sampleCountSTR = "S-" + String(sampleCount);
  else sampleCountSTR = "OVRLD";
  
  //Read all inputs and time FIRST before writing to SD card due to possible hickups and timing inconsistencies with writing to sd
  //Read values into variables
  int inW = digitalRead(waterIN);
  int inD = digitalRead(ductIN);

  updateTimeStrings();
  //Concatenate the strings into date and time
  date = day + "/" + month + "/" + year;
  time = hour + ":" + minute + ":" + second;

  dataString = date + "\t" + sampleCountSTR + "\t" + time + "\t" + String(inW) + "\t" + String(inD);
  Serial.println(dataString);
  
  File dataFile = SD.open(getName(stdFileName,"_",(month + "_" + year),stdExtension), FILE_WRITE);
  if(dataFile)
  {
    
    if(prevDay != day)
    {
      dataFile.print("\n");
      dataFile.print(stdHeaderString);
      prevDay = day;
    }

    dataFile.println(dataString);

    dataFile.close(); //Always close up the file after writing to it or writing will not actually occur.
  }
}

Now everytime the takeSample function runs (every 5 minutes), it does a digital read on pin 2 and 3.
So a logfile is gonna look like this: (look at the time)

DATE SAMPLE NR. TIME WATER GAUGE DELIVERY PIPE
19-4-2017 S-0001 23:05:19 0 0
19-4-2017 S-0002 23:10:19 1 0
19-4-2017 S-0003 23:15:19 0 1

This works fine as is.
Now what my customer wants is that in between those 5 minutes whenever a HIGH occurs on one of the pins, this also gets recorded. Those random pulses can occur at any time. Then the logfile would have to become something like this: (look at the time again)

DATE SAMPLE NR. TIME WATER GAUGE DELIVERY PIPE
19-4-2017 S-0001 23:05:19 0 0
19-4-2017 S-0002 23:10:19 1 0
19-4-2017 S-0003 23:11:25 1 0
19-4-2017 S-0004 23:13:53 0 1
19-4-2017 S-0005 23:15:19 0 1

Now I thought I'd just use an interrupt service routine at that pin which runs an interrupt function when the signal is RISING. And in that function the takeSample() function gets called once:

void ISR1(void)
{
  Serial.println("ISR1");
  takeSample();
}

void ISR2(void)
{
  Serial.println("ISR2");
  takeSample();
}

This doesn't work at all however, I don't know what the problem is.. Does it have anything to do with the millis timer in the loop? Or am I using the interrupt wrong?
I attached the full code.

I hope someone can help me out! :slight_smile:

Regards,

Kai van Veldhoven

F042017_HQ_PTS_V002.zip (3.29 KB)

You cannot print from within an interrupt routine, because printing depends on interrupts and they turned are off.

Interrupts should be short, fast and set a global flag (declared "volatile") to tell the main program that something is to be done.

void ISR2(void)
{
  Serial.println("ISR2");

You can't write to the SD card in an interrupt service routine, either.

You are uselessly pissing away resources using Strings to manipulate the data to be written to the file. Writing to the file is buffered, so make several calls to write data to the buffer. It is NOT faster to make one call to write 100 characters to the buffer than it is to make 10 calls that write 10 characters each. It is, in fact, faster to not manipulate Strings.

Hi PaulS,

I figured out the interrupt issue.

I am however a layman when it comes to programming. All the knowledge I have, I've obtained by learning it myself and trial and error. Could you maybe give me a little example on what you mean with the string manipulation and file writing?

Could you maybe give me a little example on what you mean with the string manipulation and file writing?

You could:

  int a = 3;
  int b = 14;
  int c = 27;

  String A(a);
  String B(b);
  String C(c);

  String ABC = A + ", " + B + ", " + C;
  File dataFile = SD.open(getName(stdFileName,"_",(month + "_" + year),stdExtension), FILE_WRITE);
  if(dataFile)
  {
     dataFile.print(ABC);

     dataFile.close();
  }

or, you could:

  int a = 3;
  int b = 14;
  int c = 27;

  File dataFile = SD.open(getName(stdFileName,"_",(month + "_" + year),stdExtension), FILE_WRITE);
  if(dataFile)
  {
     dataFile.print(a);
     dataFile.print(", ");
     dataFile.print(b);
     dataFile.print(", ");
     dataFile.print(c);

     dataFile.close();
  }

The file will contain exactly the same data in both cases, but the sketch will be a lot smaller in the second case, and there will be no memory fragmentation.