Shop OBEX P1 Docs P2 Docs Learn Events
BS2 Code Question -INPUT — Parallax Forums

BS2 Code Question -INPUT

KatyBriKatyBri Posts: 171
edited 2006-12-03 01:22 in BASIC Stamp
The following is from the V2.2.6 help file-
Setup:
· INPUT 4

Hold:
· IF (IN4 = 0) THEN Hold······················· ' Stay here until P4 is 1

1. Will the following also work-
TEST PIN 4

Hold:
·If (TEST·= 0)·THEN Hold······················ '·Stay here until P4 is 1

Let's now change the help file example to-
Setup:
· INPUT 4

Hold:
· IF (IN4 = 1) THEN Hold······················· ' Stay here until P4 is 0
2. Will the following also work-
TEST PIN 4

Hold:
·If·TEST·THEN Hold······················ '·Stay here until P4 is 0
3. Finally, can you "scan" the states of the pins-

MAIN:
For index = 0 to 4
·INPUT IN(index)
·IF IN(index) THEN SUB(index) ' If IN(index)=1 then goto corresponding sub
NEXT
GOTO MAIN
SUB(index)
'code to do something
GOTO MAIN

Thanks
·

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-12-02 05:14
    KatyBri,
    ·
    ·· You can define a PIN constant and the IDE will know whether the pin is input or output.· You don’t need to declare a pin as an input if it has never been made an output.· By default at startup all pins are inputs.· You can also use the example code below.

    TEST   PIN  4
     
    DO : LOOP WHILE TEST   ' Wait until P4 goes LOW
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-12-02 05:22
    KatyBri,
    ·
    ·· As for ‘scanning’ the I/O pins, you can read them all with one command.· If you’re trying to index the individual pins into an array that can be done by referencing the pins from IN0.· In other words, IN0(index) where index is a value from 0 through 15 referring to one of the 16 I/O pins.· Oh, almost forgot, reading all I/O pins, or 8 or 4…I hope this helps.· Take care.

    ioData = INS  ' This reads all 16 pins at once, ioData must be Word
    OUTS = ioData ' This writes all 16 I/O registers at once
                  ' DIRS must be output to appear on pins
     
    ioData = INL  ' Read P0 - P7 (ioData should be a BYTE)
     
    ioData = INA  ' Read P0 - P3 (ioData should be Nib)
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2006-12-02 14:58
    KatyBri said...


    For index = 0 to 4
    ·INPUT IN(index)
    ·IF IN(index) THEN SUB(index) ' If IN(index)=1 then goto corresponding sub
    NEXT
    Here's a suggestion. Please note inputs are continuous, if you need them as one-shots I can post a code for that.

    Main:

    DO
    For index = 0 to 4 '5 consecutive pins scanned
    ··IF IN0(index) THEN
    ······· ON index GOSUB sub_0, sub_1, sub_2, sub_3, sub_4
    · ENDIF
    NEXT
    LOOP
    'subroutines
    sub_0:
    '..code goes here
    Return

    sub_1:
    '..code goes here
    Return

    sub_2:
    '..code goes here
    Return

    sub_3:
    '..code goes here
    Return


    sub_4:
    '..code goes here
    Return

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR
  • KatyBriKatyBri Posts: 171
    edited 2006-12-03 01:22
    Chris and TechnoRobbo- thanks very much. Your comments were just what I needed.
Sign In or Register to comment.