@@ -24,6 +24,10 @@ const uint32_t LOG_INTERVAL_USEC = 2000;
2424
2525// Set USE_RTC nonzero for file timestamps.
2626// RAM use will be marginal on Uno with RTClib.
27+ // 0 - RTC not used
28+ // 1 - DS1307
29+ // 2 - DS3231
30+ // 3 - PCF8523
2731#define USE_RTC 0
2832#if USE_RTC
2933#include " RTClib.h"
@@ -151,8 +155,15 @@ file_t csvFile;
151155char binName[] = " ExFatLogger00.bin" ;
152156// ------------------------------------------------------------------------------
153157#if USE_RTC
158+ #if USE_RTC == 1
154159RTC_DS1307 rtc;
155-
160+ #elif USE_RTC == 2
161+ RTC_DS3231 rtc;
162+ #elif USE_RTC == 3
163+ RTC_PCF8523 rtc;
164+ #else // USE_RTC == type
165+ #error USE_RTC type not implemented.
166+ #endif // USE_RTC == type
156167// Call back for file timestamps. Only called for file create and sync().
157168void dateTime (uint16_t * date, uint16_t * time, uint8_t * ms10) {
158169 DateTime now = rtc.now ();
@@ -211,6 +222,15 @@ void binaryToCsv() {
211222 Serial.print (0.001 *(millis () - t0));
212223 Serial.println (F (" Seconds" ));
213224}
225+ // ------------------------------------------------------------------------------
226+ void clearSerialInput () {
227+ uint32_t m = micros ();
228+ do {
229+ if (Serial.read () >= 0 ) {
230+ m = micros ();
231+ }
232+ } while (micros () - m < 10000 );
233+ }
214234// -------------------------------------------------------------------------------
215235void createBinFile () {
216236 binFile.close ();
@@ -261,7 +281,7 @@ bool createCsvFile() {
261281 if (!csvFile.open (csvName, O_WRONLY | O_CREAT | O_TRUNC)) {
262282 error (" open csvFile failed" );
263283 }
264- serialClearInput ();
284+ clearSerialInput ();
265285 Serial.print (F (" Writing: " ));
266286 Serial.print (csvName);
267287 Serial.println (F (" - type any character to stop" ));
@@ -289,7 +309,7 @@ void logData() {
289309 if (binFile.write (fifoBuf, 512 ) != 512 ) {
290310 error (" write first sector failed" );
291311 }
292- serialClearInput ();
312+ clearSerialInput ();
293313 Serial.println (F (" Type any character to stop" ));
294314
295315 // Wait until SD is not busy.
@@ -397,7 +417,7 @@ void logData() {
397417// ------------------------------------------------------------------------------
398418void openBinFile () {
399419 char name[FILE_NAME_DIM];
400- serialClearInput ();
420+ clearSerialInput ();
401421 Serial.println (F (" Enter file name" ));
402422 if (!serialReadLine (name, sizeof (name))) {
403423 return ;
@@ -425,7 +445,7 @@ void printData() {
425445 if (!binFile.seekSet (512 )) {
426446 error (" seek failed" );
427447 }
428- serialClearInput ();
448+ clearSerialInput ();
429449 Serial.println (F (" type any character to stop\n " ));
430450 delay (1000 );
431451 printRecord (&Serial, nullptr );
@@ -439,16 +459,10 @@ void printData() {
439459}
440460// ------------------------------------------------------------------------------
441461void printUnusedStack () {
442- #if HAS_UNUSED_STACK
462+ #if HAS_UNUSED_STACK
443463 Serial.print (F (" \n Unused stack: " ));
444464 Serial.println (UnusedStack ());
445- #endif // HAS_UNUSED_STACK
446- }
447- // ------------------------------------------------------------------------------
448- void serialClearInput () {
449- do {
450- delay (10 );
451- } while (Serial.read () >= 0 );
465+ #endif // HAS_UNUSED_STACK
452466}
453467// ------------------------------------------------------------------------------
454468bool serialReadLine (char * str, size_t size) {
@@ -476,7 +490,7 @@ void testSensor() {
476490 const uint32_t interval = 200000 ;
477491 int32_t diff;
478492 data_t data;
479- serialClearInput ();
493+ clearSerialInput ();
480494 Serial.println (F (" \n Testing - type any character to stop\n " ));
481495 delay (1000 );
482496 printRecord (&Serial, nullptr );
@@ -538,7 +552,7 @@ void setup() {
538552void loop () {
539553 printUnusedStack ();
540554 // Read any Serial data.
541- serialClearInput ();
555+ clearSerialInput ();
542556
543557 if (ERROR_LED_PIN >= 0 ) {
544558 digitalWrite (ERROR_LED_PIN, LOW);
0 commit comments