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

[error]

Started by Zhaedst, 31 October 2012 - 04:43 PM
Zhaedst #1
Posted 31 October 2012 - 05:43 PM
Im having trouble getting this to work right. It's an elevator operation code that uses frame motors, brick breaker, and deployer.

Also sorry Im not sure how to post code here otherwise.


–[[Frames Elevator code
Elevator will be lowered from roof of building.
When the elevator goes up a frame will be removed with a block breaker and sent to the deployer through pneumatic pipe.
When the elevator goes down a frame will be placed on top from a deployer.

Requirements

Will detect a redstone pulse from the right from either yellow (up) or blue (down).
On a yellow pulse it will send a redstone signal to the left to black (block breaker) then again left on white (frame motor up)
On a blue pulse it will send a redstone signal to the right to red (frame motor down) then a signal on the right to green (deployer)
The loop will operate 5 times (height of floors)
Additional ideas: replace yellow and blue buttons with rednet signals and have a selection of floors with a variable indicating which floor the elevator is on.
]]–
While true do
term.clear
term.setCursorPos(1,1) – clear and set cursor position
print("awaiting call") – tells operator that the program is looking for a signal
if rs.getInput("left") then – looks for an input
if colors.test(colors.yellow) then – is the
t = 0 – sets t (floor height counter) to 0
term.clear
term.setCursorPos(1,1) – clear and set cursor position
print("elevator moving up") – tells operator that the elevator is moving up
repeat – starts a repeat loop between the block breaker and the frame motor (up)
rs.setBundledOutput("right", colors.black) – turns on block breaker
rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.black) – turns off block breaker
os.sleep(1) – this sleep is here to give the block breaker time to operate
rs.setBundledOutput("right", colors.white) – turns on frame motor (up)
rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.white) – turns off frame motor (down)
os.sleep(1) – this sleep is here to give the frame motor time to operate
t = t + 1 – loop counter
until t == 5 – operates until next floor
end – ends repeat loop
if colors.test(colors.blue) then – if the signal is blue it will break here.
t = 0 – sets t (floor height counter) to 0
term.clear
term.setCursorPos(1,1) – clears and sets cursor position
print("elevator moving down") – tells the operator that the elevator is moving down
repeat – start of repeat loop between the frame motor (down) and the deployer
rs.setBundledOutput("right", colors.red) – turns on frame motor (down)
rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.red) – turns off frame motor (down)
os.sleep(1) – this sleep to give the frame motor time to operate
rs.setBundledOutput("right", colors.green) – turns on deployer
rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.green) – turns off deployer
os.sleep(1) – this sleep to give the deployer time to operate
t = t + 1 – loop counter
until t == 5 – operates until next floor
end – ends the loop
end – ends identify input color
end – ends detect loop
– At this point it should go back to the begining to wait for redstone signals from blue or yellow. I'm not sure how to do that in Lua.
exploder #2
Posted 31 October 2012 - 07:39 PM
Well, if I understand this correctly, you want to code to start again from the beginning?

If so, you could just make your code a function and make computer run it again at end.

Like this:

Spoiler

function elevator() -- Declares a function.
while true do
term.clear()
term.setCursorPos(1,1) -- clear and set cursor position
print("awaiting call") -- tells operator that the program is looking for a signal
if rs.getInput("left") then -- looks for an input
if colors.test(colors.yellow) then -- is the
t = 0 -- sets t (floor height counter) to 0
term.clear() -- Found a mistake on this line.
term.setCursorPos(1,1) -- clear and set cursor position
print("elevator moving up") -- tells operator that the elevator is moving up
repeat -- starts a repeat loop between the block breaker and the frame motor (up)
rs.setBundledOutput("right", colors.black) -- turns on block breaker
rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.black) -- turns off block breaker
os.sleep(1) -- this sleep is here to give the block breaker time to operate
rs.setBundledOutput("right", colors.white) -- turns on frame motor (up)
rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.white) -- turns off frame motor (down)
os.sleep(1) -- this sleep is here to give the frame motor time to operate
t = t + 1 -- loop counter
until t == 5 -- operates until next floor
end -- ends repeat loop
if colors.test(colors.blue) then -- if the signal is blue it will break here.
t = 0 -- sets t (floor height counter) to 0
term.clear() -- Another mistake.
term.setCursorPos(1,1) -- clears and sets cursor position
print("elevator moving down") -- tells the operator that the elevator is moving down
repeat -- start of repeat loop between the frame motor (down) and the deployer
rs.setBundledOutput("right", colors.red) -- turns on frame motor (down)
rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.red) -- turns off frame motor (down)
os.sleep(1) -- this sleep to give the frame motor time to operate
rs.setBundledOutput("right", colors.green) -- turns on deployer
rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.green) -- turns off deployer
os.sleep(1) -- this sleep to give the deployer time to operate
t = t + 1 -- loop counter
until t == 5 -- operates until next floor
elevator() -- Runs the function again.
end -- ends the loop
end -- ends identify input color
end -- ends detect loop
end -- Another end for the function.

Fixed some mistakes you made with term.clear().You wrote them without brackets.


This isn't tested though, but I guess you just wanted to code to repeat, so I added function that makes it repeat if it reaches end of the code. If I didn't fix your problem don't be afraid to ask :P/>/>.
remiX #3
Posted 31 October 2012 - 08:18 PM
Use a os.pullEvent("redstone") rather :P/>/> And you made many mistakes like: term.clear instead of term.clear()


rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.red)
Does that work? o.O.
And exploder , if you keep calling a function like that it will get a stack overflow. You won't need to make it a function if there is a while true do …

Edit: try this

Spoiler

while true do
    term.clear()
    term.setCursorPos(1,1)
    print("Awaiting call")
    os.pullEvent("redstone")   -- So it only continues if it finds a redstone event
    term.clear()
    term.setCursorPos(1,1)
    if colors.test(colors.yellow) then
        print("elevator moving up")
        for i = 1, 5 do
            rs.setBundledOutput("right", colors.black)
            rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.black)
            os.sleep(1)
            rs.setBundledOutput("right", colors.white)
            rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.white)
            os.sleep(1)
        end
    elseif colors.test(colors.blue) then
        print("elevator moving down")
        for i = 1, 5 do
            rs.setBundledOutput("right", colors.red)
            rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.red)
            sleep(1)
            rs.setBundledOutput("right", colors.green)
            rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.green)
            sleep(1)
        end
    end
end
exploder #4
Posted 31 October 2012 - 08:55 PM
Use a os.pullEvent("redstone") rather :P/>/> And you made many mistakes like: term.clear instead of term.clear()


rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.red)
Does that work? o.O.
And exploder , if you keep calling a function like that it will get a stack overflow. You won't need to make it a function if there is a while true do …

Edit: try this

Spoiler

while true do
	term.clear()
	term.setCursorPos(1,1)
	print("Awaiting call")
	os.pullEvent("redstone")   -- So it only continues if it finds a redstone event
	term.clear()
	term.setCursorPos(1,1)
	if colors.test(colors.yellow) then
		print("elevator moving up")
		for i = 1, 5 do
			rs.setBundledOutput("right", colors.black)
			rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.black)
			os.sleep(1)
			rs.setBundledOutput("right", colors.white)
			rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.white)
			os.sleep(1)
		end
	elseif colors.test(colors.blue) then
		print("elevator moving down")
		for i = 1, 5 do
			rs.setBundledOutput("right", colors.red)
			rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.red)
			sleep(1)
			rs.setBundledOutput("right", colors.green)
			rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.green)
			sleep(1)
		end
	end
end


I was a bit in a hurry and missed that, but thanks for correcting me :P/>/>.
Luanub #5
Posted 31 October 2012 - 09:06 PM
Instead of using colors.test() you will want to use rs.testBundledInput()

Spoiler



while true do
    term.clear()
    term.setCursorPos(1,1)
    print("Awaiting call")
    os.pullEvent("redstone")   -- So it only continues if it finds a redstone event
    term.clear()
    term.setCursorPos(1,1)
    if rs.testBundledInput("right",colors.yellow) then
        print("elevator moving up")
        for i = 1, 5 do
            rs.setBundledOutput("right", colors.black)
            rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.black)
            os.sleep(1)
            rs.setBundledOutput("right", colors.white)
            rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.white)
            os.sleep(1)
        end
    elseif rs.testBundledInput("right",colors.blue) then
        print("elevator moving down")
        for i = 1, 5 do
            rs.setBundledOutput("right", colors.red)
            rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.red)
            sleep(1)
            rs.setBundledOutput("right", colors.green)
            rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.green)
            sleep(1)
        end
    end
end

Also I dont think that this is going to work

rs.setBundledOutput("right", colors.black)
rs.setBundledOutput("right", rs.getBundledOutput("right") - colors.black)

If what your trying to do is turn black on sleep 1 second then turn it off and white on all you need to do is..

rs.setBundledOutput("right", colors.black)
sleep(1)
rs.setBundledOutput("right", colors.white)

And to turn everything off use

rs.setBundledOutput("right", 0)

If your going to handle multiple colors at a time I would use the colors API to handle the colors for you but it does not look like that is what you doing.
For future reference:
Spoiler

local c = 0 --declares c as 0 or all colors off

c = colors.combine(c, colors.black)
rs.setBundledOutput("right",c) -- turn on black

c = colors.combine(c, colors.white)
rs.setBundledOutput("right",c) -- turn on white, leaving black on.

c = colors.subtract(c, colors.black)
rs.setBundledOutput("right",c) -- turn off black leaving white on.

--type help redpower from a terminal for more info
remiX #6
Posted 31 October 2012 - 09:19 PM
Yeah luanub, I haven't used bundled cabling cabling in lua coding before so I have no clue how it works :P/>/>