Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
bingus

Security Door Code

Question

I just started using OpenComputers+Security and don't really know what I'm doing. I'm trying to make a security door that opens when activated by a keypad. It's using a Door Controller, and a Keypad. The only code I have is stuff I stole from the wiki. The code is working for the keypad, but I don't know how to make the keypad activate the Door Controller. Here is the keypad code I have:

keypad = require("component").os_keypad
event = require("event")

local pin = "1234"
local keypadInput = ""

-- set this to true if you want to run the script as daemon
local runScriptInBackground = false

function updateDisplay()
    local displayString = ""
    for i=1,#keypadInput do
        displayString = displayString .. "*"
    end

    keypad.setDisplay(displayString, 7)
end

function checkPin()
    if keypadInput == pin then
        keypad.setDisplay("granted", 2)
    else
        keypad.setDisplay("denied", 4)
    end    
    keypadInput = ""
    os.sleep(1)
end

function keypadEvent(eventName, address, button, button_label)
    print("button pressed: " .. button_label)
  
    if button_label == "*" then
        -- remove last character from input cache
        keypadInput = string.sub(keypadInput, 1, -2)
    elseif button_label == "#" then
        -- check the pin when the user confirmed the input
        checkPin()
    else
        -- add key to input cache if none of the above action apply
        keypadInput = keypadInput .. button_label
    end

    updateDisplay()  
end

-- listen to keypad events
event.listen("keypad", keypadEvent)

-- clear keypad display
keypad.setDisplay("")


if not runScriptInBackground then
    -- sleep until the user interrupts the program with CTRL + C
    local stopMe = false
    event.listen("interrupted", function() stopMe = true; end)
    while not stopMe do os.sleep(0.1) end

    -- ignore keypad events on exit
    event.ignore("keypad", keypadEvent)

    -- show that the keypad is inactive
    keypad.setDisplay("inactive", 6)
end

Please help!

Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.