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

Help with timer

Started by asnarr204, 03 April 2015 - 05:28 PM
asnarr204 #1
Posted 03 April 2015 - 07:28 PM
I am still quite new a computercraft and programming and am trying to make a program that uses wireless modems. For some reason the program gets stuck at this os.pullevent() instead of stopping it after 20 seconds like I want it to. Anyone know why it's not working?


local function waitForReply()
  local maxTime = 60 --in seconds
  local iterations = 5 --number of times tried
  local count = 0
  local timeLeft = 0
  local correctReply = false
  local correctEvent = false

  while count < iterations and (not correctReply or not correctEvent) do
    timeLeft = maxTime - count * (maxTime / iterations)
    print(" ")
    print("listening for a confirmation of command execution...")
    term.write("if a valid reply is not received within aproximatly ")
    moveCursorDown()
    term.setTextColor(userTextColor)
    term.write(timeLeft)
    term.setTextColor(promptTextColor)
    print(" seconds the attempt will be abandoned")
    count = count + 1
    os.startTimer(20)
    event, side, senderChannel, replyChannel, message, distance = os.pullEvent("modem_message")
    if event == "modem_message" then
	  correctEvent = true
    end
    if message == distance then
	  correctReply = true
    end
  end
  print("")
  if not correctReply and not correctEvent then
    term.setTextColor(colors.red)
    print("Command Execution Failed")
  else
    term.setTextColor(colors.green)
    print("Success! Command Executed")
  end
  term.setTextColor(promptTextColor)
end
asnarr204 #2
Posted 03 April 2015 - 07:38 PM
Forget what I said. I see it now that I have a better view of the code.