Hello folks,
I'm making a project where I need to calculate the media between 5 minutes of a HR-S04 sensor, and if that media is below a determinated value, it will trigger a relay that it should be in HIGH state while the media between the last minute is below other value. To prevent constant changes on switching relay state I want to use the millis function, but I already use that one to reduce the point that are saved in a microSD further on the code. I wonder if that the use of the millis() function will lead to errors regarding their different purposes. I don't have the equipment with me to test it, so I'm asking for your advice.
#define MEASURE_SAMPLE_DELAY 50
#define MEASURE_SAMPLES 6000
#define MEASURE_DELAY 250
#define MEASURE_SAMPLE_DELAY1min 50
#define MEASURE_SAMPLES1min 1200
#define MEASURE_DELAY1min 250
...
//interval for SDCARD writing
const int interval = 5000;
unsigned long previousTime = 0;
//interval for the cicle where that relay state can be changed
const int intervalM = 600000;
unsigned long previousTimeM = 0;
ong singleMeasurement()
{
long duration = 0;
// Measure: Put up Trigger...
digitalWrite(TRIGGER_PIN, HIGH);
// ... wait for 11 µs ...
delayMicroseconds(11);
// ... put the trigger down ...
digitalWrite(TRIGGER_PIN, LOW);
// ... and wait for the echo ...
duration = pulseIn(ECHO_PIN, HIGH);
// Serial.print("Distancia imediata= ");
// Serial.print(((float) duration / USONIC_DIV));
// Serial.println(" mm");
return (long) (((float) duration / USONIC_DIV));
}
//Funções media de 5min
long measure()
{
long measureSum = 0;
for (int i = 0; i < MEASURE_SAMPLES; i++)
{
delay(MEASURE_SAMPLE_DELAY);
measureSum += singleMeasurement();
}
return measureSum / MEASURE_SAMPLES; //media 5 minutes
}
//Funções para media de 1 min
long measure1min()
{
long measureSum1min = 0;
for (int i = 0; i < MEASURE_SAMPLES1min; i++)
{
delay(MEASURE_SAMPLE_DELAY1min);
measureSum1min += singleMeasurement();
}
return measureSum1min / MEASURE_SAMPLES1min; //media 1 minute
}
//function for change the relay's state
unsigned long currentTimeM = millis();
if (currentTimeM - previousTimeM >= intervalM) {
if (v >= 300) { //if the pH is greater than or equal to 7.0
digitalWrite(relay, LOW); //print "high" this is demonstrating that the Arduino is evaluating the pH as a number and not as a string
rele = 0;
}
if (v < 300) { //if the pH is less than or equal to 6.99
do {
digitalWrite(relay, HIGH);
rele = 1;
}
while {v1 < 300};
}
previousTimeM = currentTimeM;
}
// function for writing on sdcard every 5 sec
unsigned long currentTime = millis();
if (currentTime - previousTime >= interval) {
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("TempDCDT.csv", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
//Serial.print("Writing to fa.txt...");
myFile.print(temperature);
myFile.print(";");
myFile.print(v);
myFile.print(";");
myFile.print(v1);
myFile.print(";");
myFile.print(releState);
myFile.print(";");
myFile.print(val_a);
myFile.print(";");
myFile.print(hora);
myFile.print(";");
myFile.println(data);
// close the file:
myFile.close();
//Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("SD error");
}
previousTime = currentTime;
}
Thanks in advance!