Jump to content




[Timer] Timer Problem With Keys

computer lua

11 replies to this topic

#1 plazter

  • Members
  • 134 posts

Posted 20 September 2013 - 05:11 PM

[SOLVED]
Title: Timer Problems with key presses

Hello 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/e9gH8QeV

Code:
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/1fuAu4wf
Thanks everyone who helped me OR tryed to help aswell :)
Thanks alot appreciate it! ;)

#2 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 20 September 2013 - 06:12 PM

You are only waiting one time for e key event, have a read through this: Shameless self advertisement, but I think I can ensure you it will help you out!

#3 plazter

  • Members
  • 134 posts

Posted 20 September 2013 - 06:29 PM

View PostEngineer, on 20 September 2013 - 06:12 PM, said:

You are only waiting one time for e key event, have a read through this: Shameless self advertisement, but I think I can ensure you it will help you out!

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 :)

#4 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 20 September 2013 - 07: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! ).

#5 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 21 September 2013 - 02: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.

#6 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 21 September 2013 - 06: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.

#7 plazter

  • Members
  • 134 posts

Posted 21 September 2013 - 12:53 PM

View Postkreezxil, on 21 September 2013 - 06:39 AM, said:

-snip-
after all tid is already a number.

Hmm might try that ;) Cheers :)

#8 plazter

  • Members
  • 134 posts

Posted 21 September 2013 - 05:44 PM

Allright guys, that worked, but it seems to be slower than the seconds it should, any ideas to improve it? :)

#9 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 22 September 2013 - 08:36 AM

View Postplazter, on 21 September 2013 - 05:44 PM, said:

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...o/wiki/Os.clock into it somewhere so you can verify how fast the routines are ...

#10 plazter

  • Members
  • 134 posts

Posted 22 September 2013 - 02:48 PM

aw, it only ticks whenever a button is pressed :/

#11 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 22 September 2013 - 04: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...Coroutine_(API), http://computercraft.../Parallel_(API).

I think the parallel might work best for you. Here is my idea for your code:
Spoiler


#12 plazter

  • Members
  • 134 posts

Posted 29 September 2013 - 03:17 AM

View Postkreezxil, on 22 September 2013 - 04:19 PM, said:

--snip.

I think the parallel might work best for you. Here is my idea for your code:
Spoiler

Thanks man!
You're the best!, me and a mate fixed it :D
it works!! :D (Will post code here later)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users