void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);  Serial.println();
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

  //Activate the EEPROM before attempting to use it
  EEPROM.begin(sizeof(flag));

  //Check to see if the flag is still set from the previous boot
  if(checkFlag()) {
    //Clear the flag now it has been detected
    writeFlag(FLAGCLEAR);
    
    //Do the firmware reset here
    Serial.println("Flag Detected");
    digitalWrite(LED_BUILTIN, LOW);
  } 
  else {
    //Set the flag for detection with the next boot
    writeFlag(FLAGSET);
  }  

  // After setup, while away the end of the reset period
  while (millis() < FLAGTIMEOUT);
  writeFlag(FLAGCLEAR);

  Serial.println("Flag Checking Stopped");
}