Pages: [1]   Go Down
Author Topic: DS1307 how to handle roll from 0 to 59 in software  (Read 778 times)
0 Members and 1 Guest are viewing this topic.
Central Indiana, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 203
So many projects, so little time...and money!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
          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)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50452
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How is minute defined? If it's a signed value, that code should work.

By the way,
Code:
minute = minute - 1;
is usually written:
Code:
minute--;
Logged

Boston, MA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1024
wiblocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 *)
« Last Edit: July 12, 2010, 06:54:33 am by jluciani » Logged


Central Indiana, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 203
So many projects, so little time...and money!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 87
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you could also use mod (%)

Code:
minute = (minute - 1) % 60
minute = (minute + 1) % 60
« Last Edit: July 12, 2010, 07:05:53 am by squiggler » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50452
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: July 12, 2010, 07:17:37 am by PaulS » Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 7
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
you could also use mod (%)
Code:
minute = (minute - 1) % 60

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

Regards,

Dave
« Last Edit: July 12, 2010, 08:17:03 am by davekw7x » Logged

Central Indiana, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 203
So many projects, so little time...and money!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should be able to roll the minutes downwards using

Code:
minutes = (minutes+59)%60;
Logged

Pages: [1]   Go Up
Jump to: