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

Broken Elevator, Computer Won't Properly Read From Floppy?

Started by sfoxdale, 13 February 2013 - 12:48 PM
sfoxdale #1
Posted 13 February 2013 - 01:48 PM
I need help with this function I'm writing to control a system of computers that will operate my elevator I've made in conjunction with Redpower. The system is supposed to work with a computer that is actually on the elevator, the part of the code that starts with "x == "onboard"", that you enter what floor you want to go to, and then that computer is supposed to tell the "control" computer what floor it wants to go to. Then the control computer is supposed to check the disk drive, whose "floors" file has been edited by the "catlog" computer to add floors to it, to see what the Z coordinate is of the destination floor. The control computer then is supposed to send this Z coordinate number to the "onboard" computer which is supposed to check to see whether or not it is above or below the destination floor, and then, depending on where it is relative to the destination floor, the "onboard" computer is supposed to broadcast an "up" or "down" command to a Frame Motor Controller computer, "FMcontroller", which'll send a redstone pulse to the appropriate frame motors to send the elevator either up or down. The function's code looks like this:


function elevator(x)
if x == "catlog" then
while disk.isPresent("right") == false do
  term.clear()
  term.setCursorPos(1,1)
  print ("Please Insert Floors Disk To Catalogue")
  sleep(2)
end
  while true do
   term.clear()
   term.setCursorPos(1,1)
   print("Add a floor?")
   floornum = read()
   print("Floor depth? (Z Coordinate)")
   floordep = read()
   floornum = "f"..floornum
   floordep = "d"..floordep
   floorloc = floornum..floordep
    if fs.isDir("disk\floors") == nil then
	 fs.makeDir("disk\floors")
    end
   elecon = fs.open("disk\floors", "a")
   elecon.write(floorloc)
   sleep(2)
  end
end
if x == "control" then
thor.wireless()
  while true do
   goto = 0
   while goto == 0 do
    _,goto,_ = rednet.receive()
    if string.sub(goto, 1, 6) ~= "destin" then
	 goto = 0
    end
   end
   goto = string.sub(goto, 7)
   goto = "f"..goto
   if fs.isDir("disk\floors") == false then
    rednet.broadcast("ERROR: SET UP FLOOR CATLOG OR INSERT FLOORS DISK")
   else
    elecon = fs.open("disk\floors", "r")
    floorcat = elecon.read()
    gotoS,gotoE = string.find(floorcat, goto)
    goto = string.sub(floorcat, gotoE+1, gotoE+2)
    rednet.broadcast(goto)
   end
  end
end
if x == "onboard" then
thor.wireless()
  while true do
   term.clear()
   term.setCursorPos(1,1)
   print("Destination? (Num of Floor You Would Like To Go To)")
   destin = read()
   destin = "destin"..destin
   rednet.broadcast("destin")
   destin = 0
   it = 0
   while destin == 0 and it <= 25 do
    _,destin,_ = rednet.receive(7)
    if string.sub(destin, 1, 1) ~= "d" then
	 destin = 0
    end
    it = it+1
   end
   if it < 25 then
    destin = string.sub(destin, 2)
    _,_,currnt = gps.locate()
    if currnt > destin then
	 while currnt > destin do
	  rednet.broadcast("down")
	  sleep(2)
	  _,_,currnt = gps.locate()
	 end
    elseif currnt < destin then
	 while currnt < destin do
	  rednet.broadcast("up")
	  sleep(2)
	  _,_,currnt = gps.locate()
	 end
    elseif currnt == destin then
	 print("Already at floor")
    end
    print("YOU HAVE ARRIVED!")
    sleep(4)
   else
    print("There is an ERROR in the system!")
   end
  end
end
if x == "FMcontroller" then
thor.wireless()
  while true do
   cmd = 0
   while cmd == 0 do
    _,cmd,_ = rednet.receive()
    if cmd ~= "up" and cmd ~= "down" then
	 cmd = 0
    end
   end
   if cmd == "up" then
    redstone.setOutput("right", true)
    redstone.setOutput("right", false)
   end
   if cmd == "down" then
    redstone.setOutput("left", true)
    redstone.setOutput("left", false)
   end
  end
end
end

My problem occurs when I enter what floor I want to go to into the "onboard" computer. I'll put in a number, and then the "control" computer claims that the floppy disc directory doesn't exist and won't give the Z coordinate. What am I doing wrong? I've stared and tried to fix this function for hours, and my eyes are starting to hurt and I'm getting a headache… I just don't know what else to do.
Lyqyd #2
Posted 13 February 2013 - 02:00 PM
Split into new topic.
ChunLing #3
Posted 13 February 2013 - 09:27 PM
While nil and false both evaluate as false when used in a conditional, nil ~= false in Lua.

For this reason (among others) it is better to use conditionals like:
if not fs.isDir("disk\floors") then  ... end
rather than fs.isDir("disk\floors") == nil expressions.

Of course, you should check this computer's file system by terminating the program and navigating about in the shell a bit to see if disk\floors does or doesn't exist as a directory. If it does exist, then your problem could be something else.
Edited on 13 February 2013 - 08:32 PM
sfoxdale #4
Posted 15 February 2013 - 10:00 AM
I think I'm just going to get rid of the catalog part, and merge the control and onboard parts together to eliminate whatever problem there may be or possibly be altogether. I won't be able to add floors from in-game, but it's not like I'm going to have to add a floor every other day :P/>