Im currently tring arduino C and trying to figure out what the best way to convert the given added values into the correct seconds minutes and hours.Im aware this needs division and modulo its just im not sure where. how would i go about doing this?
this is the code
int swimhours=0;
int swimmins=50;
int swimsecs=59;
int bikehours=2;
int bikemins=50;
int bikesecs=59;
int runhours=1;
int runmins=50;
int runsecs=59;
int totalhours=swimhours+bikehours+runhours;
int totalmins=swimmins+bikemins+runmins;
int totalsecs=swimsecs+runsecs+bikesecs;`
i need the values of the above to be added(which is the total) and then i need the conversion in the complete variables
>Solution :
The "trick" is to work in seconds.
These are durations, so we don’t have to worry about time zones or Daylight-Savings Time, etc. Good.
First, convert everything to seconds.
swimmins += swimhours * 60;
swimsecs += swimmins * 60;
bikemins += bikehours * 60;
bikesecs += bikemins * 60;
runmins += runhours * 60;
runsecs += runmins * 60;
Then, find the total number of seconds.
int totalsecs = swimsecs + bikesecs + runsecs;
Finally, find convert back to hours, minutes and seconds.
int totalmins = totalsecs / 60;
totalsecs %= 60;
int totalhours = totalmins / 60;
totalmins %= 60;