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

Programming problem that escapes me.

Started by Tassyr, 12 November 2012 - 12:54 PM
Tassyr #1
Posted 12 November 2012 - 01:54 PM
I'm trying to write a pair of programs to work in tandem that are completly confusing me. What I'm attempting to make is a 'vault' entrance;

Here's what I"m trying to -get- it to do:

computer 1 (away from the vault)
menu option 1: "Unlock"
(Sends redstone signal to computer 2)
menu option 2: "Lock"
(Turns off Redstone Signal to computer 2)


computer 2:
menu option 1: Unlock
IF the computer is recieving the above redstone signal, opens the door
If not, does nothing
menu option 2: Lock
shuts the door.

Also, if computer one is set to lock, I'd like the door to close reguardles of if the 'unlock' option is active on computer2.


This is a lot more complex than anything i've attempted before; is the second computer's setup even POSSIBLE? I'm only using single redstone inputs, so. Nothing bundled.
Espen #2
Posted 12 November 2012 - 03:13 PM
I hope I understood you correctly.

Lock Control:
Spoiler
local rsOutputSide = "back"
local unlocked = false

local function write( text, color )
    if term.isColor() and color then
        term.setTextColor( color )
        term.write( text )
        term.setTextColor( colors.white )
    else
        term.write( text )
    end
end

while true do
    term.clear()
    term.setCursorPos(1, 2)
    print( " Menu:n" )
    print( " 1 - Unlock" )
    print( " 2 - Lock" )
    print( " ----------" )
    print( " Q - Quitn" )
    write( " State: ") if unlocked then write( "unlocked", colors.lime ) else write( "locked", colors.red ) end
    local _, char = os.pullEvent( "char" )

    if char == "1" then
        rs.setOutput( rsOutputSide, true )    -- Unlock door
        unlocked = true
    elseif char == "2" then
        rs.setOutput( rsOutputSide, false )   -- Lock door
        unlocked = false
    elseif string.lower( char ) == "q" then
        term.clear()
        term.setCursorPos(1,1)
        return
    end
end
Opener/Closer:
Spoiler
local rsInputSide = "back"
local rsDoorSide = "left"
local unlocked = rs.getInput( rsInputSide )

local function write( text, color )
    if term.isColor() and color then
        term.setTextColor( color )
        term.write( text )
        term.setTextColor( colors.white )
    else
        term.write( text )
    end
end

while true do
    term.clear()
    term.setCursorPos(1, 2)
    print( " Menu:n" )
    print( " 1 - Open" )
    print( " 2 - Close" )
    print( " ----------" )
    print( " Q - Quitn" )
    write( " State: " )
    if unlocked then write( "unlocked", colors.lime ) else write( "locked", colors.red ) end
    write( " and " )
    if rs.getOutput( rsDoorSide ) then write( "opened", colors.lime ) else write( "closed", colors.red ) end
    local event, char = os.pullEvent( )

    if event == "redstone" then
        if rs.getInput( rsInputSide ) then
            unlocked = true
        else
            rs.setOutput( rsDoorSide, false )   -- Close door
            unlocked = false
        end
    end

    if event == "char" then
        if char == "1" then
            if rs.getInput( rsInputSide ) then
                rs.setOutput( rsDoorSide, true )    -- Open door
            end
        elseif char == "2" then
            rs.setOutput( rsDoorSide, false )   -- Close door
        elseif string.lower( char ) == "q" then
            term.clear()
            term.setCursorPos(1,1)
            return
        end
    end
end

Remember to set the variables at the top to the side where your redstone in- and outputs are.
Edited on 12 November 2012 - 02:14 PM
Tassyr #3
Posted 12 November 2012 - 09:06 PM
Sadly I can't test this until tomorrow- but I'll give it a shot, and thanks in advance for trying! :P/>/>