Created
February 18, 2019 16:35
-
-
Save DreamerDeLy/18490e0afa554c786428e272ca581bb7 to your computer and use it in GitHub Desktop.
Configuring RTC time from Serial Port
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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