Go Down

### Topic: DS1307 how to handle roll from 0 to 59 in software (Read 1 time)previous topic - next topic

#### flyboy

##### Jul 12, 2010, 01:10 pm
I've been writing a routine for my DS1307 to set the time.  Dates are easy since you don't ever have a day or month that equals 0.  When you are dealing with 24hour time, you will have a 0 hour and every hour you will have 00 minutes.

My question is, when adjusting your time and you get to 0, how do you get it to roll to 59 mins or 23 hours?

The -1 I tried didn't work...so I need some guidance.

Code: [Select]
`           if (dec == LOW )           //If I press the decriment button, begin reducing the minute value           {              if(minute > -1)              {                 minute = (minute-1);                 if(minute == -1)                    {                        minute = 59;                    }                //end of if statement if(minute == -1)                  }                      //end of if statement if(minute>1)              lcd.clear();           }                         //end of if statement if(dec == low)           if (inc == LOW)           //If I press the incriment button, begin increasing the minute value           {              if(minute <59)              {                 minute = (minute + 1);                    if(minute == 59)                        {                            minute = 0;                        }             //end of if statement if(minute == 59)              }                       //end of if statement if(minute<59)              lcd.clear();           }                          //end of if statement if(inc==low)`

#### PaulS

#1
##### Jul 12, 2010, 01:53 pm
How is minute defined? If it's a signed value, that code should work.

By the way,
Code: [Select]
`minute = minute - 1;`
is usually written:
Code: [Select]
`minute--;`

#### jluciani

#2
##### Jul 12, 2010, 01:53 pmLast Edit: Jul 12, 2010, 01:54 pm by jluciani Reason: 1
For doing math on times it is easier to convert the time and date to epoch seconds,
do the math and then covert epoch seconds back to time/date. It is a lot easier
than dealing with various carries between secs, mins, hours.

The code you posted did not include the carries for hours or seconds.
But you would need to handle that as well. In your code you have
minute > -1 which looks like it is always true.

I would code the block like this --

Code: [Select]
`if (dec == LOW) {   if (minute == 0) minute = 59;   else minute--;   lcd.clear();}if (inc == LOW) {  if (minute == 59) minute = 0;  else minute++;  lcd.clear();}`

(* jcl *)

#### flyboy

#3
##### Jul 12, 2010, 02:05 pm
In order to avoid dealing with the carries from seconds to minutes to hours, I'm only changing one value at a time.  This application shows you the time and date and then asks if you want to make changes.  I am using a switch statement to go to change the hours value.  Next I go to change the minutes, etc.  I haven't graduated to having the whole thing on the screen in real time and then moving the cursor to each value and changing it that way.  That will be a later project.

Regarding the minutes value, I've defined minutes as an INT.  The -1 should work, but for some reason, it doesn't seem to.  When I get to 0, it rolls to 255.

#### squiggler

#4
##### Jul 12, 2010, 02:05 pmLast Edit: Jul 12, 2010, 02:05 pm by squiggler Reason: 1
you could also use mod (%)

Code: [Select]
`minute = (minute - 1) % 60minute = (minute + 1) % 60`

#### PaulS

#5
##### Jul 12, 2010, 02:16 pmLast Edit: Jul 12, 2010, 02:17 pm by PaulS Reason: 1
Quote
I've defined minutes as an INT.  The -1 should work, but for some reason, it doesn't seem to.  When I get to 0, it rolls to 255.

This behavior is what I would expect if minute is defined as a byte, not as an int. Can you post all of your code?

#### davekw7x

#6
##### Jul 12, 2010, 02:57 pmLast Edit: Jul 12, 2010, 03:17 pm by davekw7x Reason: 1
Quote
you could also use mod (%)
Code: [Select]
`minute = (minute - 1) % 60`

Won't work: (-1) % 60 is equal to -1, not 59

Regards,

Dave

#### flyboy

#7
##### Jul 12, 2010, 03:12 pm
You're right.  It is a byte, not int.  I didn't look closely enough.  I used the SparkFun code from here: http://wiring.org.co/learning/libraries/realtimeclock.html

That should answer my question.  It's usually the little things that trip us up.  I should be able to say that

if(minute == 255)
{
minute = 59;
}

As it should roll from 0 to 255 when decrimenting the minute value.  This should fix my problem, I believe.

#### agib

#8
##### Jul 15, 2010, 03:30 pm
You should be able to roll the minutes downwards using

Code: [Select]
`minutes = (minutes+59)%60;`

Go Up