I am having an issue with a simple clock program I am writing for my ArduinoMKRZERO. The entire program runs as expected when connected to the Arduino serial monitor, but I am trying to implement a headless mode so that I can start the Arduino without having it connected to a PC.
I have set a simple timeout for waiting for a serial connection, and a boolean that gets set when the timeout expires so that any serial commands are skipped. Anytime I test the program without the serial monitor connected, it hangs indefinitely as soon as (I assume) the timeout expires. After this hang, the Arduino IDE hangs whenever I attempt to use serial communication. Presumably because of the MKR's Serial port being busy or hung.
I cannot for the life of me figure out what could be causing this, and hope that you can help. Relevant sections of code follow:
bool head = true;
void setup() {
DEBUG_SERIAL.begin(115200); //Start Debug serial
unsigned long start = millis();
while(!DEBUG_SERIAL) //Wait until the serial port is opened with a 5 second timeout
{
if (millis() - start > 5000){
head = false;
break;
}
}
if( head == true ){
if(!Clock.begin()) {
DEBUG_SERIAL.println("Couldn't find RTC!");
DEBUG_SERIAL.flush();
while (1) delay(10);
}
DEBUG_SERIAL.print("RTC Time: "); //Print current RTC time
DEBUG_SERIAL.print(Clock.now().hour());
DEBUG_SERIAL.print(":");
DEBUG_SERIAL.print(Clock.now().minute());
DEBUG_SERIAL.print(":");
DEBUG_SERIAL.println(Clock.now().second());
DEBUG_SERIAL.println("Set RTC Clock, enter invalid times to skip");
DEBUG_SERIAL.print("Enter Hour (0-23): ");
while (!DEBUG_SERIAL.available());
int hour = DEBUG_SERIAL.parseInt();
Serial.read();
DEBUG_SERIAL.println(hour);
DEBUG_SERIAL.print("Enter Minute (0-59): ");
while (!DEBUG_SERIAL.available());
int minute = DEBUG_SERIAL.parseInt();
Serial.read();
DEBUG_SERIAL.println(minute);
DEBUG_SERIAL.print("Enter Second (0-59): ");
while (!DEBUG_SERIAL.available());
int second = DEBUG_SERIAL.parseInt();
Serial.read();
DEBUG_SERIAL.println(second);
DateTime set = DateTime(2022, 1, 1, hour, minute, second);
if(set.isValid())
Clock.adjust(set);
DEBUG_SERIAL.print("RTC Time Set: "); //Print current RTC time
DEBUG_SERIAL.print(Clock.now().hour());
DEBUG_SERIAL.print(":");
DEBUG_SERIAL.print(Clock.now().minute());
DEBUG_SERIAL.print(":");
DEBUG_SERIAL.println(Clock.now().second());
}