Shop OBEX P1 Docs P2 Docs Learn Events
How can I shorten this statement? — Parallax Forums

How can I shorten this statement?

SassySassy Posts: 21
edited 2007-03-23 17:27 in BASIC Stamp
How can I shorten this statement?
IF HOURS = HOURS=$02 OR HOURS=$04 OR HOURS =$06 OR HOURS=$08 OR HOURS=$10 OR HOURS=$12 OR HOURS=$10 OR HOURS=$12 OR HOURS =$14 OR HOURS=$16 OR HOURS=$18 OR HOURS =$20 OR HOURS=$22 THEN SWT·

[noparse][[/noparse]Subject Changed]

Post Edited By Moderator (Chris Savage (Parallax)) : 3/22/2007 12:56:47 AM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-22 01:49
    It looks like you want to check for even numbered hours. I can't tell what you want to do with midnight, but I'll assume you want to include it. You'd write "IF HOURS & 1 = 0 THEN SWT". This does a bit-wise AND of the hours with a one and checks the result for zero which will occur with even hours. If you want to exclude midnight (zero hours), you'd write "IF (HOURS <> 0) AND (HOURS & 1 = 0) THEN SWT". Do note that, by putting a "$" in front of your numbers, you've made them hexadecimal instead of decimal. That may be what you want. The statement I wrote doesn't check for values like $0A through $0F or $1A through $1F or, for that matter, anything above $22.
  • Bill ChennaultBill Chennault Posts: 1,198
    edited 2007-03-22 02:04
    Sassy--

    I'm gonna act like Mike did not reply because his ideas are always better than mine. But . . .

    How about . . .

    IF Hours//2 = 0 then SWT

    I am a newbie and have never used the "//" operator, so consider that and test it. However, it should handle any even numbered hour, including midnight.

    --Bill

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    You are what you write.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-03-22 06:33
    Or, to shorten it just a tad more:

    ON hours//2 GOTO SWT ' branches only when hours is an even number
    ' continues here if hours is an odd number.

    As an aside, a math curiousity, you can test a BCD time value to see if it is evenly divisible by 2, 3 or 6 without converting it to flat binary first. For example, the BCD time value $18 would be converted to to flat binary like this...
    10 * 1 + 8 = 18
    whereas BCD value as stored in the computer memory as binary has the value,
    16 * 1 + 8 = 24
    Either way, it is divisible by 2, 3 and 6. I think that is true in general of BCD values. For example, $1701 is stored in the computer memory as 5889, and both 1701 and 5889 are evenly divisible by 3, but not 2 or 6.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • LSBLSB Posts: 175
    edited 2007-03-22 11:25
    As an aside...
    It isn't neccessary to divide the whole number by two---only the last digit. It doesn't add anything to this discussion, but it is a cool trick when testing word size variables.
    To test for /3, sum the digits and test the total.
    If it passes both the above, it's also divisible by 6

    Post Edited (LSB) : 3/22/2007 11:30:20 AM GMT
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-03-22 15:38
    Good point, it could be further shortened to,

    ON hours.bit0 GOTO SWT

    which branches only when hours is an even number. Incidentally, the ON ... GOTO .. command is the PBASIC 2.5 equivalent of the native BRANCH ...,[noparse][[/noparse]...] command, as in

    BRANCH hours.bit0,[noparse][[/noparse]SWT]

    and the tokenized form occupies minimal eeprom.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • SassySassy Posts: 21
    edited 2007-03-22 20:45
    All Thank you.....Your all so kind, I am absoulty a newbee,,,, I have been doing some reading but the more I do The more confused I get. What exactly I am trying to accomplice is to have diferent tasks, lets say every 2 hours, 3 hours, etc. Can I just use the above equation and accomplish this?
  • Bill ChennaultBill Chennault Posts: 1,198
    edited 2007-03-22 23:17
    Sassy--

    I'd say it is time you supplied more information about what you really want to do! And, another thing: I noticed that you preceded your hour numbers in your original post with the dollar sign ($). In PBASIC, this means they are hexadecimal numbers. Is that what you meant?

    For example, your $10 is actually 16 decimal. (However, everything below $10 is the same in hex as it is in decimal.) You end with $22 which would be 34 in decimal. Is this what you really mean?

    In your last post, you mentioned "3 hours." Although everyone had great ideas about how to start to answer your question, they were all (except one very clever solution)·based on the fact that you indicated an EVEN number of hours. However, unless you ABSOLUTELY MUST save every bit of space you can, CLARITY quickly outweighs brevity in programming. In other words, if capacity is not an issue, clarity of the code is paramount. The more capacity impacts HOW you write something, the trickier your code will become. Trickier is not better; it is shorter.

    More info!

    --Bill
    ps Don't give up; this is fun stuff!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    You are what you write.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-03-23 17:27
    The hex values are probably the output of a real time clock chip, like the DS1302 or DS1307. It is really binary coded decimal, in which the nibbles are a decimal digit. So $15 is 6pm, 1500 hours, but the "1" and the "5" are two separate nibbles in one byte,
    1=%0001
    5=%0101
    $15=%00010101
    The value of the whole byte as a flat binary number is 21 in decimal, as shown by:

    hours=$15
    DEBUG HEX hours, tab, DEC hours  'displays 18    21
    



    In cases where you have to have different events occur at different intervals, it is best to convert the hex value to flat binary, and then use the // operator to determine the intervals. I often have to time events to different numbers of minutes, so I convert hours and minutes to a single number that goes from 0 to 1439 as time returned from the real time clock goes from midnight to 23:59.

    julian = hours.nib1 * 10 + hours.nib0 * 6 + minutes.nib1 *10 + miinutes.nib0
    



    The julian // interval operator gives the remainder when julian is divided by interval. So when that remainder is zero, the interval is up. You can have different intervals for different processes. For example:

    ON julian//12 GOSUB interrogate   ' action taken every 12 minutes
    ON julian//120 GOSUB telemetry   ' every two hours
    



    Another factor in this kind of routine is that you often want an action to occur only once when the interval is up, not multiple times during the whole hour or whole minute. That refinement can be accomplished by means of a flag that becomes one at the top of the loop when the minute or hour first rolls over from one value to the next, but is zero for the rest of the time during the interval, until it rolls over to a new value.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.