Reading pins and outputting to serial port

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

Comments

  • 3 Comments sorted by Date Added Votes
  • It looks like you are looking for changes of the inputs. I would keep two variables, OldPins and CurrentPins. Every so often I would
    CurrentPins = INL        'nice new copy of pins
    DeltaPins = CurrentPins ^ OldPins   'look for something changed
    If (DeltaPins = 0) Then NoChange    'nothing interesting happened
    for PinNumber = 0 to 7
        if (DeltaPins.LowBit(PinNumber)) = 0 then PinNotChange    'check this pin
        'print whatever you want for this pin
    PinNotChange:   NEXT
    
    
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
    Cool, CA, USA 95614
  • Tracy AllenTracy Allen Posts: 6,116
    edited April 11 Vote Up0Vote Down
    Doc,
    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
    DeltaHi = DeltaPins & CurrentPins   '  changes from 0 to 1, a byte variable
    DeltaLo = DeltaPins & OldPins       ' changes from 1 to 0, ditto
    OldPins = CurrentPins                   ' ready for the next scan
    ' and rather than a FOR NEXT loop, one that allows for the possibility that
    ' two relays might change at exactly the same instant of time:
    Do while DeltaHi   ' this will catch all of the relays that went hi at that instant
      MyPin = NCD DeltaHi - 1   ' a pin number that changed, 0 to 7, MyPin is a nibble variable
      DEBUG "A"+(MyPin*2)     ' prints "A" , "C", "E" etc.
      DeltaHi = DeltaHi & ~ DCD MyPin  ' knocks down bits already processed
    Do while DeltaLo   ' ditto for low
      MyPin = NCD DeltaLo - 1   ' a pin number that changed, 0 to 7
      DEBUG "B"+(MyPin*2)    ' prints "B" , "D", "F" etc.
      DeltaLo = DeltaLo & ~ DCD MyPin  ' knocks down bits already processed
    

  • Thanks for the input! I will breadboard these suggestions, and report back!
    Lots of interesting ideas.
    Many thanks again,
    -- Doc
Sign In or Register to comment.