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

Creating a Call Elevator Function as part of an ElevatorAPI

Started by nalexander50, 23 March 2013 - 06:05 AM
nalexander50 #1
Posted 23 March 2013 - 07:05 AM
I am creating an elevator using Frames & Frame Motors and trying to control it all by using computers. I have a house that is 3 floors, a roof, and a basement – each with 4 blocks between. I have written functions for moveUp and moveDown (code below). Now I am trying to make a callElevator function to make the elevator rise or fall to your current floor. I am trying to bundle this into an API as well. The code I have written already is saved as elevatorAPI on a floppy disk:
– Note, 0 blocks = basement,
4 blocks = ground floor
8 blocks = 2nd floor
12 blocks = 3rd floor
16 blocks = roof
– will use in the code

function moveUp()
  side = "left" -- denotes the side where the cables are attached
  color = colors.orange -- orange cable activates UP motor
  currentBlocks = 1  -- Number of  blocks ranging from 16 to 0 to denote position of elevator
  floor = tonumber(io.read())
  floors = floor * 4 --used in for loop

  if floors > 14 then -- prevents elevator from raising off the motors
	for i=currentBlocks, 14 do
	  rs.setBundledOutput(side, color)
	  sleep(1)
	  rs.setBundledOutput(side, 0)
	  sleep(1)
	end
  else
	for i=currentBlocks, floors do
	  rs.setBundledOutput(side, color)
	  sleep(1)
	  rs,setBundledOutput(side, 0)
	  sleep(1)
	end
  end
end


I have the exact same code written for the moveDown() function except with the obvious changes to make the elevator descend. Now, I am trying to find a way to make a callElevator() function and tried for 4 hours last night until I passed out. I am seeking guidance, not necessarily someone writing it for me at all.

Also, note I have a computer set up at the elevator door on each floor all connected to the same cables to the same motor. Need any more information I will gladly present it. Please help! Also, I accept any optimization tips. I am trying to learn to program as best I can.
OmegaVest #2
Posted 23 March 2013 - 07:41 AM
Hmm. Well, you could always make it so that the computers send a rednet signal with their floor number when the elevator hits/passes their floor. When you want to call, compare the last received signal with the desired one, and determine how to move.

ie.

thisFloor = 1
atFloor = 1
floorDiff = 4

while true do
  evt, arg1, arg2, arg3 = os.pullEvent()
  if evt == key and arg1 == keys.c then
    if atFloor == thisFloor then
      print("Already at this floor, cannot call.")
    elseif atFloor > thisFloor then
      targer = atFloor - thisFloor
      targer = targer*floorDiff
      for i = 1, targer do
        moveDown()
      end
    elseif atFloor < thisFloor then
      targer = thisFloor - atFloor
      targer = targer*floorDiff
      for i = 1, targer do
        moveUp()
      end
    end
    rednet.broadcast("elev "..tosrting(thisFloor))  -- I think the tostring works here, can't remember.
  end
end

And, of course, you'd have to figure out how to tell if the elevator would know if it reached its floor. a torch would probably work, just make the computer look for the torch's signal to reach it.

Lastly, the above code is only an example. I have no idea if it would work, and I would suggest you only use it as an outline at best.