How can I shorten this statement?
Sassy
Posts: 21
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
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
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.
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
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
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
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.
1=%0001
5=%0101
$15=%00010101
The value of the whole byte as a flat binary number is 21 in decimal, as shown by:
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.
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:
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