Shop OBEX P1 Docs P2 Docs Learn Events
Simplifying IF..THEN based code — Parallax Forums

Simplifying IF..THEN based code

ArnoNLArnoNL Posts: 15
edited 2009-01-07 21:01 in BASIC Stamp
Hello all,

I'm trying to simplify some code based on the IF...THEN statement. I·thought the branch and Select...case instructions·are possible alternatives...generally. Looking at my code I see a possibility to replace it with the select...case instruction, however this will not save me any space nor time. Therefore I started with the branch instruction.·However as this instruction seems to rely·on simple whole-number variables/constants I·don't see how I can·use branch in stead of the if...then·instructions in my code.·Could·somebody·please have a look at my code and tell me what direction I should go, or maybe if he/she·comes to the same conclusion as I did, that is.. that it's not possible to use some other instruction·for the·if...then statements for this particular code (due to the use of input data together with the outcome of a statement). You're help is really appreciated.

Kind regards Arno (from the Netherlands)
Find the code below, as one can se it's a subroutine, I have 5 more in my program all comparable to this one.

'---subroutine2---
Forward2:
fw2=fw2+1
IF fw2>=120 THEN
GOSUB standing
ENDIF
IF IN4^IN5=1 THEN
GOSUB standing
ENDIF
DEBUG DEC fw2,CR
IF IN1 THEN Forward2
IF fw2 <60 AND IN0=1 THEN
DEBUG "spd1",CR
ELSEIF fw2 <60 AND IN3=1 THEN
DEBUG "spd2",CR
ELSEIF fw2<75>=60 AND IN0=1 THEN
DEBUG "spd3",CR
ELSEIF fw2<75>=60 AND IN3=1 THEN
DEBUG "spd4",CR
ELSEIF fw2<90>=75 AND IN0=1 THEN
DEBUG "spd5",CR
ELSEIF fw2<90>=75 AND IN3=1 THEN
DEBUG "spd6",CR
ELSEIF fw2<120>=90 AND IN0=1 THEN
DEBUG "spd7",CR
ELSEIF fw2<120>=90 AND IN3=1 THEN
DEBUG "spd8",CR
ENDIF
fw2=0
GOTO main··

Comments

  • metron9metron9 Posts: 1,100
    edited 2008-11-11 05:55
    Not sure what you wanted to do if IN0 and IN3 both equal 0
    Used a for next loop to simulate fw2 minimum and maximum values

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' {$PORT COM1}
    fw2 VAR Byte
    spd VAR Byte
    IN0test VAR Bit      'Variables used for test run
    IN3test VAR Bit
    IN0test=1
    IN3test=0
     
    main:
    FOR fw2=0 TO 120
    spd=7
    IF fw2<120 THEN spd=7
    IF fw2<90 THEN spd=5
    IF fw2<75 THEN spd=3
    IF fw2<60 THEN spd=1
    IF IN0test=1 THEN GOTO donespd
    IF IN3test=1 THEN spd=spd+1
    donespd:
    DEBUG "spd",DEC spd, "   fw2=",DEC fw2,CR
    NEXT
    
     
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2008-11-11 06:36
    LOOKDOWN can help with this kind of problem...

    x VAR NIB  ' a helper variable
    LOOKDOWN fw2, <[noparse][[/noparse]60, 75, 90, 120], x   ' note the < conditional, so x comes out as 0,1,2,or 3
    x = x * 2 + 49    ' now x is an ascii code, "1", "3", "5", "7"
    IF in0 THEN
      DEBUG "spd",x    ' print 1,3,5 or 7
    ELSEIF in3 THEN
      DEBUG "spd",x+1   ' or 2,4,6 or 8
    ENDIF
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-11-11 14:49
    Ah, that Tracy Allen is a GENIUS! We'd be lost without him.
  • ArnoNLArnoNL Posts: 15
    edited 2008-11-24 09:05
    Hello Tracy,

    I tried your suggested code and have tried all kinds of combinations to get this into my code . However it doesn't work like it should. It even does some strange things. What happens is that when I run the code the debug screen neer shows any SPD1, SPD2, etc. With my old code the debug screen showed something like:
    1
    2
    3
    4
    5
    6
    ..
    ..
    66
    67
    spd1··········· <= this is how it should be it counts until I hit an IN0, IN2, IN3, etc.
    1·················<=then starts over again
    2
    3
    ..
    ..
    93
    94
    spd6

    This is how it should go. But with your code it goes something like:
    1
    2
    3
    ...
    10
    11
    12
    13d········ <= why the D???
    24········· <=counting by 11???
    35
    46
    57
    68
    79

    OR

    1
    2
    3
    ..
    ..
    10
    11
    12
    13d····· <= always 13d and/or 14d sometimes "normal" 13 o 14 very strange
    101····· <=sometimes it jumps 30, 40, 50 counts
    102
    103
    1
    2
    sp3······ <=sometimes it shows sp and than a number

    so it looks like a bug or something but I encounter this with 2 different BS2's and also with my BSpx
    I really can't figure out what's happening? I hope somebody knows what's going on.

    Thank you in advance,

    Arno (the Netherlands)
    ·
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2008-11-24 17:26
    Hi Arno,

    It is hard to figure without your complete code. All I can see is your first try subroutine and a snippet that I suggested to cover part of that subroutine.

    @allenlane, hehe, : smurf.gif. Us Allens want to unionize, huh?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • ArnoNLArnoNL Posts: 15
    edited 2008-12-31 12:54
    Thanks Tracy!

    It worked, I got it working. Thank you very much for your valuable help.
    By the way why did you use 49 in the equation x = x * 2 + 49, any number will do... or not?

    Have a great new year all!

    Greetings, Arno
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-01-01 19:33
    You're welcome, I'm glad you got it working! The formula, x = x*2+49, turns a number in the set x:{0,1,2,3} into an ascii code in the set x:{49,51,53,55} that will then print as "1","3","5","7" when sent to the debug terminal screen. You just have to understand that 49 is the ascii code for numeral "1".

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • ArnoNLArnoNL Posts: 15
    edited 2009-01-07 21:01
    I see, thanks again, it all works as planned!

    Kind regards
Sign In or Register to comment.