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

Elevator using bundled wires and elevator rails

Started by Cred, 03 September 2012 - 05:11 PM
Cred #1
Posted 03 September 2012 - 07:11 PM
I am trying to make a wired elevator using Railcraft's elevator rails and cables from Redpower with the Computercraft computers acting as controllers. Here is what I've made in my test world:

User interface side
Back end wiring

I am not very experienced with programming, much less computercraft programming.

I currently have two computers and a button on each floor:
  • The button is for summoning the elevator. When pressed, it sends a signal through the cable bundle of its color.
  • The top computer is the user interface which will prompt the user to enter a destination. When a destination is input, it sends a signal into the cable bundle of the correct color for the requested floor.
  • The bottom computer just waits for a signal input from the cable bundle. When a signal comes in, it first checks to see what color the signal is on. If the color matches its floor, then the computer will toggle all signals on the bundle off and then turns the local black wire on. If the signal received on the cable does not match its floor then it will turn its local black wire off.
I am doing this with cables because I am unsure of how to use the wireless rednet and also because I personally think the cables are more cool. :D/>/>

I have what I thought would work for the bottom machine on each level but errors out saying "lift:3: bad argument: string expected, got nil". I know it is expecting a string now, but I don't want it to take the current value but to wait for a change in the bundle and then act.

Here is my current code for the floor controllers:

-- Floor 1 (pink)
while true do
  x = rs.getBundledInput(right)
  if x == "64" then
	rs.setBundledOutput("front", 0)
	rs.setOutput("right", true)
  else
	rs.setOutput("right", false)
  end
end

I haven't been able to come up with anything thats even plausibly workable for my user input other than this:

-- Floor 1 User (pink)
while true do
compID = os.getComputerLable()
  print("You are currently on "..compID..".")
  print("Please select a floor: ")
end
All this does so far is to spam that message until I force quit the program.

Any helpful advice would be appreciated.

Thanks,
Lettuce #2
Posted 03 September 2012 - 08:11 PM
You don't tell it to get the user's input. use os.pullEvent(), or io.read(). Otherwise it just loops print, which is why it's spammy.
Luanub #3
Posted 04 September 2012 - 12:05 AM
I use a menu for the user input of my elevator. Here's and example of how I do it.

Spoiler

local floor = "1" -- This computers floor
local cFloors = "" -- Current Floor Elevator is on.
local select = 2 -- where we want to start in the selection process.

local tFloor= {} -- table of available floors
tFloor[1] = "1st"
tFloor[2] = "2nd"
tFloor[3] = "3rd"

local tAction = {} -- table of functions
local tAction[1] = function()
tActions[1] = function() --move ele to 1st floor
c = 0
c = colors.combine(c, colors.white, colors.red)
rs.setBundledOutput("left", c)
end

tActions[2] = function()
c = 0
c = colors.combine(c, colors.orange, colors.magenta)
rs.setBundledOutput("left", c)
end

tActions[3] = function()
c = 0
c = colors.combine(c, colors.yellow, colors.lime)
rs.setBundledOutput("left", c)
end

local function getFloor()  -- find the elevator
local one = rs.testBundledInput("left", colors.red)
local two = rs.testBundledInput("left", colors.magenta)
local three = rs.testBundledInput("left", colors.lime)
if one then cFloor = "1"
elseif two then cFloor = "2"
elseif three then cFloor = "3"
end
end

local function drawMenu() -- print the menu
clear()
getFloor()
print("Currently on: "..cFloor)
write("Select Floor: "..tFloors[select])
end

while true do
local num = 1
drawMenu()
local evt, p1, p2 = os.pullEvent()
if evt == "key" then
  if p1 == 200 then -- up pressed
	 if select < #tFloors then
	  if tFloors[select + 1] == floor and select + 2 <= #tFloors then num = 2
	  elseif tFloors[select + 1] == floor and select + 2 > #tFloors then num = 0 end
	   select = select + num
	 end
  elseif p1 == 208 then -- down pressed
	  if select > 1 then
	  if tFloors[select - 1] == floor and select - 2 >= 1 then num = 2
	  elseif tFloors[select - 1] == floor and select - 2 < 1 then num = 0 end
		select = select - num
	  end
  elseif p1 == 28 then -- enter pressed
	if tActions[select] then
	  tActions[select]()
	end
  end
elseif evt == "redstone" and rs.getInput("right") then -- call the elevator
  tActions[1]()
elseif evt == "rednet_message" then
  local msg = tonumber(p2)
  tActions[msg]()
end
end

This is the main computer that is on floor 1 that controls the elevator. The other computers on the other floors have the same code only no table of function. Just a function that does a rednet.send() that sends this computer a message telling it what floor to go to. A redstone signal sent to the side of the computer will call the elevator to that floor.
Lettuce #4
Posted 04 September 2012 - 01:18 AM
(I posted this because I thought luanub was Cred, responding to me, and posting the full code for us all to look at. I didn't pay attention. If I could delete this I would, instead it will serve as a permanent testament to my absent-mindedness, unless a moderator sees it. Sorry.)