KEYBOARD Input
HEDGE
Posts: 8
Is it possible to make the BS2 perform a certain action when a keyboard button is pressed?· I want to control several servos with different keyboard letters.
Comments
- Do you mean to:
- Use the keyboard on your PC while the BASIC Stamp is connected to the PC via a serial or USB port?
If you mean #2, you can do this easily via the Debug Terminal's Transmit Windowpane, using the DEBUGIN command.·Attach a keyboard directly to your BASIC Stamp, or
-Stephanie
If you mean a separate keyboard attached to the Stamp, you can do this too. You can buy a PS/2 keyboard adapter that attaches to a Stamp via a serial connection. You'd use a SERIN statement to receive the typed character.
There are articles in the Nuts and Volts Columns series on attaching a keypad to a Stamp and scanning the keys.
Also, in "What's a Microcontroller?" there is an activity using DEBUGIN with a Standard Servo, chapter 4, activity 2, on page 119.· You can download it·free·from the BASIC Stamp Activity Kit page.
-Stephanie Lindsay
Editor, Parallax Inc.
'Program: Keyboard Control
'{$STAMP BS2}
'{$PBASIC 2.5}
X VAR Byte 'Declare variable
Do until (X = "q" or X = "Q") 'Open Loop
DEBUGIN X 'Accept an input keystroke
Debug X 'Show it back to the user
If X = "w" or X = "W" then
Gosub Forward
ElseIf X = "s" or X = "S" then
Gosub Backwards
ElseIf X = "a" or X = "A" then
Gosub Left
ElseIf X = "d" or X = "D" then
Gosub Right
ElseIf X = "h" or X = "H" then
Gosub Honk
EndIf 'Closes the conditional
Loop 'Close the loop
END 'End the program
Forward:
pulsout 12, 650 'right wheel forward
pulsout 13, 850 'left wheel forward
return 'Back to program
Backwards:
pulsout 12, 850 'right wheel backwards
pulsout 13, 650 'left wheel backwards
return 'Back to program
Left:
pulsout 12, 650 'right wheel forward
pulsout 13, 650 'left wheel backwards
return 'Back to program
Right:
pulsout 12, 850 'right wheel backwards
pulsout 13, 850 'left wheel forwards
return 'Back to program
Honk:
Freqout 4, 500, 2000 'honks for half of a second with a high pitch frequency
return