Skip to content

Instantly share code, notes, and snippets.

@DreamerDeLy
Created February 18, 2019 16:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DreamerDeLy/18490e0afa554c786428e272ca581bb7 to your computer and use it in GitHub Desktop.
Save DreamerDeLy/18490e0afa554c786428e272ca581bb7 to your computer and use it in GitHub Desktop.
Configuring RTC time from Serial Port
void setTime() {
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
DateTime now = rtc.now();
int newYear;
int newMonth;
int newDay;
int newHour;
int newMinute;
int newSecond;
Serial.println(" > Enter time (!TddMMyyyyhhmmss)");
while (! (Serial.available() > 0));
String time_string = Serial.readString();
newDay = time_string.substring(2, 4).toInt();
newMonth = time_string.substring(4, 6).toInt();
newYear = time_string.substring(6, 10).toInt();
newHour = time_string.substring(10, 12).toInt();
newMinute = time_string.substring(12, 14).toInt();
newSecond = time_string.substring(14, 16).toInt();
Serial.print(" > New time - ");
Serial.print(newYear, DEC);
Serial.print('/');
Serial.print(newMonth, DEC);
Serial.print('/');
Serial.print(newDay, DEC);
Serial.print(' ');
Serial.print(newHour, DEC);
Serial.print(':');
Serial.print(newMinute, DEC);
Serial.print(':');
Serial.print(newSecond, DEC);
Serial.println();
Serial.println(" > Change time? (Y/N)");
while (! (Serial.available() > 0));
if (Serial.readString() == "Y") {
rtc.adjust(DateTime(newYear, newMonth, newDay, newHour, newMinute, newSecond));
Serial.print(" > Time set to ");
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.println(" Sucsesfull!");
} else {
Serial.println(" > Canceled");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment