Shop OBEX P1 Docs P2 Docs Learn Events
How do I poll for serial input AND check for button changes at the same time? — Parallax Forums

How do I poll for serial input AND check for button changes at the same time?

ArchiverArchiver Posts: 46,084
edited 2004-04-12 19:15 in General Discussion
I don't know if this is possible, but I'm trying to monitor the
serial line for any input AND poll a handfull of pushbuttons
simultaneoulsly. I have some code functions that doesn't work very
well. If I pound on the pushbuttons I can get them to show up half
of the time in that small window of opportunity they have to be
recognized.

I really don't want a hardware solution to this because I suck at
electronics. However if there was some really easy way when I press
a pushbutton that it would stay in that state for a second or two my
routine would catch it. I was looking up RS Latches, but got
horribly confused.


The relevant code I have follows:

WaitForCommand:

'First check for inputs from the computer
Mode = 0
SERIN RxD,Baud96,2000,ReadButtons,[noparse][[/noparse]WAIT ("?"), Mode,OutputID]
'waits for a ? to signal start then reads Mode, and OutputID
'# to change

IF Mode = "T" THEN ToggleIt 'Toggle the Output
IF Mode = "N" THEN TurnOn 'Turn On the Output
IF Mode = "O" THEN TurnOff 'Turn Off the Output
IF Mode = "S" Then StateOfButtons 'Return State

GOTO ReadButtons 'If no inputs from serial, check buttons
'connected to the Stamp

ReadButtons:
BUTTON 6,0,255,10,buttonData0,1,Button0
BUTTON 7,0,255,10,buttonData1,1,Button1
BUTTON 8,0,255,10,buttonData2,1,Button2
BUTTON 9,0,255,10,buttonData3,1,Button3
BUTTON 10,0,255,10,buttonData4,1,Button4
BUTTON 11,0,255,10,buttonData5,1,Button5
BUTTON 12,0,255,10,buttonData6,1,Button6
BUTTON 13,0,255,10,buttonData7,1,Button7


SEROUT Txd,Baud96,[noparse][[/noparse]"Nothing"] 'just for debugging
GOTO WaitForCommand

Button0:
--do something because button 0 was pressed
GOTO WaitForCommand


THANKS IF YOU CAN HELP!

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2004-04-12 19:15
    I'd recommend that you dispense with the BUTTON command, because it
    is overkill for what you are trying to do.

    Just read the state of all the buttons at once. This would be a
    little easier if you had all of the 8 buttons on one port, say p8 to
    p15, instead of p6 to p13. If they are all on one port then do this:

    buttonState = inH ' reads all 8 buttons at once
    buttonChange = buttonState ^ oldButtonState & oldButtonState
    oldButtonState = buttonState
    ON NCD buttonChange GOTO button0,button1,......,button7

    That uses fewer variables but still debounces the buttons (using the
    ^ XOR logic between the old and new states), branching to to the
    routine on the 1->0 transition.

    As to the serial input, that is a harder nut to crack.

    1) can you add handshaking, so that the computer will only send
    the Stamp the data when the Stamp is ready? That would use the SERIN
    RxD\flow syntax along with hardware handshaking on the PC. Or,
    implement some kind of software Xon/Xoff handshaking.

    2) make the timeout on the SERIN much shorter, say 0.1 second, but
    include a WAIT modifier, before the data. The PC might have to
    resend the data a couple of times.

    3) add a capacitor to ground on each of the button inputs. The
    code would charge up the capacitors, and a button pressed would
    discharge it. So the capacitor holds the record of the event, that a
    button was pressed or not when the program is off reading the serial
    port.

    -- Tracy






    >I don't know if this is possible, but I'm trying to monitor the
    >serial line for any input AND poll a handfull of pushbuttons
    >simultaneoulsly. I have some code functions that doesn't work very
    >well. If I pound on the pushbuttons I can get them to show up half
    >of the time in that small window of opportunity they have to be
    >recognized.
    >
    >I really don't want a hardware solution to this because I suck at
    >electronics. However if there was some really easy way when I press
    >a pushbutton that it would stay in that state for a second or two my
    >routine would catch it. I was looking up RS Latches, but got
    >horribly confused.
    >
    >
    >The relevant code I have follows:
    >
    >
    >WaitForCommand:
    >
    > 'First check for inputs from the computer
    > Mode = 0
    > SERIN RxD,Baud96,2000,ReadButtons,[noparse][[/noparse]WAIT ("?"), Mode,OutputID]
    > 'waits for a ? to signal start then reads Mode, and OutputID
    > '# to change
    >
    > IF Mode = "T" THEN ToggleIt 'Toggle the Output
    > IF Mode = "N" THEN TurnOn 'Turn On the Output
    > IF Mode = "O" THEN TurnOff 'Turn Off the Output
    > IF Mode = "S" Then StateOfButtons 'Return State
    >
    >GOTO ReadButtons 'If no inputs from serial, check buttons
    > 'connected to the Stamp
    >
    >ReadButtons:
    > BUTTON 6,0,255,10,buttonData0,1,Button0
    > BUTTON 7,0,255,10,buttonData1,1,Button1
    > BUTTON 8,0,255,10,buttonData2,1,Button2
    > BUTTON 9,0,255,10,buttonData3,1,Button3
    > BUTTON 10,0,255,10,buttonData4,1,Button4
    > BUTTON 11,0,255,10,buttonData5,1,Button5
    > BUTTON 12,0,255,10,buttonData6,1,Button6
    > BUTTON 13,0,255,10,buttonData7,1,Button7
    >
    >
    > SEROUT Txd,Baud96,[noparse][[/noparse]"Nothing"] 'just for debugging
    >GOTO WaitForCommand
    >
    >Button0:
    > --do something because button 0 was pressed
    >GOTO WaitForCommand
    >
    >
    >
    >THANKS IF YOU CAN HELP!
    >
    >
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the
    >Subject and Body of the message will be ignored.
    >
    >Yahoo! Groups Links
    >
    >
    >
    >
    >
Sign In or Register to comment.