Shop OBEX P1 Docs P2 Docs Learn Events
Is there another way to shorten Code such as this? — Parallax Forums

Is there another way to shorten Code such as this?

SassySassy Posts: 21
edited 2007-12-10 17:38 in BASIC Stamp
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

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-12-06 23:46
    I think this is equivalent to what you wrote, although I'm not sure what you wrote is what you meant. smile.gif ("HOURS = HOURS" ? And what's really supposed to happen when HOURS = 0?)

    IF (minutes = 0 AND hours <> 0) THEN GOSUB doit
    GOSUB odd
    RUN 0
    
    doit:
      BRANCH hours // 4, [noparse][[/noparse]swt, fill, med, tank]
    
    
    


    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
  • Bill ChennaultBill Chennault Posts: 1,198
    edited 2007-12-06 23:57
    sassy--
    IF HOURS = HOURS=$20 OR HOURS=$16 OR HOURS=$12 OR HOURS=$08 OR HOURS=$04 AND MINUTES=$00 THEN GOSUB SWT
    The first problem you have is syntax "if hours = hours = " is incorrect. Additionally, the dollar sign ($) is incorrect. No such standalone symbol exists in PBasic. Do you mean money or something else?

    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.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-12-07 00:09
    Bill, Sassy,

    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
  • Bill ChennaultBill Chennault Posts: 1,198
    edited 2007-12-07 00:16
    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.
  • metron9metron9 Posts: 1,100
    edited 2007-12-07 00:55
    This will show you a listing of the branches made for the values 0 .. 23 using Phil's code

    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

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    HOURS VAR Byte
    MINUTES VAR Byte
    TEMP VAR Byte
    X VAR Byte
    
    FOR X = 1 TO 4
    DEBUG CR
    FOR HOURS = 01 TO 23
     BRANCH HOURS // 4, [noparse][[/noparse]SWT,FILL,MED,TANK]
    BRANCHRETURN:
    NEXT
    NEXT
    END
     
    FILL:
    IF X = 1  THEN
     DEBUG DEC HOURS," = FILL ",CR
    ENDIF
    GOTO BRANCHRETURN
    MED:
    IF X = 2  THEN
     DEBUG DEC HOURS," = MED ",CR
    ENDIF
    GOTO BRANCHRETURN
    TANK:
    IF X = 3  THEN
     DEBUG DEC HOURS," = TANK ",CR
    ENDIF
    GOTO BRANCHRETURN
    SWT:
    IF X = 4  THEN
     DEBUG DEC HOURS," = SWT ",CR
    ENDIF
    GOTO BRANCHRETURN
    




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • Bill ChennaultBill Chennault Posts: 1,198
    edited 2007-12-07 01:03
    metron9--

    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.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-12-07 03:02
    metron9

    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·idea.gif·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
  • metron9metron9 Posts: 1,100
    edited 2007-12-07 03:49
    As you can see the code if hours = hours = $20 is never a true statement for any byte value so the code is simply nonsense even though it does not generate a syntax error.


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    HOURS VAR Byte
    X VAR Byte
    
    hours = $20
    FOR x = 0 TO 255
     IF hours = hours = x THEN GOSUB yes ELSE GOSUB no
    NEXT
    
    END
    yes:
    DEBUG DEC x, " Yes",CR
    RETURN
    no:
    DEBUG DEC x, " No",CR
    RETURN
    

    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.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    HOURS VAR Byte
    X VAR Byte
    FOR hours = 0 TO 24
      x=4 'lookup uses this number or rather leaves this number in x if the index is out of range
      LOOKUP hours, [noparse][[/noparse]4,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2],x 'The first value of 4 is used if hours = 0
      BRANCH x , [noparse][[/noparse] fill, med, tank, swt,error] 'I have added the label error: to the branch statement
      error:
      IF hours = 0 THEN DEBUG "Hours <1",CR
      IF hours > 23 THEN DEBUG "Hours > 23",CR
      fnloop:
    NEXT
    END
      fill:
      DEBUG DEC hours," fill",CR
      GOTO fnloop
      med:
      DEBUG DEC hours," med",CR
      GOTO fnloop
      tank:
      DEBUG DEC hours," tank",CR
      GOTO fnloop
      swt:
      DEBUG DEC hours," swt",CR
      GOTO fnloop
    

    ·Just for fun i added the BCD to decimal code and error message if BCD is not valid
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    HOURS VAR Byte
    X VAR Byte
    h VAR Byte
     
     
    
    FOR h = $0 TO $99
      x=4
      hours = ((h>>4)*10)+(h & 15)
      LOOKUP hours, [noparse][[/noparse]4,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2],x
      DEBUG "Input=$", HEX h , " Converted=", DEC hours, " "
      IF (h & 15) > 9 OR (h>>4) > 9 THEN DEBUG "Invalid BCD number input! "
      BRANCH x , [noparse][[/noparse] fill, med, tank, swt,error]
      error:
      IF hours = 0 THEN DEBUG "Hours <1",CR
      IF hours > 23 THEN DEBUG "Hours > 23",CR
      fnloop:
    NEXT
    END
      fill:
      DEBUG DEC hours," fill",CR
      GOTO fnloop
      med:
      DEBUG DEC hours," med",CR
      GOTO fnloop
      tank:
      DEBUG DEC hours," tank",CR
      GOTO fnloop
      swt:
      DEBUG DEC hours," swt",CR
      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!

    Post Edited (metron9) : 12/7/2007 4:21:50 AM GMT
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-12-07 03:53
    Well, if it is a DS1302, which uses BCD, then the hexadecimal notation is correct, and my program needs fixing. But I'll wait until Sassy responds...

    -Phil
  • Rory StormRory Storm Banned Posts: 16
    edited 2007-12-07 04:06
    So "we" know that Sassy is a chick, Bill?
    Just thought I'd throw that out there.
  • Bill ChennaultBill Chennault Posts: 1,198
    edited 2007-12-07 14:33
    Rory--
    So "we" know that Sassy is a chick, Bill?
    No. Stereotyping on my part. Bad lapse in common sense. Thanks for calling me on it.

    Merry Christmas and everyone's code and explanations were better than mine, anyway! [noparse]:)[/noparse]
    --Bill

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    You are what you write.
  • SassySassy Posts: 21
    edited 2007-12-08 12:03
    Thank You all for your help.

    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
  • SassySassy Posts: 21
    edited 2007-12-08 12:14
    How would you state the equivalent to Hours=$20, Hours=$21 Etc without using the look up table...Can I add a specific value to $20 to accomplish this?
  • metron9metron9 Posts: 1,100
    edited 2007-12-08 17:06
    Sassy said...
    How would you state the equivalent to Hours=$20, Hours=$21 Etc without using the look up table...Can I add a specific value to $20 to accomplish this?
    Could you restate the question, not sure I understand your question.

    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.
    Still a little confused about what happens on Hours> then 20, as I see it does not error out or function in the program
    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!
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-12-08 19:37
    Sassy,

    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:

    IF (minutes = 0 AND hours <> 0) THEN GOSUB doit
    GOSUB odd
    RUN 0
    
    doit:
      BRANCH hours + (hours & $30 >> 3) // 4, [noparse][[/noparse]swt, fill, med, tank]
    
    swt:
      ...
      RETURN
    
    fill:
      ...
      RETURN
    
    med:
      ...
      RETURN
    
    tank:
      ...
      RETURN
    
    odd:
      ...
      RETURN
    
    
    



    The additional hours & $30 >> 3 is to correct for the rollovers from $09 to $10 and from $19 to $20.

    -Phil
  • metron9metron9 Posts: 1,100
    edited 2007-12-09 06:18
    "The additional hours & $30 >> 3 is to correct for the rollovers from $09 to $10 and from $19 to $20."


    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!
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-12-09 06:35
    Nah, no wizardry. In fact, a little trial and error. I started with this program, just to see how the remainders behaved at the rollover points:

    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    
    FOR B0 = $00 TO $23
      IF (B0 & $0F < 10) THEN
        DEBUG HEX2 B0, " ", DEC B0 // 4, CR
      ENDIF
    NEXT
    
    
    


    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, ... :

    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    
    FOR B0 = $00 TO $23
      IF (B0 & $0F < 10) THEN
        DEBUG HEX2 B0, " ", DEC B0 + (B0 & $30 >> 3) // 4, CR
      ENDIF
    NEXT
    
    
    



    -Phil
  • SassySassy Posts: 21
    edited 2007-12-09 11:04
    Thank You Phil, I finally got it.

    Sassy
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-12-10 17:38
    Phil

    Thanks Phil for Posting this

    I have learned something my self watching this post

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam
Sign In or Register to comment.