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

Need help for a program for a computer to stay active and react to a rs signal

Started by Redrah, 14 October 2016 - 11:14 PM
Redrah #1
Posted 15 October 2016 - 01:14 AM
How should I program a computer to send a redstone signal at the right side after 0.5 seconds everytime a redstone signal comes from the left side. And if there isn't a redstone signal the computer should wait for 3 seconds to turn the redstone signal which it has sent in the step before off.
The computer should be active every day so that I don't have to write the name of the program to activate the computer.

I am quite new in this area and I don't know much about programming.
hbomb79 #2
Posted 15 October 2016 - 03:44 AM
So, you want to computer to output a redstone signal 0.5 seconds after a signal is detected on the left?

You also want the computer to output a redstone signal 3 seconds after not signal is detected on the left?

If so, how long should the pulse be?
Bomb Bloke #3
Posted 15 October 2016 - 03:52 AM
That would depend on how long the left-hand input remains active.

local onTimer, offTimer
rs.setOutput("right", rs.getInput("left"))                     -- Set right side output to match left side input

while true do                                                  -- A loop that repeats indefinitely; see http://lua-users.org/wiki/ControlStructureTutorial
	local event, parameter = os.pullEvent()                -- Yield (do nothing) until an event goes into the event queue; http://www.computercraft.info/wiki/Os.pullEvent
	
	if event == "redstone" then                            -- Redstone input has changed in some way
		if rs.getInput("left") then                    -- http://www.computercraft.info/wiki/Redstone_(API)
			-- Signal is on
			onTimer = os.startTimer(0.5)           -- Timer event will be generated in half a second; http://www.computercraft.info/wiki/Os.startTimer
		
		else
			-- Signal is off
			offTimer = os.startTimer(3)
		end
		
	elseif event == "timer" then
		if parameter == onTimer then
			rs.setOutput("right", true)
			
			-- This next line assumes you want a *minimum* output duration of 3s, even
			-- if the input turned off before the output turned on; delete it if not:
			if not rs.getInput("left") then offTimer = os.startTimer(3) end  
		
		elseif parameter == offTimer then
			rs.setOutput("right", false)
		end
	end
end

Also see this article re auto-booting scripts.
Edited on 15 October 2016 - 02:18 AM
KingofGamesYami #4
Posted 15 October 2016 - 03:55 AM
I believe you want something like this:

local id1, id2
while true do
local event = {os.pullEvent()} --# get an event
if event[ 1 ] == "timer" and event[ 2 ] == id1 then --#if the initial 0.5s is up
rs.setOutput( "right", true ) --# turn on the redstone
id2 = os.startTimer( 3 ) --# start the second timer
elseif event[ 1 ] == "timer" and event[ 2 ] == id2 then --# if the second timer (3s) is up
rs.setOutput( "right", false ) --# turn off the redstone
end
if rs.getInput( "left" ) then --# if there's redstone input from the left
id1 = os.startTimer( 0.5 ) --# start the first timer (0.5s)
end
end
Normally I would not post code like this, but it's quite difficult to explain. I would recommend looking at the wiki page for any functions you don't understand, and asking follow up questions for any logic you do not understand.

Edit: Ninja'd
Edited on 15 October 2016 - 01:56 AM
Redrah #5
Posted 15 October 2016 - 09:38 AM
Thank you for your posts but it doesn't work as it should.
I try to explain in details what the computer should do.
A redstone torch powers sticky pistons to close hidden stairs and there has to be another sticky piston in the bootom of this construction which should be inactive if the others are activated. If I turn a leaver on the sticky pistons should get back in their starting position and the sticky piston in the bottom of this construction should get active after this whole process (after 3 seconds after someone has turned the leaver on). But if I turn the leaver off to close everything the sticky piston should get back in the starting position before the other sticky pistons even have reacted to the redstone signal. My idea was to connect the sticky pistons which are hiding the stairs with the redstone torch to be activated if the leaver is turned off and the sticky piston in the bottom with the leaver by itself to turn on or off. The sticky pistons hiding the stairs are working with repeaters but the sticky piston in the bottom has to turn on slowly if the leaver is activated and should get back fast in the starting position if the leaver is turned off.
The computer should stay active because it should stick in the ground so I don't have to turn it on and write the program name to run it. The computer should react to the redstone signal immediately and doesn't have to get turned off and on to react to it.
I have tried the other ideas but it doesn't work and I don't know why…
H4X0RZ #6
Posted 15 October 2016 - 11:30 AM
Can't you do this with vanilla redstone? Then you you don't have to care about persistence or chunk loading (which would influence the computer, a lot).
Redrah #7
Posted 15 October 2016 - 11:49 AM
I don't think it would work because if the piston is activated and the other pistons above are in their start position and I turn the leaver off the pistons would be faster activated as the piston under them would get back in its start position. That means the piston with the block on it would block the others pistons getting activated. Everything would be displaced and I have to rebuild it. The leaver should open it (bringing pistons to their start position faster than the piston under them get activated) and close it (piston should get back in its start position faster than the other pistons need to get activated).
So I can't use the same line. And I don't know how I could deactivate the line to use another line without using 2 different leavers.
Bomb Bloke #8
Posted 16 October 2016 - 07:32 AM
You could indeed set this up using vanilla blocks - you'd basically be relying on the Redstone Repeater's "signal delay" feature (an inverter or two would let you pick which signal to delay when). Catch is that'll only pause up to 0.4 seconds per block, so you'd need a few of the things. Assuming you can't afford to decrease your delays. Three seconds may be overkill.

To me, it'd be simpler to hook up buttons to the computer (as opposed to torches or levers), and have that computer handle all pistons. For eg, something along these lines:

local lonePistonSide    = "right"  -- Or name whatever side of the computer you want to connect this piston to.
local otherPistonsSide  = "back"   -- This wire goes to all the other pistons.
local redstoneInputSide = "left"   -- Connect all buttons to this side of the computer.

-- Script starting; make sure stairs reset to closed position:
rs.setOutput(lonePistonSide, true)
sleep(3)
rs.setOutput(otherPistonsSide, true)

while true do
	os.pullEvent("redstone")  -- Do nothing until a redstone event occurs.
	
	if rs.getOutput(lonePistonSide) then  -- If this output is already on, then...
		rs.setOutput(otherPistonsSide, false)
		sleep(3)
		rs.setOutput(lonePistonSide, false)
	else
		rs.setOutput(lonePistonSide, true)
		sleep(3)
		rs.setOutput(otherPistonsSide, true)
	end
end

Adjust according to your desired activation order, then hook up as many buttons to the computer as you like.