how to send a serial command with the press of a button
Chris Savage
Parallax EngineeringPosts: 14,406
Everytime an active-high button connected to P0 is pressed it will send the text to the serial I/O line.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
' {$STAMP BS2} ' {$PBASIC 2.5} DO IF IN0 = 1 THEN SEROUT LCD, Baud, [noparse][[/noparse]"Hello World!"] PAUSE 100 ENDIF LOOP
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
Comments
ButtonState = 0
IF MyButton = 1 THEN
IF ButtonState = 0 THEN
SEROUT 16, 63864, [noparse][[/noparse]"High",13]
ButtonState = 1
' ELSE, do nothing
END IF
ELSE ' MyButton == 0 (not pressed)
IF ButtonState = 1 THEN
ButtonState = 0
END IF
END IF
The idea behind this is you only output ONE message, when the system
first detects the button is pressed (goes high, I'm assuming here).
As long as you hold the button down, ButtonState stays 1, and you don't
output any more messages.
It's only after you Release the button, the system reads that, and
sets ButtonState to zero -- After that, you can press the button
again and get ONE more message out.
Post Edited By Moderator (Chris Savage (Parallax)) : 4/2/2006 3:18:21 AM GMT
I used your example, now I have two buttons that will change the parameters of a Microscan 820 UPC scanner by sending one command with one button and the other command with the other button. Works like a Champ.
' {$STAMP BS2sx}
' {$PBASIC 2.5}
Start:
IF IN0=0 THEN
SEROUT 16,8432, [noparse][[/noparse]"<KL1><A>"]
PAUSE 500
ENDIF
IF IN1=0 THEN
SEROUT 16,8432, [noparse][[/noparse]"<KL2><A>"]
PAUSE 500
ENDIF
GOTO Start
Your the Man Chris!
Thanks again
ButtonState = 0
IF MyButton = 1 AND ButtonState = 0 THEN
SEROUT 16, 63864, [noparse][[/noparse]"High",13]
ButtonState = 1
ELSEIF MyButton = 0 THEN
ButtonState = 0
END IF
How would your example work with 2 buttons. As you can see I'm far from a programming guru, but I'm giving it hell.