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! ![]()
Regards,
Kai van Veldhoven
F042017_HQ_PTS_V002.zip (3.29 KB)