Is there another way to shorten Code such as this?
Sassy
Posts: 21
IF HOURS = HOURS=$20 OR HOURS=$16 OR HOURS=$12 OR HOURS=$08 OR HOURS=$04 AND MINUTES=$00 THEN GOSUB SWT
·IF HOURS = HOURS=$23 OR HOURS=$19 OR HOURS =$15 OR HOURS=$11 OR HOURS=$07 OR HOURS=$03 AND MINUTES=$00 THEN GOSUB·TANK
·IF HOURS = HOURS=$22 OR HOURS=$18 OR HOURS =$14 OR HOURS=$10 OR HOURS=$06 OR HOURS=$02 AND MINUTES=$00 THEN GOSUB·MED
·IF HOURS = HOURS=$21 OR HOURS=$17 OR HOURS =$13 OR HOURS=$09 OR HOURS=$05 OR HOURS=$01 AND MINUTES=$00 THEN GOSUB·FILL
·GOSUB ODD
·RUN 0
Sassy
·IF HOURS = HOURS=$23 OR HOURS=$19 OR HOURS =$15 OR HOURS=$11 OR HOURS=$07 OR HOURS=$03 AND MINUTES=$00 THEN GOSUB·TANK
·IF HOURS = HOURS=$22 OR HOURS=$18 OR HOURS =$14 OR HOURS=$10 OR HOURS=$06 OR HOURS=$02 AND MINUTES=$00 THEN GOSUB·MED
·IF HOURS = HOURS=$21 OR HOURS=$17 OR HOURS =$13 OR HOURS=$09 OR HOURS=$05 OR HOURS=$01 AND MINUTES=$00 THEN GOSUB·FILL
·GOSUB ODD
·RUN 0
Sassy
Comments
In particular, you should read up on BRANCH and // to understand what the above code does.
-Phil
Post Edited (Phil Pilgrim (PhiPi)) : 12/6/2007 11:51:13 PM GMT
Do you mean something like this (note my change below: "IF HOURS = HOURS-20) . . .
IF HOURS = HOURS-20 OR HOURS=16 OR HOURS=12 OR HOURS=08 OR HOURS=04 AND MINUTES=00 THEN GOSUB SWT
or something like this (note my change below: "IF HOURS = HOURS or hours = 20") . . .
IF HOURS = HOURS or hours = 20 OR HOURS=16 OR HOURS=12 OR HOURS=08 OR HOURS=04 AND MINUTES=00 THEN GOSUB SWT
--Bill
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You are what you write.
Oh oh! The dollar sign (indicating a hex constant) might be correct if these data are being read from a realtime clock that uses BCD. I overlooked that possibility, and my code is wrong if the dollar sign is meant to be there. Let me know it that's the case, so I can revise my code.
-Phil
I interpreted the dollar sign differently, but I do not know which is right in Sassy's context. I did not think she was intending hex. I said "standalone symbol" but that is not what I meant. What I really meant is that there is no monetary symbol in PBasic. And, I THINK she is talking money, but I just ain't sure.
--Bill
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You are what you write.
Sometimes it is best to create a debug code sample that uses all of the input values and simply shows the formatted or sorted output for inspection to eliminate any guessing as to what a piece of code will do. This table leaves no doubt as to what values will branch to in the branch command.
Add the $ to 01 to 23 in the for next loop and HEX instead of DEC if you want to see the hex values $01 .. $23
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
Good code.
Aren't we still assuming we know what she means by "IF HOURS = HOURS="?
It is not SAFE to figure that it is merely a newbie-coder mistake. I think it is though and she means nothing else. But, we should know her intent.
--Bill
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You are what you write.
This is real good code here i have learn something here
Thanks for sharing this
Sassy
Are you using the DS1302 Time Chip Demo
If so it would· be this
IF hrs = $20 OR hrs=$16 OR hrs=$12 OR hrs=$08 OR hrs=$04 AND mins=$00 THEN
GOSUB SWT
ENDIF
But what··· ·metron9···· ·alot neater way to do this
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
Post Edited (sam_sam_sam) : 12/7/2007 3:15:21 AM GMT
I think it is safe to assume the OP means to compare hours with each of the variables in the if then construct. As Phil notes though, his code would not work with numbers 0 - 35 ($0..$23 hex) So·I think it is also safe to assume that we have 24 hours in a day and the OP has used 1..23 Since the variable minutes is also used I think that is clear.
So it boils down to the constructs used to test the hours variable with 24 numbers.
The following code handles any Byte value number given and runs the required routine.
If indeed Hours is in BCD format then just add a bdc to byte conversion to HOURS before the lookup in the real program.
·Just for fun i added the BCD to decimal code and error message if BCD is not valid
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
Post Edited (metron9) : 12/7/2007 4:21:50 AM GMT
-Phil
Just thought I'd throw that out there.
Merry Christmas and everyone's code and explanations were better than mine, anyway! [noparse]:)[/noparse]
--Bill
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You are what you write.
Yes it is BCD Format, I am trying to get Alarms to activate lights at specific times...Still a little confused about what happens on Hours> then 20, as I see it does not error out or function in the program
Sassy
If you get a BCD number format of $20 you can directly compare it with $20. Your code originally is fine except for the "If hours = hours =$20" Just remove one of the "Hours = "
If hours = $20 or hours = $21 then goto label
Is fine. The basic reason for the conversion when using the lookup table is to save space. As you can see the number 10 in decimal is $0A in HEX so it is bumped up to 16 decimal or $10 in HEX the lookup table could very well use the HEX number value. The numbers 10 through 15 (in hex $$0A thru $0F) would just be set to the number 4 in the code I posted and would function the same giving an error for the non valid BCD numbers. The number $23 is 35 in decimal so there would be 12 additional bytes in the table that would be set to the number 4 so the branch statement would send the program path to the error label.
Again, not sure what you mean. The code handles· $21, $22, and $23 and branches to swt, med and tank correctly. Your code originally compares hours with $21 and branches to fill: as well ast $22 and $23.
The hex number $1A covnverts to the decimal number 20 in my code because the 1 in $1A represents 10 in BCD then the A converts to then decimal number 10 is added to it and becomes 20. It is not a valid BCD number (the $1A) for the decimal number 20 because $20 is the correct BCD format. The if then line checks the 4 bit high and low nibbles for numbers above 9 (10 through 15) or in hex ($A through $F) and outputs an error message.
Yes after the error message "Invalid BCD number input!" is displayed the code falls through to the next line that performs the branch to fill,med,tank,swt and error. you could change this to
IF (h & 15) > 9 OR (h>>4) > 9 THEN goto invalidbcd
and add a label and the code that outputs the message and then returns to the main loop after the branch statement.
invalidbcd:
·DEBUG "Invalid BCD number input! "
·goto fnloop
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
You still haven't explained what's supposed to happen when HOURS = 0, but here's my modified code for BCD (hex) notation, based as closely as I can estimate from your original code:
The additional hours & $30 >> 3 is to correct for the rollovers from $09 to $10 and from $19 to $20.
-Phil
What I want to know though Phil, are you a binary wizard or did you learn that one along the way? I wish I could see math in my head as clear as I can see logic but for some reason I don't. I never would have come up with that one. Nice code. I have learned two new tricks from this thread now. Thanks.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
Once I knew that, I modified it until I ended up with this one, in which the remainders progress regularly 0,1,2,3,0,1,2,3, ... :
-Phil
Sassy
Thanks Phil for Posting this
I have learned something my self watching this post
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam