134 posts
Posted 20 September 2013 - 06:11 PM
[SOLVED]Title: Timer Problems with key pressesHello There pro's!
I've been working on this code this evening, and it seems to work, tho there is this small Problem
pastebin:
http://pastebin.com/e9gH8QeVCode:
local tid = 1
local delay = tonumber(tid)
function speed()
term.clear()
term.setCursorPos(1,1)
print("Delay is now: ".. delay)
local event, key = os.pullEvent("key")
if key == keys.up then
tid = tid+1
elseif key == 208 then
tid = tid-1
end
end
function timer()
speed()
rs.setOutput("back", true)
sleep(0.3)
rs.setOutput("back", false)
sleep(delay)
end
repeat
timer()
until rs.getInput("left")
return
What im seaching it for to do, is when ever arrow Up is pressed, it increases the delay, and when arrow down is pressed it shortens the delay, i've been looking around trying some fixes, but does't seem to help..
Any suggestion on what im doing wrong? :)/>
Regards Plazter
EDIT: This has been solved heres the link, if some one wants to use it :)/>
http://pastebin.com/1fuAu4wfThanks everyone who helped me OR tryed to help aswell :)/>
Thanks alot appreciate it! ;)/>
1522 posts
Location
The Netherlands
Posted 20 September 2013 - 07:12 PM
134 posts
Posted 20 September 2013 - 07:29 PM
The picture is missing :)/>, and i dont really understand sorry ;b tho' i knew the most of that tutorial, now im reading up on the rest of the links you provided :)/>
1522 posts
Location
The Netherlands
Posted 20 September 2013 - 08:43 PM
Oh, accidentally deleted that then. Will upload tomorrow, when I'm behind a pc.
But about you're problem, you only have one pullEvent, so you can only push one time a key.
Put the os.pullEvent in a loop, or using the parallel api ( try to avoid it though! ).
997 posts
Location
Wellington, New Zealand
Posted 21 September 2013 - 03:21 AM
It already is in a loop…
The problem is that "local delay = tonumber(tid)" doesn't mean what you seem to think it means.
It means "Get the value of tid, convert it to a number, and store it in delay."
It does not mean "Make sure delay is always equal to tid (converted to a number)."
When you change tid after this, it does not change delay.
187 posts
Location
Bowie, TX
Posted 21 September 2013 - 07:39 AM
In short replace
sleep(delay)
with
sleep(tid)
and get rid of or comment out
local delay = tonumber(tid)
after all
tid is already a number.
134 posts
Posted 21 September 2013 - 01:53 PM
-snip-
after all tid is already a number.
Hmm might try that ;)/> Cheers :)/>
134 posts
Posted 21 September 2013 - 06:44 PM
Allright guys, that worked, but it seems to be slower than the seconds it should, any ideas to improve it? :)/>
187 posts
Location
Bowie, TX
Posted 22 September 2013 - 09:36 AM
Allright guys, that worked, but it seems to be slower than the seconds it should, any ideas to improve it? :)/>
Try adding the example code at
http://computercraft.info/wiki/Os.clock into it somewhere so you can verify how fast the routines are …
134 posts
Posted 22 September 2013 - 03:48 PM
aw, it only ticks whenever a button is pressed :/
187 posts
Location
Bowie, TX
Posted 22 September 2013 - 05:19 PM
Sounds like you want the loop to not wait for a keypress but to keep operating. In which case it'll need an
sleep(0.8)
put in it somewhere so you don't the
too long without pausing
error. However, it sounds also like you want it to react to a keypress as well inwhich case you might want to make use either of these two apis:
http://computercraft.info/wiki/Coroutine_(API),
http://computercraft.info/wiki/Parallel_(API).
I think the parallel might work best for you. Here is my idea for your code:
Spoiler
local tid = 1
local key = 0
function testKey()
_, key = os.pullEvent("key")
if key == keys.up then
tid = tid+1
elseif key == keys.down then
tid = tid-1
end
end
function speed()
term.clear()
term.setCursorPos(1,1)
print("Delay is now: ".. delay)
end
function timer()
speed()
rs.setOutput("back", true)
sleep(0.3)
rs.setOutput("back", false)
sleep(tid)
end
while key ~= keys.left do
-- using the parallel api we should be able to allow the program run all the time
parallel.waitForAny(timer,testKey)
end
134 posts
Posted 29 September 2013 - 04:17 AM
–snip.
I think the parallel might work best for you. Here is my idea for your code:
Spoiler
local tid = 1
local key = 0
function testKey()
_, key = os.pullEvent("key")
if key == keys.up then
tid = tid+1
elseif key == keys.down then
tid = tid-1
end
end
function speed()
term.clear()
term.setCursorPos(1,1)
print("Delay is now: ".. delay)
end
function timer()
speed()
rs.setOutput("back", true)
sleep(0.3)
rs.setOutput("back", false)
sleep(tid)
end
while key ~= keys.left do
-- using the parallel api we should be able to allow the program run all the time
parallel.waitForAny(timer,testKey)
end
Thanks man!
You're the best!, me and a mate fixed it :D/>
it works!! :D/> (Will post code here later)