Reading pins and outputting to serial port
drdumont
Posts: 2
in BASIC Stamp
If someone has a free moment, maybe you could give me a little push...
I'm on the road, and the Stamp is at home, but I thought I would see about writing some programming
and then when I get home I can see what actually works.
Simply put, I just need to read the status of the individual digital input pins (0 through 7). (I presume I would set up a loop to scan the inputs.)
Then if an input goes high, output a character, e.g., input 1 High = "A" sent to the serial port (terminating with an ASCII LF (ASCII 13)). When input goes low, output a "B" and a LF
I need to monitor some relay contacts and put something on the screen to indicate when any relay is fired. Timing? Not an issue. The shortest cycle would be
over 5 seconds.
In good ol' GW Basic, it would be
Scan inputs - 0 through 7
each scan,
if input 0 =1 then print chr$(65) (And CHR$(13)) else print chr$(66)
(Insert code to ignore the pin until it goes low)
if input 1 =1 then print chr$(67) (And CHR$(13)) else print chr$(68)
Rinse, repeat until all pins scanned then start the scan again...
Each pin would then be scanned, if it is high, a unique character would be sent to the serial port. (Once)
If it is low, then a another unique character would be sent to the serial port. (Once)
IIRC my Basic Stamp 2 is a serial (rs-232) output. That would make things easier. If it is a USB out,
then I can deal with that later.
I admit this is pretty simple, but its been awhile since I have had time to play with my BS2, and if anyone
likes to noodle around with this kind of stuff, a little code would be appreciated. I admit I am not a structured
programmer, I still depend on GOTOS and stuff liek that, to the derision of my more advance cohorts.
Anyway, thanks for listening, and my appreciation to anyone who would like to chime in.
-- Doc
I'm on the road, and the Stamp is at home, but I thought I would see about writing some programming
and then when I get home I can see what actually works.
Simply put, I just need to read the status of the individual digital input pins (0 through 7). (I presume I would set up a loop to scan the inputs.)
Then if an input goes high, output a character, e.g., input 1 High = "A" sent to the serial port (terminating with an ASCII LF (ASCII 13)). When input goes low, output a "B" and a LF
I need to monitor some relay contacts and put something on the screen to indicate when any relay is fired. Timing? Not an issue. The shortest cycle would be
over 5 seconds.
In good ol' GW Basic, it would be
Scan inputs - 0 through 7
each scan,
if input 0 =1 then print chr$(65) (And CHR$(13)) else print chr$(66)
(Insert code to ignore the pin until it goes low)
if input 1 =1 then print chr$(67) (And CHR$(13)) else print chr$(68)
Rinse, repeat until all pins scanned then start the scan again...
Each pin would then be scanned, if it is high, a unique character would be sent to the serial port. (Once)
If it is low, then a another unique character would be sent to the serial port. (Once)
IIRC my Basic Stamp 2 is a serial (rs-232) output. That would make things easier. If it is a USB out,
then I can deal with that later.
I admit this is pretty simple, but its been awhile since I have had time to play with my BS2, and if anyone
likes to noodle around with this kind of stuff, a little code would be appreciated. I admit I am not a structured
programmer, I still depend on GOTOS and stuff liek that, to the derision of my more advance cohorts.
Anyway, thanks for listening, and my appreciation to anyone who would like to chime in.
-- Doc
Comments
I'd add the following to Tom's suggestion to key out the changes from low to high versus those from high to low. Tom's suggestion and my followup use fairly advanced programming techniques and the wealth of operators that are available in the BASIC Stamp math arsenal. This kind of program is a "state machine" that treats all the relays in a kind of parallel processing. Let us know if you'd rather see it as straightforward good ol' Basic!
^ is bitwise XOR
& is bitwise AND
~ is bitwise NOT
NCD ifinds the number of highest bit set in a variable
DCD sets the Nth bit in a variable
Lots of interesting ideas.
Many thanks again,
-- Doc