@schrolock:
This will turn on the redstone output on the back of the computer for .5 seconds and then turn it off again.
Then it will wait for 1 second and repeat the process. It will do all of this in an infinite loop.
local pulseLength = 0.5 -- Length of redstone pulse in seconds.
local delayLength = 1 -- Length of delay between pulses in seconds.
local side = "back" -- Side of the computer we want to control redstone signals for.
while true do
redstone.setOutput( side, true )
sleep( pulseLength )
redstone.setOutput( side, false )
sleep( delayLength )
end
Now, if you want to be able to listen for events while the redstone pulses, then it gets a bit more complicated.
Because sleep() waits itself for an event, we can't use it if we wait for events ourselves.
Well, we
could, but then our event loop wouldn't be interactive and we would always have to wait until sleep() finishes.
Therefore we can implement sleep() ourselves by making use of os.startTimer() instead:
local pulseLength = 0.5 -- Length of redstone pulse in seconds.
local delayLength = 1 -- Length of delay between pulses in seconds.
local side = "back" -- Side of the computer we want to control redstone signals for.
local pulseTimer = os.startTimer( pulseLength )
local delayTimer
redstone.setOutput( side, true ) -- Redstone initially turned on (will be turned back off after the first time pulseTimer fires).
while true do
local sEvent, param = os.pullEvent()
if sEvent == "timer" and param == pulseTimer then
redstone.setOutput( side, false ) -- Turn off redstone output
delayTimer = os.startTimer( delayLength ) -- (Re-)Start delayTimer.
end
if sEvent == "timer" and param == delayTimer then
redstone.setOutput( side, true ) -- Turn on redstone output
pulseTimer = os.startTimer( pulseLength ) -- Restart pulseTimer.
end
if sEvent == "char" and param == "q" then break end
end
Yet another (and for some use cases easier) way would be to make use of the 'parallel' API and let both the redstone pulse and our own event listener run in spearate threads.
We have to wrap their respective code in functions then, though:
function redstonePulser()
local pulseLength = .5 -- Length of redstone pulse in seconds.
local delayLength = 1 -- Length of delay between pulses in seconds.
local side = "back" -- Side of the computer we want to control redstone signals for.
while true do
redstone.setOutput( side, true )
sleep( pulseLength )
redstone.setOutput( side, false )
sleep( delayLength )
end
end
function eventProcessing()
local side = "back" -- Side of the computer we want to control redstone signals for.
local maxProgramTimer = os.startTimer( 60 )
while true do
local sEvent, param = os.pullEvent()
if sEvent == "char" and param == "q" then -- 'q' breaks out of the loop, ends the function and therefore the whole program.
break
end
if sEvent == "timer" and param == maxProgramTimer then -- After 60 seconds, breaks out of the loop, ends the function and therefore the whole program.
break
end
end
end
parallel.waitForAny( redstonePulser, eventProcessing )