Shop OBEX P1 Docs P2 Docs Learn Events
Sparkfun Clock module time comparisson? — Parallax Forums

Sparkfun Clock module time comparisson?

qxoticqxotic Posts: 47
edited 2011-12-05 12:54 in BASIC Stamp
Hello,

For my project the clock is working fine. I am using the Real Time Clock Module from Spark Fun which I believe uses the DS1307. I am working with just the hours in my problem. It displays properly when you tell it to display Hex2 clock(2) as a decimal number on the screen.

The problem is, since the clock keeps the number in hex I am not sure how to go about using it in an IF statement for evaluation.

I want to say If clock(2) > 7 and clock(2) <20 Then......

The problem is because it is in hex I am having to use some very odd numbers to ballpark it at the moment... such as 5 and 34 instead of 7 and 20. (7 am and 8 pm).

How do I change clock(2) to a decimal for my IF statement evaluation?

Thank you,
Daniel

Comments

  • FranklinFranklin Posts: 4,747
    edited 2011-12-02 17:01
    I think (and may be wrong as I usually am) the chip outputs in "binary coded decimal" which is different than regular hex. Check the datasheet.
  • qxoticqxotic Posts: 47
    edited 2011-12-05 12:28
    Thank you Franklin for the information. Using Hex2 for the display was making me assume it was hex. I did a search and read several threads on the problem.

    Currently I have
    ELSEIF clock(2)>5 AND clock(2)<34 THEN
    GOSUB ValidCode 'code to compare hour of day.. I want the code to only be valid between 7am and 10 pm.. using 24hr clock

    DEBUG HEX2 clock(2), ":", HEX2 clock(1), ":", HEX2 clock(0), CR 'this displays the correct time.

    I have two ideas to try from what I read...
    1st..
    ELSEIF clock(2)>$06 AND clock(2)<$22 THEN

    2nd idea...have to add one variable called HrsDec
    HrsDec = (clock(2).NIB1 * 10) + clock(2).NIB0
    ELSEIF HrsDec > 6 AND HrsDec < 22 THEN

    I will try this on the stamp in operation in the next day or so... would both of these work? If not can you please elaborate as to why?

    Thank you,
    Daniel
  • ZootZoot Posts: 2,227
    edited 2011-12-05 12:33
    Your second example is correct. BTW, you do not need to "save" HrsDec as its own variable if you don't want to -- you can put in the whole IF/THEN expression:
    IF ( clock(2) >> 4 * 10 ) + ( clock(2) & $F )  >= 7 AND ( clock(2) >> 4 * 10 ) + ( clock(2) & $F ) < 22 THEN 
      ' do here only from 07:00 to 21:59, inclusive
    ENDIF
    
  • qxoticqxotic Posts: 47
    edited 2011-12-05 12:54
    Thanks Zoot I believe that is exactly what I was looking for. If I have any problems when I get to test it I'll check back.

    Thanks to all,
    Daniel
Sign In or Register to comment.