Only triggering twice!
I added a counter in the display update function to show how many times the function is called. It gets called three times.
Here is the simplified code as it stands now...
local modem = peripheral.wrap("back")
local count = 1
modem.open(555)
term.clear()
local computerInRange = true
-- Function transmit
--
-- Transmits a message and gets a response.
-- If no response in 4 seconds, returns false.
local function transmit(port, message)
modem.transmit(port, 555, message)
local timeout = os.startTimer(4)
while true do
local e, p1, p2, p3, p4, p5 = os.pullEvent()
if e == "timer" then
if p1 == timeout then
return false
end
end
if e == "modem_message" and p2 == 555 then
return p4
end
end
end
local function checkIn()
local reply = transmit(1, "Hello?")
if reply == false then
computerInRange = false
return true
elseif reply == "Hi!" then
return true
end
return false
end
local function updateDisplay()
term.setCursorPos(1,1)
term.write(tostring(count))
count = count + 1
end
updateDisplay()
while true do
local myTimer = os.startTimer(2)
local event, p1, p2, p3, p4, p5 = os.pullEvent()
if event == "timer" then
if p1 == myTimer then
updateDisplay()
if checkIn() == false then break end
end
end
-- Other events
end
PS I am a seasoned ex-programmer. I had over 10 years professional C/C++ programming before I was forced to quit for family reasons
EDIT - I ran the above code as a test program and the timer was triggered 3 times (the updateDisplay was called 4 times) then stopped!
I adjusted the value from 2 seconds to 4 seconds and it works fine!
Edited by Galbi3000, 10 March 2015 - 03:06 AM.