BS2 Code Question -INPUT
KatyBri
Posts: 171
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
·
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
·
·· 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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
·
·· 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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
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