Shop OBEX P1 Docs P2 Docs Learn Events
Programming Help BS2 For Next IN pin... — Parallax Forums

Programming Help BS2 For Next IN pin...

neotericneoteric Posts: 144
edited 2006-01-29 18:04 in BASIC Stamp
I have a robot that has six ir pairs.· I am trying to figure out how to read pins in a loop.· pins 0,2,3,5,6 are transmitters.· Pins 1,3,5,7,9,11 are recievers.

For x = 0 to 11 step 2 
FREQOUT x,1,37500 'transmit 
irDetect = INX+1 '<----- WHAT SHOULD I DO HERE? All the examples use irdetect=in9 or whatever the in pin is 
'do stuff 
Next


I know this is probably obvious, but I have not been able to find an example for the last two hours.· Thanks for helping.

Comments

  • Steve JoblinSteve Joblin Posts: 784
    edited 2006-01-29 15:59
    Try this...

    For x = 0 to 11 step 2
    FREQOUT x,1,37500 'transmit
    x=x+1
    irDetect = input x
    Next
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-01-29 16:09
    Here's what I would suggest:

    Emit····· DATA··· 0, 2, 3, 5, 6, 10
    Detect··· DATA·· ·1, 3, 5, 7, 9, 11

    Scan:
    · FOR idx = 0 TO 5
    ··· READ Emit + idx, emitter
    ··· READ Detect + idx, detector
    ··· FREQOUT·emitter, 1, 37500
    ··· irDetect(idx) = INS.LOWBIT(detector)
    · NEXT

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • neotericneoteric Posts: 144
    edited 2006-01-29 16:47
    thank you, I really appreciate the response

    but this does not seem to work...·· This returns an "Expected a constant, variable, unary operator, or "("" error.· In the help file, it looks like input is used to set a pin as input, not to read it's input.·

    DO
    GOSUB drive
    GOSUB ReadIr
    IF irdetect = 1 THEN
      GOSUB Forward
    ENDIF
    IF irdetect = 0 THEN
      GOSUB Park
    ENDIF
    LOOP
    

    ReadIr:
    FOR x = 0 TO 11 STEP 2
      FREQOUT 0,x,37500
      y = x+1
      irDetect = INPUT y
      DEBUG HOME,"irDetect", DEC y, " = ",DEC irDetect,CR
    NEXT
    RETURN
    
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-01-29 17:13
    neoteric -

    Make: INPUT Y

    read: IN Y

    Regards,

    Bruce Bates
  • neotericneoteric Posts: 144
    edited 2006-01-29 17:44
    Dan, Thanks· I have copied your code for later.· Since the rest of my code is not built on the array idea, I am going to try the other suggestions first.· Then I will try yours.· I would like to get this working before I takle arrays and data storage. (unless its the only way I can do it.)

    Bruce,

    ' {$STAMP BS2} 
    ' {$PBASIC 2.5} 
    ' -----[noparse][[/noparse] I/O Definitions ]------------------------------------------------- 
    x VAR Word 'just a temp variable 
    y VAR Word 
    irDetect VAR Word 
    'END 'save on batterys uncomment to stop stamp temporarily 
    '-----[noparse][[/noparse]Main Program]------------------------------------------- 
    DO 
    GOSUB readIR 
    LOOP 
    FOR x = 0 TO 11 STEP 2 
    FREQOUT 0,x,37500 
    y = x+1 
    INPUT y 
    [b]irDetect = IN y '<--- says IN is an undefined symbol.[/b] 
    DEBUG HOME,"irDetect", DEC y, " = ",DEC irDetect,CR 
    NEXT 
    RETURN
    

    I think I am doing this as you suggested, but I am getting an error on IN y.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-01-29 18:04
    IN is not a PBASIC keyword.·· In the version above I had assumed irDetect would be an array of bits -- here's how to update that code so that irDetect is a Byte.

    idx······ · VAR·· Nib
    emitter··· ·VAR·· Nib
    detector··· VAR·· Nib
    irDectect·· VAR·· Byte

    Emit····· · DATA··· 0, 2, 3, 5, 6, 10
    Detect···· ·DATA·· ·1, 3, 5, 7, 9, 11

    Scan:
    · FOR idx = 0 TO 5
    ··· READ (Emit + idx), emitter
    ··· READ (Detect + idx), detector
    ··· FREQOUT·emitter, 1, 37500
    ··· irDetect.LOWBIT(idx) = INS.LOWBIT(detector)
    · NEXT

    You input pins should never be made outputs so you don't need to complicate things by using the INPUT function.· This routine gives you the most flexibility to assign your emitter/detector pin pairs (in the DATA tables)·without resorting to monkey-motion with your for next loop -- monkey-motion that, if it goes wrong, could be driving outputs into each other and creating a conflict.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.