Keyboard input to change event
Kram11
Posts: 8
·I am using the propeller starter kit with a keyboard and I want to change an event depending on what key I press i.e Key 1 flash LED1 key 2 Flash LED 5 and 6 etc. And also show the key on·a LCD display.·Can anyone help?
Comments
- in a loop wait for a key
- examine the key:
-- Key1? Then set OUTA for LED1 and rset OUTA for LED2
-- Key2? Then set OUTA for LED2 and reset OUTA for LED1
-- otherwise: reset OUTA for both LEDs
There are many more possibilities, e.g. you can switch off the LED aftera fixed time, independant of a key press...
This is all the fun of programming
Read through the comments of the keybord driver to find out how to call it...
· repeat
···· term.hex(kb.getkey,3)· ' it waits here untill it gets a key press
··· 'term.hex(kb.key,3)· ' just looks at what key is pressed and moves on
···
···· 'if kb.gotkey ==1
···· 'if kb.key<>000
········· term.out(" ")
········· term.str(string("got to here",13))
····
····· !outa[noparse][[/noparse]Pin]·· ' toggle LED when keyboard pressed
··· ' when button pressed clear the screen··
··· if ina[noparse][[/noparse]3]==1·· ' looking for the button to be pressed ( and key )
····· term.out($00)· '· this clears the screen look at other TV modes
····· term.str(string("· Start Again",13))
····· term.str(string("··· ",13))
····· term.str(string("button pressed",13))
at the moment I am waiting for the key to be pressed. I would like to scan through and only print to the screen if a key is pressed which would allow me to scan my input as well. I have tried a couple of methods using the gotkey but doesnt seem to work.
· any help would be appreciated.
You most likely already included it by
otherwise your program qouldn't compile in the first place.
- Have you initialized it ("start")??
- Have you set the right pins wrt your board??
Please read the comments in the routines:
- KEY
- GETKEY
- NEWKEY
- GOTKEY
so you can understand when you need which...
· My code is as follows
CON
······· _clkmode······· = xtal1 + pll16x
······· _xinfreq······· = 5_000_000
··········· PIN=16·
OBJ
······· term··· : "tv_text"
······· kb····· : "keyboard"
PUB start | i
··· dira[noparse][[/noparse]Pin]~~·
· 'start the tv terminal
· term.start(12)
· term.str(string("· DEMO",13))
· term.str(string("··· ",13))
· term.str(string("·· Enter a LED",13))
· 'start the keyboard
· kb.start(26, 27)
· 'echo keystrokes in hex
· repeat
···· term.hex(kb.getkey,3)· ' it waits here untill it gets a key press
··· 'term.hex(kb.key,3)· ' just looks at what key is pressed and moves on
···
···· 'if kb.gotkey ==1
···· 'if kb.key<>000
········· term.out(" ")
········· term.str(string("got to here",13))
····
····· !outa[noparse][[/noparse]Pin]·· ' toggle LED when keyboard pressed
··· ' when button pressed clear the screen··
··· if ina[noparse][[/noparse]3]==1·· ' looking for the button to be pressed ( and key )
····· term.out($00)· '· this clears the screen look at other TV modes
····· term.str(string("· Start Again",13))
····· term.str(string("··· ",13))
····· term.str(string("button pressed",13))······
····· '' Output a character
''
''···· $00 = clear screen
''···· $01 = home
''···· $08 = backspace
''···· $09 = tab (8 spaces per)
''···· $0A = set X position (X follows)
''···· $0B = set Y position (Y follows)
''···· $0C = set color (color follows)
''···· $0D = return
''· others = printable characters
· I understand the key and getkey but I dont seem to be able get my If statement to work if a key is pressed.
·I have only had my starter kit about a week and I am still getting to grips with spin as most of my programming experience is on industrial PLCs in Ladder.
E.g.,
···