Shop OBEX P1 Docs P2 Docs Learn Events
How do you scan multiple input pins inside a FOR ...NEXT loop? — Parallax Forums

How do you scan multiple input pins inside a FOR ...NEXT loop?

Lighter ManLighter Man Posts: 3
edited 2007-07-06 14:36 in BASIC Stamp
I can do it using brute force:

IF IN1 = 1 THEN GOHERE1
IF IN2 = 1 THEN GOHERE2
IF IN3 = 1 THEN GOHERE3
IF IN4 = 1 THEN GOHERE4
IF IN5 = 1 THEN GOHERE5
IF IN6 = 1 THEN GOHERE6


But I'd like to do it using something like:


FOR N= 1·TO 6
IF IN(N) = 1·THEN GOHERE(N)
NEXT N

This does not work. Any ideas?· Thanks, Perry

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2007-06-27 21:30
    ON...GOTO
    Syntax: ON Offset GOTO Address1, Address2, ...AddressN

    Function
    Go to the address specified by offset (if in range).
    • Offset is a variable/constant/expression (0 - 255) that specifies the index of the address, in the list, to branch to (0 - N).
    • Addresses are labels that specify where to go. ON...GOTO will ignore any list entries beyond offset 255.

    Quick Facts
    attachment.php?attachmentid=73928

    Explanation
    The ON...GOTO instruction is useful when you want to write something like this:

    IF idx = 0 THEN Case_0 ' idx = 0: jump to label "Case_0" IF idx = 1 THEN Case_1 ' idx = 1: jump to label "Case_1" IF idx = 2 THEN Case_2 ' idx = 2: jump to label "Case_2"

    You can use ON...GOTO to organize this into a single statement:

    ON idx GOTO Case_0, Case_1, Case_2This works exactly the same as the previous [url=mk:@MSITStore:C:\Program%20Files\Parallax%20Inc\Stamp%20Editor%20v2.2\PBASIC.chm::/if_then.htm]IF...THEN[/url] example. If the value isn't in range (in this case if value is greater than 2), ON...GOTO does nothing and the program continues with the next instruction after ON...GOTO.
    243 x 104 - 2K
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-06-27 21:38
    Hello,

    When referencing inputs your must specify the index from an actual pin, for example: IN0(N). Also, you can’t specify a label with an offset like that. If I were trying to do this I would probably try something like:
    [color=#000000]Main:[/color]
    
    [color=#000000]  BRANCH NCD INL, Main, GOHERE1, GOHERE2, GOHERE3, GOHERE4, etc.[/color]
    


    Note that this will look at all 8 I/O pins from P0 through P7 and that you will need 9 addresses unless you filter the input. Also, in cases where multiple inputs are high the lower pins will have priority.· I hope this helps. If you have additional questions let me know. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • jHamjHam Posts: 2
    edited 2007-07-06 05:09
    Very nice Chris! Do you know of a way to make it scan only IN0 - IN4 in a single pass like that? I can almost get away with INA, but I need the 5th input read as well. If i use INL, is there a way to still use those other 3 inputs without causing a problem?

    Thanks!

    -john
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-07-06 05:53
    BRANCH NCD (INL & %11111), [noparse][[/noparse]Main, GOHERE1, GOHERE2, GOHERE3, GOHERE4, GOHERE5]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • jHamjHam Posts: 2
    edited 2007-07-06 06:20
    Great Tracy! So if I wanted to scan 6 inputs i would use (INL & %111111)?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-07-06 14:36
    That is correct. INL is 8 bits and the (INL & %xxxxxx) is essentially masking to get only the bits we want from that port. Those bits are then eNCoDed to produce a return value based on the bit position.· Since these start at 1 and BRANCH starts at 0, we must adjust for that.· Since we want to loop anyway simply specifying the first label as the Main loop takes care of that.· If this code was entered via subroutine you would have to subtract one from the calculation prior to the BRANCH instruction.· Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support


    Post Edited (Chris Savage (Parallax)) : 7/6/2007 2:41:12 PM GMT
Sign In or Register to comment.