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

I need a little help with my elevator

Started by apfelrest, 18 September 2012 - 03:49 PM
apfelrest #1
Posted 18 September 2012 - 05:49 PM
Hi,

im new to this Forum (this is my first post, as you can see), and i could need some help with my little elevator program.
So basically its just some frame motors from redpower2, and some other stuff that moves the elevator, and they are connected via bundled cable to the computer. when not using the program, but just simple redpower2 gates and stuff, it works fine.

so this is the code:


r = fs.open("level/level","r")
h = fs.open("level/level","w")
local level
local callPos
local liftMov
-- used to move elevator to the position where its called
function moveLift()
if callPos ~= level then
  liftMov = callPos-level
  if (liftMov >= 0) then
   liftMov = (liftMov*(-1))
   for j=1,liftMov,1 do
	liftDown()
   end
  else
   for j=1,liftMov,1 do
	liftUp()
   end
  end
end
end

-- move lift up (it has to go seven blocks up to reach the next floor) and save new position.
function liftUp()
for i=1,7,1 do
  rs.setBundledOutput("front",16384)
  sleep(0.5)
  rs.setBundledOutput("front",16)
  sleep(0.5)
  rs.setBundledOutput("front",0)
  sleep(0.5)
end
level=level+1
h.writeLine(level)
h.close()
end

-- moving down and save
function rightliftDown()
for i=1,7,1 do
  rs.setBundledOutput("front",2048)
  sleep(0.5)
  rs.setBundledOutput("front",1)
  sleep(0.5)
  rs.setBundledOutput("front",0)
  sleep(0.5)
end
level=level-1
h.writeLine(level)
h.close()
end

-- Loop - the last 4 bundled cable inputs are to define the call position.
while true do
local file = {}
local line = r.readLine()
repeat
table.insert(file,line)
line = r.readLine()
until line == nil
r.close()
local level = tonumber(fileData[1])
local event = os.pullEvent()
if event =="redstone" then
  if (rs.testBundledInput("back",1)==true)
  and (level>4) then
   liftUp()
  elseif (rs.testBundledInput("back",16)==true)
  and (level<1) then
   liftDown()
  else
   if (rs.testBundledInput("back",64)==true) then
	callPos = 1
   elseif (rs.testBundledInput("back",2048)==true) then
	callPos = 2
   elseif (rs.testBundledInput("back",8192)==true) then
	callPos = 3
   elseif (rs.testBundledInput("back",16384)==true) then
	callPos = 4
   end
  moveLift()
  end
end
end


i think there is something wrong with the saving of the level but i cant put my finger on whats really wrong.
i hope you guys can help me out.

thanks in advance
apfelrest
Cranium #2
Posted 18 September 2012 - 05:58 PM
What kind of error are you getting? It seems to me that you need to define level,callPos, and liftMov. Right now, you would get an unexpected symbol if I am not mistaken.