you are already using interruptions (overflowCount should be volatile by the way), I've not checked the setting, but do you know how much pressure does it put on your system? (how often do you call the ISRs?)
What are you getting on Serial1 ?
if (Serial1.available() > 0)
{
String Received = Serial1.readString();
if (int(Received[0]) == 1)
are you sure you want to compare against the number 1? should that be the character '1' instead?
readString() includes also a timeout possibly, so it's not a a great function to use
using delay() and willing to have a responsive/low latency to user actions code is not compatible
void tempAndHumRead() //SHT T&H sensor reading
{
delay(2000);
==> don't use any delay() use millis() to decide when it's time to update the T° reading.
This would be a non blocking way to update the T° and log every two seconds and listen to the Serial port for the number 0 or 1 (again did you mean '0' and '1' ?)
void tempAndHumRead() //SHT T&H sensor reading
{
sht.readSample();
t = sht.getTemperature();
h = sht.getHumidity();
}
void loop() {
static uint32_t lastReadChrono = -30000; // long time ago so that first call would trigger a read
int received = Serial1.read(); // returns -1 if nothing to read
if (received == 1) digitalWrite(Led, HIGH);
else if (received == 0) digitalWrite(Led, LOW);
if (millis() - lastReadChrono > 2000) {
float frq = counter();
sendNex(frq);
tempAndHumRead();
logValue();
writeSDCard(frq);
lastReadChrono = millis();
}
}
PS:why do you declare stuff you don't use (and why are they Strings when const char* would do)?
String daysOfTheWeek[7] = { "Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado" };
String monthsNames[12] = { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" };
There is also no need to build a String before writing in the SD file, just print along
void writeSDCard(float frq) { // Function for saving the Temperature humidity and frequency to SD
DateTime now = rtc.now();
logFile = SD.open("datalog.csv", FILE_WRITE);
if (logFile) {
logFile.print(now.year()); logFile.write(';');
logFile.print(now.month()); logFile.write(';');
logFile.print(now.day()); logFile.write(';');
logFile.print(now.hour()); logFile.write(';');
logFile.print(now.minute()); logFile.write(';');
logFile.print(now.second()); logFile.write(';');
logFile.print(h * 100, 0); logFile.write(';');
logFile.print(t * 100, 0); logFile.write(';');
logFile.print(freq, 0); logFile.write(';');
logfile.println();
logFile.close();
} else {
Serial1.print(F("d3.txt=\"Error openig the file\""));
Serial1.write(0xff);
Serial1.write(0xff);
Serial1.write(0xff);
}
}
and then you can get rid of all the global variables that are useless
float h = 0.0f;
float t = 0.0f;
int currentYear = 0;
int currentMonth = 0;
int currentDay = 0;
int currentHour = 0;
int currentMinute = 0;
int currentSecond = 0;
unsigned long currentCount = 0;
and the misnamed logValue() function that was just reading the time