I am making a clock on my Arduino Uno using the time library http://playground.arduino.cc/Code/Time
I let the user set a time. Which is then converted to seconds and used in the
setTime(s);
function. When the value is over 9:10h, the seconds evaluate to 4 294 941 360 seconds (= 1 193 039.27 hours)
A code snippet:
void timeChange() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Change time");
lcd.setCursor(0, 1);
lcd.print("00:00");
int hoursSet = 0; //Stores the hours added.
int minutesSet = 0; //Stores the minutes added.
String printTime;
bool timeComplete = false;
while (!timeComplete) {
int buttonVal = analogRead(A0);
if (buttonVal >= 490 && buttonVal <= 520) { //Indicates button 3 is pressed. It's function is to add one hour to the time.
hoursSet = (hoursSet + 1) % 24; //Adds one hour to the hours.
delay(100);
} else if (buttonVal >= 5 && buttonVal <= 10) { //Indicates button 4 is pressed. It's function si to add 5 minutes to the time.
minutesSet = (minutesSet + 5) % 60; //Adds minutes to the minutes.
delay(100);
}
lcd.setCursor(0,1);
String strHr = String(hoursSet);
String strMn = String(minutesSet);
if(strHr.length() == 2){
lcd.print(hoursSet);
} else if(strHr.length() == 1) {
lcd.print("0");
lcd.setCursor(1, 1);
lcd.print(hoursSet);
}
lcd.setCursor(2,1);
lcd.print(":");
lcd.setCursor(3,1);
if(strMn.length() == 2){
lcd.print(minutesSet);
} else if(strMn.length() == 1) {
lcd.print("0");
lcd.setCursor(4, 1);
lcd.print(minutesSet);
}
delay(100);
if (buttonVal >= 990 && buttonVal <= 1010) { //if button 2 is pressed again, the timecomeplete is set to true, and the script continues.
timeIsSet = true;
timeComplete = true;
}
}
lcd.clear(); //Clear the screen
lcd.setCursor(0, 0);
lcd.print("The time"); //Print text, move cursor and print the time
lcd.setCursor(0, 1);
String strHr = String(hoursSet);
String strMn = String(minutesSet);
if(strHr.length() == 2){
lcd.print(hoursSet);
} else if(strHr.length() == 1) {
lcd.print("0");
lcd.setCursor(1, 1);
lcd.print(hoursSet);
}
lcd.setCursor(2,1);
lcd.print(":");
lcd.setCursor(3,1);
if(strMn.length() == 2){
lcd.print(minutesSet);
} else if(strMn.length() == 1) {
lcd.print("0");
lcd.setCursor(4, 1);
lcd.print(minutesSet);
}
lcd.print(" is saved!");
setTime((hoursSet * 3600) + (minutesSet * 60));
timeIsSet = true;
delay(5000); //Wait five, seconds then proceed
}
Added this to the code:
Serial.print("hours ");
Serial.println(hoursSet);
Serial.print("hours in s ");
Serial.println(hoursSet * 3600);
Serial.print("minutes ");
Serial.println(minutesSet);
Serial.print("minutes in s ");
Serial.println(minutesSet * 60);
Serial.print("Combined: ");
Serial.println( (hoursSet * 3600) + (minutesSet * 60) );
When I input 5:15 it results in:
hours 5 hours in s 18000 minutes 15 minutes in s 900 Combined: 18900
When I input 9:15 it results in:
hours 9 hours in s 32400 minutes 15 minutes in s 900 Combined: -32236
So, it looks like the library only holds an Int. But that seems odd, as the function expects seconds.