This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Smidds's profile picture

[Lua][Question] Timers?

Started by Smidds, 07 May 2012 - 10:52 PM
Smidds #1
Posted 08 May 2012 - 12:52 AM
I've been trying to get a redstone door to operate. I've got a set of code to tell the RedPower bundle to send power to the door, and that part works. However, when I try to utalize a timer, it doesn't cut the redstone supply. I'm not entirely sure how to use Timers, even though I've done some searching around.. HERE and a bit of HERE (The problem with that last one, is that it doesn't entirely apply to my situation, and I've got no idea how to fix it..)
So here's what I've come up with…

term.clear()
term.getCursorPos(1,1)
print ("Welcome to the Redstone Program!\n")
print ("How may I help you?")
print ("O-----oOo-----O")
print ("~1 Open Door")
print ("~2 Feed Me!")
print ("~3 Gimme some water!")
print ("~4 Some Iron please?")
print ("~5 Turn on lights")
print ("~6 Turn off lights")
print ("O-----oOo-----O")
write ("-")
choice = io.read()
if choice == "1" then
print ("You have choosen for me to open the door!")
print ("Remeber, you only have 5 seconds!")
print ("")
rs.setBundledOutput("front", colors.white)
	event, param1 = os.pullEvent()
	if event == "timer" and param1 == 5 then
	rs.setBundledOutput("front",
	rs.getBundledOutput("front") - colors.white)
	end
end

And to zoom into the point of interest,

if choice == "1" then
print ("You have choosen for me to open the door!")
print ("Remeber, you only have 5 seconds!")
print ("")
rs.setBundledOutput("front", colors.white)
	event, param1 = os.pullEvent()
	if event == "timer" and param1 == 5 then
	rs.setBundledOutput("front",
	rs.getBundledOutput("front") - colors.white)
	end
end

To clarify, I'm trying to get the door open, wait 5 seconds, then close the door/cut off the redstone circuit. I'm not getting any errors, so I'm not sure where to go from here… any help would be GREATLY appreciated! Thanks! :)/>/>
MysticT #2
Posted 08 May 2012 - 01:12 AM
To use timers you have to start one first, and then make a loop that checks for events:

local timer = os.startTimer(time) -- change time to the time you want for it
while true do
  local sEvt, arg = os.pullEvent("timer")
  if arg = timer then
    -- time passed, do something here
  end
end
But for what you want to do, it's easier to use sleep:

-- some code
sleep(5) -- wait 5 seconds
-- more code
Smidds #3
Posted 08 May 2012 - 04:35 AM
To use timers you have to start one first, and then make a loop that checks for events:

local timer = os.startTimer(time) -- change time to the time you want for it
while true do
  local sEvt, arg = os.pullEvent("timer")
  if arg = timer then
	-- time passed, do something here
  end
end
But for what you want to do, it's easier to use sleep:

-- some code
sleep(5) -- wait 5 seconds
-- more code

Thank you so much! I'll try this out, and get back to you later :)/>/>