Shop OBEX P1 Docs P2 Docs Learn Events
INx Question — Parallax Forums

INx Question

8136mike8136mike Posts: 2
edited 2010-05-21 12:48 in BASIC Stamp
I have a loop in which I want to continually monitor two inputs, IN3 & IN13. But not both INs at the same time. One time the loop will be set up to monitor IN3 another time IN13. How can I change a 3 to 13 or 13 to 3 prior to the loop starting something like:

x var word
x = 3

loop:
if INx = 1 then do something
pause 3000
etc
etc
goto loop

I thought I could do it by saying x = 3 or x = 13, however INx will not compile.

I'm using Stamp 2.0. Do I need 2.5?

Comments

  • JomsJoms Posts: 279
    edited 2010-05-18 03:24
    try:

    x = IN3


    if x = 1 then do something
  • Mike GreenMike Green Posts: 23,101
    edited 2010-05-18 03:27
    The input register is really a 16 bit register and can be accessed that way as INS. This can be shifted. For example, if you have a value X that's zero for IN3 and one for IN13, you could write:

    IF (INS >> (10*X + 3)) & 1 = 1 THEN

    There are other ways to do the same thing, but this is the most straightforward.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-05-18 16:46
    Another way to do it is by index as follows:
    x=13
    IF in0(x) = 1 THEN DEBUG "zip"
    
    


    The variable x is an offset in bits from in0, so in0(0) is the same as in0, and in0(13) is the same as in13.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • davejamesdavejames Posts: 4,047
    edited 2010-05-18 22:54
    Mr. Allen,

    Would you tell me where the INS bits can be accessed by an index as you mentioned? I just looked in the programming guide and am unable find it.

    Regards,

    DJ

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-05-19 00:21
    DJ, it is not well documented, but the whole Stamp memory map is an array that can be indexed implicity by bit, nib, byte or word. There are 16 words in the memory map, consisting of INS, OUTS, DIRS, and W0 thru W12.

    If you DEBUG INS(3), you will get the current value of W0, three up in the array of words. The index is sequential, small-endian. That also applies to the array of 256 bits. IN0 refers to the least significant bit of INS, and also to the least significant bit of the 256 bit memory array. So IN0(13) is the same as IN13. IN0(255) is the same as W12.bit15. The array is a bit array because IN0 is a bit. When referring to INS(3) it was a word, because INS is a word.

    The offset can start anywhere in the array. For example, IN3(10) is also the same as IN13.

    This sort of addressing is the same across the Stamp 2 family; very useful, despite being somewhat of a "secret".

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • 8136mike8136mike Posts: 2
    edited 2010-05-19 02:03
    Mike & Tracy:

    Thanks for the reply. Both of your recommendations are just what I need. I did not mention in my initial inquiry, but I wanted to use only one line of code to look at IN3 or IN13 as it went though the loop. I had a solution with doing a skip a line routine but wanted to use only one line of code for efficiency purposes.

    Thanks again.

    8136mike
  • ZootZoot Posts: 2,227
    edited 2010-05-19 04:50
    x VAR Bit
    ' if x = 0, get IN3 into val
    ' if x = 1, get IN13 into val
    LOOKUP x, [noparse][[/noparse] IN3, IN13 ], val
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • metron9metron9 Posts: 1,100
    edited 2010-05-19 20:44
    How about this if you switch your input pins to in0 and in1 for example

    ON (INA & x) GOSUB Case_0, Case_1, Case_3
    
    
    



    where x is 1 or 2 (%00000001 or %00000010)

    so Case 1 = in1 high when x=2
    Case 2 = in0 high when x=1
    Case 0 = in0 or in1 = 0 when x = 1 or 2

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    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) : 5/19/2010 8:54:56 PM GMT
  • davejamesdavejames Posts: 4,047
    edited 2010-05-19 22:19
    Mr.Allen,

    ...wow! I never would have made that connection based on the material at hand.

    I'm going to print your answer and stick it in the INS section of the manual.


    Thanks again,

    DJ

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • metron9metron9 Posts: 1,100
    edited 2010-05-20 13:24
    Mike and Tracy,s solution allow a single statement and Zoot's solution would also require a branching statement.
    My solution only uses one statement to both compare and branch as Tracy's and Mike's solution however my solution allows branching to multiple address for multiple inputs with one statement.
    I also would note the speed of Mike's solution may be slower as it involves multiplication shifting and Boolean logic to make the branch.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    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 2010-05-21 12:48
    Example code compares pin 0 and pin 1 based on the mask set up in the variable X

    
     ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' {$PORT COM1}
    
    
    INPUT INA
    
    x VAR Byte
    x=2
    
    main:
    FOR X= 1 TO 3
         ON (INA & x) GOSUB Case_0, Case_1, Case_2,CASE_3
    NEXT
    
    GOTO main
    
    
    
         case_0:
         DEBUG " 0",CR
         RETURN
         case_1:
         DEBUG " 1",CR
         RETURN
         Case_2:
         DEBUG " 2",CR
         RETURN
         Case_3:
         DEBUG " 3",CR
         RETURN
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
Sign In or Register to comment.