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

I need help making a program that if a turtle doesn't detect a redstone signal for a amount of time to send a redstone signal

Started by koolboy5783, 07 January 2014 - 02:48 PM
koolboy5783 #1
Posted 07 January 2014 - 03:48 PM
I need help making a program that if a turtle doesn't detect a redstone signal for a amount of time to send a redstone signal, and restart the program. I am not good at LUA at all, so I don't even know where to start. Thanks for all your help.
Lyqyd #2
Posted 07 January 2014 - 04:04 PM
Please post the code you've come up with so far.
koolboy5783 #3
Posted 07 January 2014 - 04:51 PM
All I could get is to check if there is a redstone signal and pulse one:


if rs.getInput("back")

redstone.setOutput("right", true)
sleep(0.5)
redstone.setOutput("right", false)
sleep(0.5)
Bomb Bloke #4
Posted 07 January 2014 - 06:59 PM
Spoiler
local inSide, outSide, timerLength = "back", "right", 5  -- "inSide" is set to "back", "outSide" is set to "right", etc
local myTimer, event, par1   -- Declaring variables as "local" to the script ensures they're cleared from memory when it ends.

-- Wait for the redstone input to turn off before starting (assuming it's on).
while rs.getInput(inSide) do
  os.pullEvent("redstone")
end

myTimer = os.startTimer(timerLength)    -- A timer event will fire after the specified number of seconds pass.

while true do                   -- Start a loop that will repeat indefinitely.
  event, par1 = os.pullEvent()  -- Wait for an event (eg a change to redstone, or for the timer to expire).

  if event == "timer" and par1 == myTimer and not rs.getInput(inSide) then  -- Note the "then".
    -- Our timer ended before the input turned on.

    rs.setOutput(outSide,true)
    sleep(0.5)
    rs.setOutput(outside,false)

    -- If the input turned on while pulsing, then wait for it to turn off.
    while rs.getInput(inSide) do os.pullEvent("redstone") end

    -- Start a new timer.
    myTimer = os.startTimer(timerLength)
  elseif rs.getInput(inSide) then
    -- The redstone input turned on.

    -- Wait for it to turn off:
    while rs.getInput(inSide) do os.pullEvent("redstone") end

    -- Start a new timer.
    myTimer = os.startTimer(timerLength)
  end
end

Not tested.

I recommend reviewing os.pullEvent().
Edited on 07 January 2014 - 06:02 PM
koolboy5783 #5
Posted 07 January 2014 - 07:50 PM
Spoiler
local inSide, outSide, timerLength = "back", "right", 5  -- "inSide" is set to "back", "outSide" is set to "right", etc
local myTimer, event, par1   -- Declaring variables as "local" to the script ensures they're cleared from memory when it ends.

-- Wait for the redstone input to turn off before starting (assuming it's on).
while rs.getInput(inSide) do
  os.pullEvent("redstone")
end

myTimer = os.startTimer(timerLength)	-- A timer event will fire after the specified number of seconds pass.

while true do				   -- Start a loop that will repeat indefinitely.
  event, par1 = os.pullEvent()  -- Wait for an event (eg a change to redstone, or for the timer to expire).

  if event == "timer" and par1 == myTimer and not rs.getInput(inSide) then  -- Note the "then".
	-- Our timer ended before the input turned on.

	rs.setOutput(outSide,true)
	sleep(0.5)
	rs.setOutput(outside,false)

	-- If the input turned on while pulsing, then wait for it to turn off.
	while rs.getInput(inSide) do os.pullEvent("redstone") end

	-- Start a new timer.
	myTimer = os.startTimer(timerLength)
  elseif rs.getInput(inSide) then
	-- The redstone input turned on.

	-- Wait for it to turn off:
	while rs.getInput(inSide) do os.pullEvent("redstone") end

	-- Start a new timer.
	myTimer = os.startTimer(timerLength)
  end
end

Not tested.

I recommend reviewing os.pullEvent().
That is just what I wanted. Thanks! :D/>