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

Clean elevator code

Started by tysciman7, 08 June 2013 - 01:26 PM
tysciman7 #1
Posted 08 June 2013 - 03:26 PM
I really need help cleaning up and organizing this code for an elevator. I need the elevator have the ablity to be called down to different levels thats what the cf2 and cf3 are for the calling of the elevator.


rednet.close("top")

rednet.open("top")

function sleep()
  os.sleep(.35)
end

function b()
  rs.setOutput("right", true)
  sleep()
  rs.setOutput("right", false)
  sleep()
end

function o()
  rs.setOutput("left", true)
  sleep()
  rs.setOutput("left", false)
  sleep()
end

function m()
  rs.setOutput("back", true)
  sleep()
  rs.setOutput("back", false)
  sleep()
end

function w()
  rs.setOutput("bottom", true)
  os.sleep(.5)
  rs.setOutput("bottom", false)
  os.sleep(.5)
end

function down()
b()
m()
end

function up()
o()
w()
end

function onetwo()
  for i = 1, 12 do
    up()
  end
end

function twoone()
  for i = 1, 12 do
    down()
  end
end

function onethree()
end

function twothree()
end

function threeone()
end

function threetwo()
end


id,msg,dis = rednet.receive()

if id == 40 and msg == "onetwo" then
  onetwo()
elseif id == 40 and msg == "onethree" then
  onethree()
elseif id == 41 and msg == "twoone" then
  twoone()
elseif id == 41 and msg == "twothree" then
  twothree()
elseif id == and msg == "threetwo" then
  threetwo()
elseif id == and msg == "threeone" then
  threeone()
elseif id == 40 and msg == "cf2" then
  twoone()
elseif id == 40 and msg == "cf3" then
  threeone()
end
LBPHacker #2
Posted 08 June 2013 - 03:38 PM
Sooo… You have an elevator with three stations, and computers which call it on each floor? 12 block between floors? Elevator goes down with right + bottom and up with left + back redstone? If so, I can help.
tysciman7 #3
Posted 08 June 2013 - 04:14 PM
Yes that is exactly it
LBPHacker #4
Posted 08 June 2013 - 05:49 PM
Which computer is on which floor? I have in impression that 40 is on the first, 41 is on the second and <something you didn't write down> is on the third.

And in the message processing if block, what are "cf2" and "cf3" supposed to mean?
EDIT: Okay, got it (reread the OP) But then wouldn't you need a "cf1" message as well?
Anyways, working on it at the moment. Back in a few minutes.

EDIT #2: Oh! Question: What makes the caller computers call the elevator? A button?
Well, suppose it's a button on the right.
LBPHacker #5
Posted 08 June 2013 - 06:11 PM
Elevator code: (pastebin: XtbHiTFV)
Spoiler
-- * modem side here
rednet.open("top")

-- * current floor - might not be accurate
-- * since you have three computers (the ones that call the elevator),
--   you could use some GPS code to determine the current floor
local current = 1

local floorIDs = {
    -- * ["sender IDs in strings"] = the floors they are on
    -- * you can expand this table further
    ["40"] = 1,
    ["41"] = 2,
    ["42"] = 3,
}

local gotoFloor = function(target)
    repeat
        if current > target then -- target is lower ...
            for i = 1, 12 do -- ... so go down
                rs.setOutput("right", true)
                sleep(0.5)
                rs.setOutput("right", false)
                sleep(0.5)
                rs.setOutput("back", true)
                sleep(0.5)
                rs.setOutput("back", false)
                sleep(0.5)
            end
            current = current - 1
        elseif current < target then -- target is higher ...
            for i = 1, 12 do -- ... so go up
                rs.setOutput("left", true)
                sleep(0.5)
                rs.setOutput("left", false)
                sleep(0.5)
                rs.setOutput("buttom", true)
                sleep(0.5)
                rs.setOutput("buttom", false)
                sleep(0.5)
            end
            current = current + 1
        end
    until target == current -- we've reached the target
end

while true do
    local eventData = {os.pullEvent()}
    -- * now eventData[2] is the sender and eventData[3] is the message
    if eventData[1] == "rednet_message" and eventData[3] == "elevator_call" then
        -- * every computer will send only "elevator_call"
        local target = floorIDs[tostring(eventData[2])]
        if target then
            -- * call gotoFloor only if target is not nil
            -- * that would mean that a computer which is not in the floorIDs table
            --   above sent the "elevator_call" message
            gotoFloor(target)
        end
    end
end

Caller code: (pastebin: yvcVJk79)
Spoiler
-- * modem side here
rednet.open("top")

while true do
    os.pullEvent("redstone")
    -- 39 might not be accurate - it's the ID of the elevator's computer
    if rs.getInput("right") then rednet.send(39, "elevator_call") end
end

WARNING: Not tested. Should work though.
tysciman7 #6
Posted 08 June 2013 - 06:14 PM
imma trying to make touch screens to go up to level 1 or 2 and if the elevator isnt there then you call it
LBPHacker #7
Posted 08 June 2013 - 06:19 PM
How do you plan using the touchscreens?
tysciman7 #8
Posted 08 June 2013 - 06:25 PM
u press level two or three and it takes you there by sending rednet
LBPHacker #9
Posted 08 June 2013 - 06:31 PM
But… Hold on - I thought the controller computer is in the elevator… (would make no sense now that I think about it). Well, make a button layout (draw it in paint or I don't know) and then come back. But you really should start working on the program yourself already. I can't stress enough that Ask a Pro is mostly about helping people stuck with their code, not being coding monkeys and writing The Matrix for somebody who said "please".
tysciman7 #10
Posted 08 June 2013 - 07:01 PM
i know i cant figure out the way i did it its mostly the call im having trouble with

and lolz go on notepad and type


@echo off
color A
:top
echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%
goto top

and save it as a matrix.bat