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

os.startTimer much shorter than expected

Started by martok111, 01 April 2017 - 03:28 PM
martok111 #1
Posted 01 April 2017 - 05:28 PM
I'm trying to create a 1.5 hour delay in my program using os.startTimer(5400). My understanding is that this runs a timer for 5400 seconds (90 minutes). Instead, the timer event triggers after less than a minute.


rednet.open("top")
completed = 1
while true do
  if completed ~= 0 then
	redstone.setOutput("bottom", false)
	timer = os.startTimer(3600)
	print("Started ", timer, " ", os.time())
	completed = 0
  end
  local event = os.pullEvent()
  print(event)
  print(timer)
  print(os.time())
  if (event == "redstone" and redstone.getInput("right") == true) or event == "timer" then
	redstone.setOutput("bottom", true)
	os.sleep(60)
	redstone.setOutput("bottom", false)
	os.sleep(30)
	rednet.broadcast("farm", "farm")
	print("Planting")
	os.cancelTimer(timer)
	completed = 1
  end
end

Through debug outputs, I've determined that it is the timer event causes it, and the timer id is the one expected. I've also added os.time as an output and the time difference is about 0.6. oddly this was the same whether the delay was 5400 or 3600.
Bomb Bloke #2
Posted 02 April 2017 - 12:43 AM
Your code is not checking that the timers which complete have the ID of the original. Try it like this:

local timerLength = 3600

rednet.open("top")
local timer = os.startTimer(timerLength)
redstone.setOutput("bottom", false)

while true do
	local event, par1 = os.pullEvent()
	
	if (event == "redstone" and rs.getInput("right")) or (event == "timer" and par1 == timer) then
		redstone.setOutput("bottom", true)
		sleep(60)
		redstone.setOutput("bottom", false)
		sleep(30)
		
		rednet.broadcast("farm", "farm")
		print("Planting")
		
		os.cancelTimer(timer)
		timer = os.startTimer(timerLength)
	end
end
martok111 #3
Posted 02 April 2017 - 05:06 PM
Oh! I understand now. Took me until now to realize the difference. Thank you!!

Seems to be working perfectly now!

I wonder where the other timer event was coming from.
Bomb Bloke #4
Posted 03 April 2017 - 12:42 AM
Rednet generates them every now and then. Because the repeat script exists, it's possible for computers to receive multiple copies of the same message; hence as each message comes in, systems note their UUIDs and start a timer. If they see more copies of the message with the same UUID before that timer completes, they know to ignore them.