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

[Question][Lua] Elevator code

Started by MetalGearMuffin, 11 September 2012 - 11:44 AM
MetalGearMuffin #1
Posted 11 September 2012 - 01:44 PM
Hey everyone, I had a bit of a dumb question no doubt but I have been stuck for most of a day with this. I'm trying to write a simple code for an elevator, with one computer being on the platform itself able to send commands to a receiving terminal that does the actual moving of the elevator, I've gotten the programs to move the elevator to work in a program called "Up" and one called "Down" the problem however lies in the receiving terminal, I can't get it to receive the proper message to move the elevator. Just for overkills sake I'll post all of the code I've written so far any help is welcome! :D/>/>

Up program
Spoileros.sleep(2)
for x = 1, 11, 1 do
rs.setBundledOutput("bottom", 1)
os.sleep(0.4)
rs.setBundledOutput("bottom", 0)
os.sleep(0.4)
end

Down Program
Spoileros.sleep(2)
for x = 1, 11, 1 do
rs.setBundledOutput("bottom", 2)
os.sleep(0.4)
rs.setBundledOutput("bottom", 0)
os.sleep(0.4)
end

Wireless program
Spoilerterm.clear()
term.setCursorPos(1,1)
print("Wireless mode on")
rednet.open("back")
while message ~= "Quit" do
message = rednet.receive(message)
term.write(message)
if message == "Up" then
shell.run("Up")
print("Going up!")
end
end
term.clear()

Edit: In case it's needed, I also written an Elevator program with the Up and Down as functions

Spoilerfunction Up()
for x = 1, 11, 1 do
rs.setBundledOutput("bottom", 1)
os.sleep(0.4)
rs.setBundledOutput("bottom", 0)
os.sleep(0.4)
end
end

function Down()
for x = 1, 11, 1 do
rs.setBundledOutput("bottom", 2)
os.sleep(0.4)
rs.setBundledOutput("bottom", 0)
os.sleep(0.4)
end
end
OmegaVest #2
Posted 11 September 2012 - 03:27 PM
Uh. I'm pretty sure the parameters for rednet.receive is a number, and that being how long the computer is supposed to listen. If you want it to store the message, you just need the message = bit.

Other than that, it should work out just fine. . . unless you use lowercase letters for the message, which will not be parsed correctly, as Lua is case-sensitive.
icehaunter #3
Posted 12 September 2012 - 12:04 AM
I think i know what is the problem. Look, rednet.recieve() gives out THREE variables: id, message and distance in that exact order. So if you want your code to work:

id, message = rednet.recieve()
instead of
message = rednet.recieve(message)
Hope that helped ^^
MetalGearMuffin #4
Posted 13 September 2012 - 12:37 PM
I think i know what is the problem. Look, rednet.recieve() gives out THREE variables: id, message and distance in that exact order. So if you want your code to work:

id, message = rednet.recieve()
instead of
message = rednet.recieve(message)
Hope that helped ^^

Yeah it did help, thanks a lot!