Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 22:29 26 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Inkey question

Author Message
vk4tec

Senior Member

Joined: 24/03/2012
Location: Australia
Posts: 239
Posted: 10:20pm 07 Jan 2013
Copy link to clipboard 
Print this post

Hello

I have a GPS display with 5 screens

I would like a key press to change the screens

It sort of works for me, but the screen stops updating

Do

key_press$ = inkey$

if key_press$ = "1" then
show screen 1
endif

if key_press$ = "2" then
show screen 2
endif

Loop

Only problem is, the screen is not updated when key_press$ = ""
Which is what happens when no key is being pressed
How can I retain the current string value key_press$ ?

- Andrew -



Andrew Rich VK4TEC
www.tech-software.net
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3802
Posted: 10:45pm 07 Jan 2013
Copy link to clipboard 
Print this post

I think you need another variable, along the lines of:

key$ = "1" '(default screen to show)

Do

newkey$ = inkey$

'Check it's valid:
if newkey$ <> "" and instr(newkey$, "12") <> 0 then
key$ = newkey$
endif

if key$ = "1" then
show screen 1
endif

if key$ = "2" then
show screen 2
endif

Loop

I've ASSUMED instr returns 0 for Not Found but the manual I have doesn't say what it does!!

Change the instr's "12" to "12345" to allow keys 1-5, etc.
You may not need the "and" but I'm guessing you do.

JohnEdited by JohnS 2013-01-09
 
MOBI
Guru

Joined: 02/12/2012
Location: Australia
Posts: 819
Posted: 11:26pm 07 Jan 2013
Copy link to clipboard 
Print this post

Hi, maybe I'm on the wrong beam, but I wonder if you can use the keydown command to first check if there is a key pressed and either ignore it if there is no key pressed or if there is, then do the key selected.
David M.
 
vk4tec

Senior Member

Joined: 24/03/2012
Location: Australia
Posts: 239
Posted: 12:26am 08 Jan 2013
Copy link to clipboard 
Print this post

Sorted

key_now$ = "1"

Do

key_pad$ = inkey$

if key_pad$ = "1" then
key_now$ = "1"
endif

if key_pad$ = "2" then
key_now$ = "2"
endif

if key_now$ = "1" then
display 1
endif

if key_now$ = "2" then
display 2
endif

loop

Edited by vk4tec 2013-01-09
Andrew Rich VK4TEC
www.tech-software.net
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 12:32am 08 Jan 2013
Copy link to clipboard 
Print this post

Hi Andrew,

This function from James Deakins that I'm using in one of my programs works to pick up a press of any key:

Function Target$()
Getkey:
key$ = Inkey$
If key$ = "" Then GoTo GetKey 'Loop waiting for keypress
....do some more with "key$"....
End Function


GregEdited by paceman 2013-01-09
 
vk4tec

Senior Member

Joined: 24/03/2012
Location: Australia
Posts: 239
Posted: 12:39am 08 Jan 2013
Copy link to clipboard 
Print this post

My case is different

I am always looping

Need to catch a key press and not hang around

Andrew
Andrew Rich VK4TEC
www.tech-software.net
 
James_From_Canb

Senior Member

Joined: 19/06/2011
Location: Australia
Posts: 265
Posted: 01:53am 08 Jan 2013
Copy link to clipboard 
Print this post

OK. How about this then?


Top:
Key$ = Inkey$
If Key$ = "" Then GoGo Top 'Loop waiting for Keypress

If (Key$ >= "0") and (Key$ <= "5") then
KeyVal = Val(Key$) 'Convert to decimal
If KeyVal = 0 Then EXIT
Display KeyVal
GoTo Top
Else
? "Some error message"
GoTo Top
Endif


Or, if you prefer improving your code by removing goto's and using constants:


False = 0
True = 1
MaxScreen = "5" ' in case you want to add more screens

bExit = False
Do While (bExit = False)
bValidKey = True
Do While (bValidKey = True) ' or even: While bValidKey
Key$ = ""
Do While (Key$ = "")
Key$ = Inkey$
Loop
If ((Key$ < "0") OR (Key$ > MaxScreen)) Then
bValidKey = False
? "an appropriate error message"
Else
KeyVal = Val(Key$)
If (KeyVal = 0) Then
bExit = True
Else
display KeyVal ' display the selected screen
Endif
Endif
Loop
Loop
EXIT


I haven't tested the code, but you get the idea.

James
My mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention.

Hedley Lamarr, Blazing Saddles (1974)
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 04:51am 08 Jan 2013
Copy link to clipboard 
Print this post

I would do it differently. :) So many ways to get the same end result....
Here some code for two screens.
As you can see it would be very easy to add screens. Just change the AllowedKeys$ and add a subroutine.
I added some code as a sample to control the update frequency of the screens.
Screen 1 will update every second, screen 2 every 5 seconds.
[code]

AllowedKeys$ = "12"
CurrentScreen = 1
TickFrequency = 1000

'Screen update frequency = ScreenxMaxTicks * TickFrequency / 1000 seconds
Screen1Ticks = 0
Screen1MaxTicks = 1
Screen2Ticks = 0
Screen2MaxTicks = 5

SETTICK TickFrequency, UpdateScreenTick

Do
KeyPos = INSTR( 1, AllowedKeys$, INKEY$)
If KeyPos > 0 And KeyPos <> CurrentScreen Then
CurrentScreen = KeyPos
'If instant switching to the new screen is necessary
'you can call UpdateScreen from here
'otherwise it can take up to the used interval before the screen switches
End If
Loop

UpdateScreenTick:
UpdateScreen
IReturn

Sub UpdateScreen
On CurrentScreen Gosub Screen1Update, Screen2Update
End Sub

Sub Screen1Update
Screen1Ticks = Screen1Ticks + 1
if Screen1Ticks >= Screen1MaxTicks then
Screen1Ticks = 0
'here code to update the screen
end if
End Sub

Sub Screen2Update
Screen2Ticks = Screen2Ticks + 1
if Screen2Ticks >= Screen2MaxTicks then
Screen2Ticks = 0
'here code to update the screen
end if
End Sub
[/code]
This will scan the keyboard continuously, check for valid keys and updates the screen on a specific interval.
Edited by TZAdvantage 2013-01-09
Microblocks. Build with logic.
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024