String dateTimeString; bool loggingOn = false; // Whether logging is turned on int sampleCount = 0; // How many samples have been logged void checkLogStart(){ if (selBtn && !loggingOn && FileOK) { loggingOn = true; sampleCount = 0; // put NTP write and read here myFile = SD.open("datalog.txt", FILE_WRITE); Serial.println(F("\nDatalog file open")); // Starting timestamp sendNTPpacket(); delay(1000); dateTimeString = receiveNTPpacket(); myFile.print("Start == "); myFile.println(dateTimeString); } } void logData(String output){ if (loggingOn) { // save last data sample myFile.println(output); // increment sample count sampleCount = sampleCount + 1; if((sampleCount % 20) == 0){ Serial.print("."); } // see if we're done if (sampleCount > (NUMOFSAMPLES - 1)) { loggingOn = false; // Stopping Timestamp sendNTPpacket(); delay(1000); dateTimeString = receiveNTPpacket(); myFile.print("Stop == "); myFile.println(dateTimeString); // Close File myFile.close(); Serial.println(F("\nDatalog file closed")); } } }