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

Parallel elevator control program

Started by Aerik, 21 December 2012 - 07:58 AM
Aerik #1
Posted 21 December 2012 - 08:58 AM
Dear LUA Pro's,

I'm having a bit of an issue getting a piece of code to work, even after browsing around these forums for what feels like hours.
The program is designed to control an elevator built with the mod ugocraft, and involves computers on every floor, combined with a central server to process rednet messages

The problem I'm having involves the program that is supposed to wait for one of four possible events to happen:
1. It receives a rednet message, containing a number that when compared to it's own floor ID tells it whether or not to turn on a rs signal
2. It receives a redstone pulse from the left side, telling it to send a rednet message to the server, 'calling' the elevator
3. It receives user input, in the form of the target floor number, e.g. 1,0 or -8, and sends the associated number the server
4. It receives a redstone pulse from the right side, terminating the program

The problem I'm having is that when the program runs, I cannot enter any input, nor will supplying a redstone signal on any side have an effect.

I'm hoping for any feedback that could help me fix and improve this program!

The client program can be found here, and the server program here

Main part of client program (not full program!):


function listen()
while true do
  local sendID, obj, distance = rednet.receive()
  if sendID == serverID then
   if obj >= floor then
    rs.setOutput("bottom", true)
   else
    rs.setOutput("bottom", false)
   end
  end
end
end
function call()
while true do
  if rs.getInput("left") == true then
   rednet.send(serverID, "call")
  end
end
end
function input()
while true do
  local input = read()
  if input == "quit" then
   banana = false
   shell.quit()
  end
  for k,v in pairs(floortable) do
   if v == input then
    rednet.send(serverID, k)
   else
    term.setCursorPos(4,12)
    term.write("Invalid floor number")
   end
  end
end
end
function finish()
if rs.getInput("right") == true then
  shell.exit()
end
end
while banana == true do
gui()
parallel.waitForAny(listen, call, input, finish)
end
Lyqyd #2
Posted 21 December 2012 - 09:02 AM
The finish function completes immediately.
Doyle3694 #3
Posted 21 December 2012 - 09:39 AM
With that being said, finish() should be changed to

function finish()
   while true do
	  if rs.getInput("right") then
		shell.exit()
	  end
	  sleep(0)
   end
end
remiX #4
Posted 21 December 2012 - 10:39 AM
Doyle, would that not error because of no yield? And can't you omit the "== true" part?
Lyqyd #5
Posted 21 December 2012 - 11:30 AM
It could probably be something like:


function finish()
  repeat until os.pullEvent("redstone") and rs.getInput("left")
  shell.exit()
end
Doyle3694 #6
Posted 21 December 2012 - 11:54 AM
You are ever so true remiX, I'm not being a good teacher leaving such things out am I :P/> edited my post for better code

And @Lyqyd, wouldn't a explanation for your code be good? I think that code will look abit complicated to someone not so advanced at CC, not sure how advanced OP is though.
Anyways: explanation follows:

"repeat until" just starts a repeat loop, which will continue doing the same thing until the condition after "until" is met. here the things it's doing is nothing, so how does it work you say? well, the condition starts with "os.pullEvent("redstone"), so it will run os.pullEvent() checking for a redstone update, and until there is a redstone update it will essentially just wait. then, when it gets the redstone update, os.pullEvent() returns a string as it's first parameter, which is automatically true, so half the and statement is met.

Then, rs.getInput("left") is ran, checking if the redstone is turned on to the left, because the os.pullEvent("redstone") just checks for a redstone update, not a specific side of it or it being actually turned on. So checking if there is active redstone on the left side returnes either true or false. if false then it will just start the repeat loop over and over, and essentially just doing the same thing until the redstone on teh left is active.
Aerik #7
Posted 21 December 2012 - 02:12 PM
Alright, I tried implementing both suggested solutions, no luck. The program simply does not yield, cannot be terminated, and the computer cannot be restarted.

Any other ideas?
ChunLing #8
Posted 21 December 2012 - 02:38 PM
Whoa, you didn't mention that part before. Is there any output on the console you'd like to share?
Aerik #9
Posted 21 December 2012 - 03:12 PM
It prints what I tell it to print before the parallel functions start, then just freezes up…

And sorry for not mentioning that more clearly, although I did state it didn't accept any input.
ChunLing #10
Posted 21 December 2012 - 03:16 PM
So Ctrl+S and Ctrl+R don't reset it?

You know what, I don't like that you have a global function called call. Anyone think that might be a problem?
Edited on 21 December 2012 - 02:22 PM
Aerik #11
Posted 21 December 2012 - 03:25 PM
You got a good point there, that was kind of a stupid move. I´ll try changing that first thing in the morning!