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

Lift program using fram motors

Started by wolfy, 05 November 2012 - 10:02 PM
wolfy #1
Posted 05 November 2012 - 11:02 PM
I've been working on a spacy lift connecting the 5 floors of my base. The lift is a platform made of Frames on a long still also made out of frames. the motors are at the bottom floor, one is for up, on is for downward movement. on every floor there is a computer for controlling the lifts movements. The motors need a signal so they are both connoected to different colored insulated wires; the up motor is green, the down motor is red. The computers send their output signal from their BACK side.

Now, to enable every computer to use the same program, i made the following setup: on every floor there is a permanently ON redstone torch, linked to a repeater, which outputs into air, next to it is a redstone wire connecting to a colored insulated wire.

Now on the bottow of the platform i attached a block (stone, but it doesn't really matter) so when the lift is on a specific floor, the stone block is turned on by the repeater, the redstone wire gets turned on which then turns on the specicly colored insulated wire. That's how the computers know which floor the lift is on right now. The Input side of the computers for recieving the floors colored signal is RIGHT. and a bundled cable goes up the whole shaft which means that all computers would recieve yellow if the lift was on floor 2!

floor 1 i orange, 2 is yellow, 3 is lime, 4 is cyan, 5 is blue

so i've been making this program but i'm somewhat stuck at getting it to work really. Here it is

local side = "right"
local one = 1
local two = 2
local three = 3
local four = 4
local five = 5
local floor = redstone.getBundledInput(side)
local goal = ""
local c = ""
local a = ""
local input = ""
local output = "back"
while true do
term.clear()
term.setCursorPos(1,1)
write("Select a floor from 1 to 5: ")
local input = tonumber (read())
if input >= 1 and input <= 5 then
if floor == 2 then
local a = 1
elseif floor == 16 then
local a = 2
elseif floor == 32 then
local a = 3
elseif floor == 512 then
local a = 4
elseif floor == 2048 then
local a = 5
else
print("Error; uknown floor or damaged sensor")
if input == one then
local goal = 2
elseif input == two then
local goal = 16
elseif input == three then
local goal = 32
elseif input == four then
local goal = 512
elseif input == five then
local goal = 2048
else
print("Floor doesn't exist!")
if a > input then
while c ~= goal do
local c = getBundledInput(side)
redstone.setBundledOutput(output, colors.red)
sleep(0.1)
redstone.setBundledOutput(output, 0)
sleep(1)
if c == goal then
os.shutdown()
end
end
end
end
if input > a then
while c ~= goal do
local c = getBundledInput(side)
redstone.setBundledOutput(output, colors.green)
sleep(0.1)
redstone.setBundledOutput(output, 0)
sleep(1)
if c == goal then
os.shutdown()
end
end
end
end
if input == a then
print("You are already on that floor!")
sleep(1)
else
print("Floor doesn't exist")
sleep(1)
end
end
end

What it's supposed to do is: ask for an input, which has to be 1,2,3,4 or 5, if it isn't one of those, it should print out "flloor doesn't exist". Next it reads what color the input is so it knows which floor it is, and depending on the color, assigns a value to "a". Then it compares "a" to "input". if "a" > "input", it means that the lift will go down so the loop is set to output in the color red, if "a" < "input" it has to go up and the color is green. the variable "goal" depends on "input" and every loop, the program should check if the correct color signal is being detected.

I've never actually programmed with lua before this, so this caused me quite an amount of grief and frustration, simply because i'm not used to it.

the problem i'm stuck at right now is that no matter which number i type in for "input", the elseif statements don't assign a value to "goal" and i always get "floor doesn't exist".

any help would be very much appreciated, thanks in advance
Cruor #2
Posted 05 November 2012 - 11:47 PM
Use code tags, kthx.
wolfy #3
Posted 06 November 2012 - 12:32 AM
sorry, my bad!


local side = "right"
local one = 1
local two = 2
local three = 3
local four = 4
local five = 5
local floor = redstone.getBundledInput(side)
local goal = ""
local c = ""
local a = ""
local input = ""
local output = "back"
while true do
term.clear()
term.setCursorPos(1,1)
write("Select a floor from 1 to 5: ")
local input = tonumber (read())
if input >= 1 and input <= 5 then
if floor == 2 then
local a = 1
elseif floor == 16 then
local a = 2
elseif floor == 32 then
local a = 3
elseif floor == 512 then
local a = 4
elseif floor == 2048 then
local a = 5
else
print("Error; uknown floor or damaged sensor")
if input == one then
local goal = 2
elseif input == two then
local goal = 16
elseif input == three then
local goal = 32
elseif input == four then
local goal = 512
elseif input == five then
local goal = 2048
else
print("Floor doesn't exist!")
if a > input then
while c ~= goal do
local c = getBundledInput(side)
redstone.setBundledOutput(output, colors.red)
sleep(0.1)
redstone.setBundledOutput(output, 0)
sleep(1)
if c == goal then
os.shutdown()
end
end
end
end
if input > a then
while c ~= goal do
local c = getBundledInput(side)
redstone.setBundledOutput(output, colors.green)
sleep(0.1)
redstone.setBundledOutput(output, 0)
sleep(1)
if c == goal then
os.shutdown()
end
end
end
end
if input == a then
print("You are already on that floor!")
sleep(1)
else
print("Floor doesn't exist")
sleep(1)
end
end
end
diegodan1893 #4
Posted 07 November 2012 - 10:52 AM
This doesn't answer to your question but there is a easier way to code you program.
First make a function that makes the lift go 1 floor up and another that makes the lift go 1 floor down. Then when the user tell the computer where to go, you must do:

floors = inputFloor - currentFloor
For example, if you are in the 1st floor and you press the 4th button,
floors = 4 - 1 = 3
(you must go 3 floors up) and if you are in the 4th floor and you prest the 1st button,
floors = 1 - 4 = -3
(you must go 3 floors down).

So with this, if floors < 0 (negative) make a bucle that repeats the function down "floors" times. If floors > 0 (positive) repeat the up function and if it is 0 then print "you're in that floor". This even works with negative floors and it's easy to expand it.

I don't have time to code it now, but tell me if you need help with something
wolfy #5
Posted 07 November 2012 - 04:44 PM
I managed to get it to work now! adding functions finally made it work. this is what i've done:

local inputside = "right"

local outputside = "back"
local goalfloor = 0
local a = 0
local inputfloor = ""


local function goal()
   write("Select a floor from 1 to 5: ")

   local inputfloor = tonumber (read())
    if inputfloor == 1 then


	 goalfloor = goalfloor + 2
    return inputfloor, goalfloor
    elseif inputfloor == 2 then


	 goalfloor = goalfloor + 16

    return inputfloor, goalfloor
    elseif inputfloor == 3 then


	 goalfloor = goalfloor + 32
    return inputfloor, goalfloor
    elseif inputfloor == 4 then


	 goalfloor = goalfloor + 512
    return inputfloor, goalfloor
    elseif inputfloor == 5 then


	 goalfloor = goalfloor + 2048

    return inputfloor, goalfloor
    else


	 print("Floor doesn't exist!")
    sleep(1.5)
    os.reboot()
    end
end

local function colortofloor()
    local floor = redstone.getBundledInput(inputside)
    if floor == 2 then

    local a = a + 1

    return a
    elseif floor == 16 then

    local a = a + 2
    return a
    elseif floor == 32 then

    local a = a + 3
    return a
    elseif floor == 512 then

    local a = a + 4
    return a
    elseif floor == 2048 then

    local a = a + 5
    return a
    else

    print("Error; uknown floor or damaged sensor, you will be moved to the closest floor")
	  while true do
  local floor = redstone.getBundledInput(inputside)
  if floor == 0 then

   redstone.setBundledOutput(outputside, 0)
   redstone.setBundledOutput(outputside, colors.red)

   sleep(0.1)

   redstone.setBundledOutput(outputside, 0)

   sleep(0.85)
  else
 
 os.shutdown()

  end

  end

  end

end

local function up()
  while true do
  local floor = redstone.getBundledInput(inputside)
  if floor ~= goalfloor then

   redstone.setBundledOutput(outputside, 0)
   redstone.setBundledOutput(outputside, colors.green)

   sleep(0.1)

   redstone.setBundledOutput(outputside, 0)

   sleep(0.8)
  else
 
 os.shutdown()

  end

end
end

local function down()
  while true do
  local floor = redstone.getBundledInput(inputside)
  if floor ~= goalfloor then

   redstone.setBundledOutput(outputside, 0)
   redstone.setBundledOutput(outputside, colors.red)

   sleep(0.1)

   redstone.setBundledOutput(outputside, 0)

   sleep(0.85)
  else
 
 os.shutdown()

  end

end
end

while true do


  term.clear()

  term.setCursorPos(1,1)

local inputfloor, goalfloor = goal()

local a = colortofloor()

if inputfloor > a then
  up()
elseif inputfloor < a then
  down()
elseif inputfloor == a then
  write("You are already on that floor.")
  sleep(1.5)
  os.shutdown()
end

end
wolfy #6
Posted 07 November 2012 - 05:04 PM
also diegodan1893 the function you mentioned wouldn't work since the floors have a different height each, the 1st is 11 high, the second 8, the rest 5 each.
crackroach #7
Posted 09 November 2012 - 03:22 PM
well, i'm not a pro but you could make a function for each floor that knows how much block it have to go up or down…