if (startCycleTime == 0) {
...
...
}
else if (currentCycleTime - startCycleTime > 60000) {
startCycleTime = 0;
}
If you want to switch off after 60 seconds, you can add it to the else if block.
There is also a bug in the flow of the software. Imagine that somewhere along the line the readings are NAN. You exit loop() end the next time you do not read the sensors so the value is still NAN but now startDataTime is not zero so you continue in loop() and display the NAN data.
Your code is also growing out of hand; a rule of thumb is that a function (loop() in this case) should basically fit on a page of A4 paper if you print it.
You need to consider the use of functions to break your code in bite-sized chunks. You can e.g. take the Serial prints that display the DHT results out of loop and place them in a function.
/*
print DHT values to serial port for debugging
in:
humidity, temp C, temp F, heat index C, heat index F
*/
void debugDHT(float h, float t, float f, float hic, float hif)
{
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
}
and for the displaying on the LCD
/*
print DHT values on lcd
in:
humidity, temp C, temp F
*/
void displayDHT(float h, float t, float f)
{
lcd.setBacklight(1);
lcd.home();
lcd.print("Temperature");
lcd.setCursor(0, 1);
lcd.print(t);
lcd.print("*C");
lcd.setCursor(0, 2);
lcd.print("Humidity");
lcd.setCursor(0, 3);
lcd.print(h);
lcd.print("*%");
}
You can place those two functions anywhere
...
...
void setup()
{
...
...
}
void loop()
{
...
...
}
void debugDHT(...)
{
...
...
}
void displayDHT(...)
{
...
...
}
but the correct way in C/C++ is te declare them before the are first used, so
...
...
void debugDHT(...)
{
...
...
}
void displayDHT(...)
{
...
...
}
void setup()
{
...
...
}
void loop()
{
...
...
debugDHT(h, t, f, hic, hif)
...
...
displayDHT(h, t, f)
...
...
}
You can also write a function to log to the SD card; note that the reading of the sensors (on the analog pins) and the writing to the SD card are different functionalities; so reading and building the string in loop and next a call to your new function that logs.
Lastly I suspect a bug in the logging part of your code; have you tested it?