Shop OBEX P1 Docs P2 Docs Learn Events
Re Help me before i go mad please — Parallax Forums

Re Help me before i go mad please

Kurt InglesonKurt Ingleson Posts: 2
edited 2009-01-31 03:40 in BASIC Stamp
I am trying to get my bs2 with boe board to read the value of 4 pins at once , and depending on their state set another pin either high or low. however i cant get it to work, for example

if pin1 = 1 and pin2 = 0 and pin3 = 0 and pin4 = 0 then goto (etc etc) but then on another line
if pin1 = 0 and pin2 = 1 and pin3 = 0 and pin4 = 0 then goto ( another command)

and so on and so on, can this even be done and if so how can i get it to work, ie examples of code etc etc


thanxs for your help guys , i am new to the bs2 so please go easy on me hehe

·

Comments

  • metron9metron9 Posts: 1,100
    edited 2009-01-28 16:45
    Think about using pins 0..3 and use the memory variables INS (see BASIC Stamp 2 Series RAM Organization in the basic stamp help Memory Organization and Variables)

    That way you can branch if the nibble (INA for example representing pins 0,1,2 and 3) that make up a number from 0 to 15

    branch if INA = 4 for example would mean pin 3 is high, If INA = 15 then all 4 pins are high.

    That way you eliminate lots of code. Using lookup in combination with branching can also be done to reduce code size.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • Kurt InglesonKurt Ingleson Posts: 2
    edited 2009-01-28 19:52
    Ah so it can be done thats good, although i am a beginner and dont really understand, is there any chance you could post or send me some sample code?

    my email is kurt.ingleson@virgin.net

    or is there any existing i could modify on the net , thanxs for your help so far can you help a bit more


    thanxs thanxs
  • metron9metron9 Posts: 1,100
    edited 2009-01-28 21:16
    Go to the HELP on the menu, look up the things i said. Then try some code, start with one button. Simply output the value of INA in both decimal and hex (see the debug command in the help as well)

    You can't go through life using other peoples code, it will do you no good to plug in snippets of code without a full understanding of what it dose or how it works. Once you understand fully what a simple thing like this is, you will be off and running with all kinds of code.

    Start with a main label and a goto main. In between make use of the debug command to see what is happening when the code is run using just one thing that does nothing but communicate back to you information that shows you what is happening.

    The time you are taking to find other peoples code, you could have experimented and figured out how it works by now.

    Just DO IT. Post the code you write if you have trouble with it and I am sure people here will point out mistakes. Mistakes are what burn in the learning.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • metron9metron9 Posts: 1,100
    edited 2009-01-29 18:34
    Well, how is it going? Let's get this problem solved, whip up some code and let's give it a whirl.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-01-30 03:43
    metron9

    I am not trying to hijack this post

    I want to understand how you do this

    Think about using pins 0..3 and use the memory variables INS (see BASIC Stamp 2 Series RAM Organization in the basic stamp help Memory Organization and Variables)

    That way you can branch if the nibble (INA for example representing pins 0,1,2 and 3) that make up a number from 0 to 15

    branch if INA = 4 for example would mean pin 3 is high, If INA = 15 then all 4 pins are high.

    That way you eliminate lots of code. Using lookup in combination with branching can also be done to reduce code size.


    I know that if you want to see a pin state that you use this

    INPUT 7······························ ' Make P7 an input
    · DEBUG "State of P7: ",
    ······· BIN1 IN7, CR


    Would I use this to·see the state of·Pins 0-3

    State· VAR· INS····


    ·
    DEBUG "State of INA: ",
    ······· BIN1 INA, CR




    If this is wrong then would you·· smile.gif

    Please give me an example how you would see the state pins 0-3

    I have no way of trying this now to see if this would work or not to night

    with a Basic·Stamp





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

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 1/30/2009 3:49:38 AM GMT
  • MrBi11MrBi11 Posts: 117
    edited 2009-01-30 05:31
    sSs said...

    DEBUG "State of INA: ",
    BIN1 INA, CR

    Almost!!

    BIN1 will only give you one digit.. use BIN4


      DEBUG "State of INA: ",
            BIN4 INA, CR
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Smile ... It increases your face value!
  • metron9metron9 Posts: 1,100
    edited 2009-01-30 06:06
    Sorry I thought the lookup or lookdown commands did branching, they do not.

    The BRANCH command is what you want.

    I put in a few other things but the point here is to copy the pin states to a variable and use that variable in your logic.
    Remember a pin can change state within a command if you are operating on it's value.

    There is no branch that will do a gosub so I put a return address under the branch statement so program flow can return to the statement after the branch statement. A psudo gosub command.

    Hope this helps.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' {$PORT COM1}
    

    MYFLAG VAR Bit
    MY_INA VAR Nib    'CREATE A PLACE TO STORE THE STATE OF THE PINS
    

    MYFLAG=0
    

    MAIN:
    

    MY_INA=INA        'STORE STATE OF PINS WANT TO MAKE SURE THEY DONT CHANGE WHEN WE DO LOGIC ON THEM
    

    'USING THE IF THEN
    IF MY_INA=0 THEN GOTO ALLPINSZERO
    IF MY_INA=7 THEN GOTO PINS012SET
    

    'USING BRANCH
    BRANCH MY_INA, [noparse][[/noparse]ALLPINSZERO,TWO,THREE,FOUR,FIVE,SIX,PINS012SET,EIGHT]
    BRANCHRETURN:
    

    PAUSE 1000
    

    IF MYFLAG=0 THEN
    DEBUG "ALL OTHER COMBINATIONS "
    GOSUB SHOWVAR
    ENDIF
    MYFLAG=0
    GOTO MAIN
    

    
    
    SHOWVAR:
      DEBUG BIN4 MY_INA,CR
      MYFLAG=1
    RETURN
    

    ALLPINSZERO:
     DEBUG "ALL PINS ARE ZERO "
     GOSUB SHOWVAR
    GOTO BRANCHRETURN
    

    
    
    ONE:
    GOTO BRANCHRETURN
    

    TWO:
    GOTO BRANCHRETURN
    

    THREE:
    GOTO BRANCHRETURN
    

    FOUR:
    GOTO BRANCHRETURN
    

    FIVE:
    GOTO BRANCHRETURN
    

    SIX:
    GOTO BRANCHRETURN
    

    PINS012SET:
     DEBUG "PINS012SET "
     GOSUB SHOWVAR
    GOTO BRANCHRETURN
    

    EIGHT:
    GOTO BRANCHRETURN
    

    END
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    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 2009-01-30 08:24
    Metron, PBASIC 2.5 does have
    ON condition GOSUB routine
    For example,
    ON inA GOSUB sub0, sub1, sub2, sub3, sub4,sub5, sub6, sub7, sub8, sub9,subA, subB, subC, subD, subE,subF
    


    to cover all 16, and then return to the instruction after the ON .. GOSUB

    Kurt might only need a lookup though, or a computation.
    DIR15=1  ' make p15 an output
    LOOKUP inA, [noparse][[/noparse]1,0,1,1,0,0,1,1,1,1,0,0,1,0,1,1], OUT15
    


    Set the state of pin p15 depending on the 16 possibilities for 4 buttons on inA, using lookup table. The same sort of logic can apply if there are multiple output pins associated with the 16 possible input states.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • metron9metron9 Posts: 1,100
    edited 2009-01-30 17:57
    Yep, that's why I say, read the help and try the commands. usually there are several different ways to implement the logic necessary.
    I remember now he old Qbasic on gosub, brings back old memories of the commodore64 as well.
    One of the main points as you note above is realizing the number of possibilities or states and then designing the logic to handle all of them instead of just some of them.
    Boolean logic using and, not or and xor are also possibilities to set up logic and branching for input on 4 pins.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    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 2009-01-30 20:58
    >> read the help and try the commands. usually there are several different ways to implement the logic necessary

    Right you are about that! Rereading Kurt's original post, there are quite a few ways one could interpret what he is trying to accomplish. "Help me before I go mad" is often a question of "what do I really want to do, and why?"

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-01-31 03:40
    metron9 ·and··Tracy Allen

    Thank you both for explaining·how to do this in·some much detail

    [color=green]MYFLAG VAR Bit
    MY_INA VAR Nib    'CREATE A PLACE TO STORE THE STATE OF THE PINS[/color]
    



    IF MYFLAG=0 THEN
    DEBUG "ALL OTHER COMBINATIONS "
    GOSUB SHOWVAR
    ENDIF
    MYFLAG=0



    ALLPINSZERO:
    ·DEBUG "ALL PINS ARE ZERO "
    ·GOSUB SHOWVAR
    ·


    PINS012SET:
    ·DEBUG "PINS012SET "
    ·GOSUB SHOWVAR



    DIR15=1··'·make·p15·an·output
    LOOKUP·inA,·[noparse][[/noparse]1,0,1,1,0,0,1,1,1,1,0,0,1,0,1,1],·OUT15


    Thank you for pointing this out to me

    MrBi11

    ··DEBUG·"State·of·INA:·",
    ········BIN4·INA,·CR





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

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