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

[Help][Error] Elevator Multi-Floor control

Started by ChaosSorcerer, 18 March 2013 - 05:14 AM
ChaosSorcerer #1
Posted 18 March 2013 - 06:14 AM
This is my first large project code so it is most likely to be sloppy and have many errors and is probably over complicated. The setup
currently consists of 11 different computers, each with a wireless modem. There are 10 floors, each with their own computer which will transmit to one central computer which we keep track of the current floor, move the elevator when it receives a message from one of the computers or receives a redstone input from a wireless retriever through bundled cables. Currently the central elevator code, or Elevator_Main will seem to crash the computer and give the following error message: nil: vm error: java.lang.NullPointerException.
The current version of Computer Craft is 1.3
The code is as follows:

local modem = peripheral.wrap("top")
rednet.open("top")
a = 0
b = 0
function Floors()  --Relates the message send by wireless modems to a number value
while true do
  if message == "0" then
   b = 0

  elseif message == "18" then
   b = 18

  elseif message == "30" then
   b = 30

  elseif message == "42" then
   b = 42

  elseif message == "54" then
   b = 54

  elseif message == "66" then
   b = 66

  elseif message == "78" then
   b = 78

  elseif message == "90" then
   b = 90

  elseif message == "109" then
   b = 109

  elseif message == "114" then
   b = 114

  else
   b = b
  end
end
end
function movement()   --Coressponds to four transmitters which are conected to 2 frame motors, 1 block breaker, and 1 deployer
while true do
  while a < b do
   redstone.setBundledOutput("right", colors.green)  --Places frame
   sleep(.2)
   redstone.setBundledOutput("right", colors.white)  --Moves elevator up
   a=a+1
   sleep(1)
  end
  while a > b do
   redstone.setBundledOutput("right", colors.red)   --Breakes frame
   sleep(.2)
   redstone.setBundledOutput("right", colors.black)  --Moves elevator down
   a=a-1
   sleep(1)
  end
  while a == b do
   redstone.setBundledOutput("right", 0)
   id, message = rednet.receive()
   sleep(1)
  end
end
end
function buttons()  --Buttons on each floor that are hooked up to transmitters to 'call' the elevator to that level
while true do
  if rs.getBundledInput("back", colors.pink) == true then
   b = 0

  elseif rs.getBundledInput("back", colors.black) == true then
   b = 18

  elseif rs.getBundledInput("back", colors.white) == true then
   b = 30

  elseif rs.getBundledInput("back", colors.red) == true then
   b = 42

  elseif rs.getBundledInput("back", colors.green) == true then
   b = 54

  elseif rs.getBundledInput("back", colors.blue) == true then
   b = 66

  elseif rs.getBundledInput("back", colors.gray) == true then
   b = 78

  elseif rs.getBundledInput("back", colors.yellow) == true then
   b = 90

  elseif rs.getBundledInput("back", colors.orange) == true then
   b = 109

  elseif rs.getBundledInput("back", colors.brown) == true then
   b = 114
  
  else
   b = b
  end
end
end

function Doors()  --Sends redstone signal to transmitters that move pistons
while true do
  while a == 0 and a == b do
   redstone.setBundledOutput("left", colors.black)
   redstone.setBundledOutput("left", colors.white)
   redstone.setBundledOutput("left", colors.red)
   redstone.setBundledOutput("left", colors.green)
   redstone.setBundledOutput("left", colors.blue)
   redstone.setBundledOutput("left", colors.gray)
   redstone.setBundledOutput("left", colors.yellow)
   redstone.setBundledOutput("left", colors.orange)
   redstone.setBundledOutput("left", colors.brown)
  end
  while a == 18 and a == b do
   redstone.setBundledOutput("left", colors.white)
   redstone.setBundledOutput("left", colors.red)
   redstone.setBundledOutput("left", colors.green)
   redstone.setBundledOutput("left", colors.blue)
   redstone.setBundledOutput("left", colors.gray)
   redstone.setBundledOutput("left", colors.yellow)
   redstone.setBundledOutput("left", colors.orange)
   redstone.setBundledOutput("left", colors.brown)
  end
  while a == 30 and a == b do
   redstone.setBundledOutput("left", colors.black)
   redstone.setBundledOutput("left", colors.red)
   redstone.setBundledOutput("left", colors.green)
   redstone.setBundledOutput("left", colors.blue)
   redstone.setBundledOutput("left", colors.gray)
   redstone.setBundledOutput("left", colors.yellow)
   redstone.setBundledOutput("left", colors.orange)
   redstone.setBundledOutput("left", colors.brown)
  end
  while a == 42 and a == b do
   redstone.setBundledOutput("left", colors.black)
   redstone.setBundledOutput("left", colors.white)
   redstone.setBundledOutput("left", colors.green)
   redstone.setBundledOutput("left", colors.blue)
   redstone.setBundledOutput("left", colors.gray)
   redstone.setBundledOutput("left", colors.yellow)
   redstone.setBundledOutput("left", colors.orange)
   redstone.setBundledOutput("left", colors.brown)
  end
  while a == 54 and a == b do
   redstone.setBundledOutput("left", colors.black)
   redstone.setBundledOutput("left", colors.white)
   redstone.setBundledOutput("left", colors.red)
   redstone.setBundledOutput("left", colors.blue)
   redstone.setBundledOutput("left", colors.gray)
   redstone.setBundledOutput("left", colors.yellow)
   redstone.setBundledOutput("left", colors.orange)
   redstone.setBundledOutput("left", colors.brown)
  end
  while a == 66 and a == b do
   redstone.setBundledOutput("left", colors.black)
   redstone.setBundledOutput("left", colors.white)
   redstone.setBundledOutput("left", colors.red)
   redstone.setBundledOutput("left", colors.green)
   redstone.setBundledOutput("left", colors.gray)
   redstone.setBundledOutput("left", colors.yellow)
   redstone.setBundledOutput("left", colors.orange)
   redstone.setBundledOutput("left", colors.brown)
  end
  while a == 78 and a == b do
   redstone.setBundledOutput("left", colors.black)
   redstone.setBundledOutput("left", colors.white)
   redstone.setBundledOutput("left", colors.red)
   redstone.setBundledOutput("left", colors.green)
   redstone.setBundledOutput("left", colors.blue)
   redstone.setBundledOutput("left", colors.yellow)
   redstone.setBundledOutput("left", colors.orange)
   redstone.setBundledOutput("left", colors.brown)
  end
  while a == 90 and a == b do
   redstone.setBundledOutput("left", colors.black)
   redstone.setBundledOutput("left", colors.white)
   redstone.setBundledOutput("left", colors.red)
   redstone.setBundledOutput("left", colors.green)
   redstone.setBundledOutput("left", colors.blue)
   redstone.setBundledOutput("left", colors.gray)
   redstone.setBundledOutput("left", colors.orange)
   redstone.setBundledOutput("left", colors.brown)
  end
  while a == 109 and a == b do
   redstone.setBundledOutput("left", colors.black)
   redstone.setBundledOutput("left", colors.white)
   redstone.setBundledOutput("left", colors.red)
   redstone.setBundledOutput("left", colors.green)
   redstone.setBundledOutput("left", colors.blue)
   redstone.setBundledOutput("left", colors.gray)
   redstone.setBundledOutput("left", colors.yellow)
   redstone.setBundledOutput("left", colors.brown)
  end
  while a == 114 and a == b do
   redstone.setBundledOutput("left", colors.black)
   redstone.setBundledOutput("left", colors.white)
   redstone.setBundledOutput("left", colors.red)
   redstone.setBundledOutput("left", colors.green)
   redstone.setBundledOutput("left", colors.blue)
   redstone.setBundledOutput("left", colors.gray)
   redstone.setBundledOutput("left", colors.yellow)
   redstone.setBundledOutput("left", colors.orange)
  end
end
end
function Debug()  --Displays the 'a' and 'b' values for debugging purposes only
while true do
  term.clear()
  term.setCursorPos(1,1)
  print("Curently at "..a)
  print("Moving to " ..B)/>/>
  sleep(.5)
end
end
Floors()
movement()
buttons()
Doors()
Debug()

I would like to put in a screenshot of the setup but I don't know to insert an image from my computer. also in the Debug function it should be a regular b but the forums change it.
Lyqyd #2
Posted 18 March 2013 - 06:20 AM
Split into new topic.
JokerRH #3
Posted 18 March 2013 - 09:25 AM
Your program has more problematic statements than lines, obvious that that won't run :P/>

I modified it a littlebit. Haven't tested it because I don't have your setup, but it should work.
(Though I didn't understand your Door function…)


rednet.open("top")
local currLayer = 1
local layer = 1

--Table of layers and buttoncolors
layers = {
  0 = colors.pink,
  18 = colors.black,
  30 = colors.white,
  42 = colors.red,
  54 = colors.green,
  66 = colors.blue,
  78 = colors.gray,
  90 = colors.yellow,
  109 = colors.orange,
  114 = colors.brown
}

--Scans if the layer is valid
function valid(param)
  for layer, col in pairs(layers) do
	if layer == param then
	  return true
	end
  end

  return false
end

--Returns the layer of a bundled input
function get(color)
  for layer, col in pairs(layers) do
	if col == color then
	  return layer
	end
  end
end

--Waits for rednet input
function rednetInput()
  while true do
	local message = rednet.receive()

	if valid(message) then
	  layer = message
	  return
	end
  end
end

--waits for button input
function buttonInput()
  while true do
	os.pullEvent("redstone")

	local ilayer = get(rs.getBundledInput("back"))
	if ilayer then
	  layer = ilayer
	  return
	end
  end
end

--Waits for input and moves up or down
function process()
  while true do
	parallel.waitForAny(rednetInput, buttonInput)

	if layer > currLayer then
	  move(layer - currLayer, colors.green, colors.white)
	elseif layer < currLayer then
	  move(currLayer - layer, colors.red, colors.black)
	end

	currLayer = layer
  end
end

--moves in the given direction
function move(dist, p1, p2)
  for i = 1, dist do
	redstone.setBundledOutput("right", p1)
	sleep(.2)
	redstone.setBundledOutput("right", p2)
	sleep(1)
  end
end

--Start the program
process()
ChaosSorcerer #4
Posted 18 March 2013 - 10:12 AM
Thank you so much for taking a look at this! However, when I plug that code into a computer it gives an error message saying that the { at line 6 was expected to be closed at line 7. I'm only familiar with while, if, and else statements so I don't know how to fix that. Also, the Doors function is supposed to keep pistons extended at all floors except the current floor and the basement, or layer 0 (Which doesn't have any pistons for maintenance on the physical movement) and closes them while it is moving. The pistons are connected to receivers, which are connected to transmitters, connected to a bundled input, connected to the central computer's left side.
JokerRH #5
Posted 18 March 2013 - 11:29 AM
I'm only on my ipod right now, so I can't tell for sure, but the solution might be
[0] = colors.whatever,
[2] = colors…
(That means I think I forgot those brackets:D)
ChaosSorcerer #6
Posted 18 March 2013 - 01:12 PM
Yeah, that fixed it! It's on my friend's server so I'll give him the code tomorrow to see if it works.
ChaosSorcerer #7
Posted 19 March 2013 - 06:16 AM
Figured out how to put on an image