There's never any need to check if something "==true". If it
already evaluates as "true", why perform the extra comparison? For eg, this is perfectly valid:
if(redstone.getInput("left")) then
Under Lua, you can also drop the brackets around the condition, if you like. But the "==true" thing is unnecessary under
any language.
If a given program runs for more then about ten seconds without yielding ("waiting"), the Lua interpretor will kill it. Your periscope program yields once every iteration of its while loop thanks to the use of "os.pullEvent()", but your sending program doesn't yield at all.
Fortunately, every time a redstone input on the computer changes, that triggers an event. This means you can have the sending machine constantly yield while waiting for "os.pullEvent()" to pull that event, rather then spamming modem messages flat out.
Here's a proposed re-write for the loop on the periscope side:
while(true) do
local event, modemside, senderchannel, replychannel, message = os.pullEvent("modem_message")
if (message == "red") then
paintutils.drawPixel(8, replychannel*3+1, colour.red)
elseif (message == "green") then
paintutils.drawPixel(8, replychannel*3+1, colour.lime)
end
end
Note that you can omit variables you're not interested in when pulling a list of the things from a function. "message" is the last one we need from the os.pullEvent() statement, so "senderdistance" can be dropped.
Then on the other machines, our loop would be something like:
while(true) do
if(redstone.getInput("left")) then
modem.transmit(3, 0, "red")
else
modem.transmit(3, 0, "green")
end
os.pullEvent("redstone") -- Sit and do nothing until the redstone input changes.
end
Then on the next sensor machine, you'd use:
while(true) do
if(redstone.getInput("left")) then
modem.transmit(3, 1, "red")
else
modem.transmit(3, 1, "green")
end
os.pullEvent("redstone")
end
… and so on. Sure, this probably isn't the intended use of the "replyChannel" variable, but hey, why not?
It could be condensed down a fair bit more, if you're interested in seeing a few other concepts explained.
Fair warning, I haven't tried making a monitor yet, let alone tried
this code, so I'm not 100% sure it'll all display as you want it to.