Advertisement
Forecaster

OpenSecurity Keypad Program

Apr 6th, 2016
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local c = require("component")
  2. local keypad = c.os_keypad
  3. local computer = require("computer")
  4. local event = require("event")
  5.  
  6. local code = "12345678"                                        -- Your code (Max 8 digits to fit on keypad display)
  7. local user_code = ""                                           -- Variable that stores input
  8.  
  9. local run = true
  10. while run do
  11.   local e, address, _, key = event.pull(1000)
  12.   if (e and e == "interrupted") then                           -- Kill program when ctrl + c is pressed
  13.     run = false
  14.   elseif (e and e == "keypad") then
  15.     if (key and key == "*") then                               -- Uses star button as reset key
  16.       user_code = ""
  17.       keypad.setDisplay("")
  18.     elseif (key) then                                          -- On key other than reset key add it to user_code
  19.       user_code = user_code .. key
  20.       keypad.setDisplay(user_code)                             -- Update display to updated input string
  21.     end
  22.     if (string.len(code) == string.len(user_code)) then        -- If code and user_code are the same length:
  23.       if (code == user_code) then                              -- Test if they match
  24.         computer.beep(2000)
  25.         print("Code correct!")
  26.         keypad.setDisplay("CORRECT")
  27.         user_code = ""
  28.       else
  29.         computer.beep(100)
  30.         print("Wrong code!")
  31.         keypad.setDisplay("WRONG")
  32.         user_code = ""
  33.       end
  34.     end
  35.   elseif (e and e == "magData") then                           -- If mag card is detected, check if stored string matches code
  36.     if (key == code) then
  37.       keypad.setDisplay("CORRECT")
  38.       computer.beep(2000)
  39.     else
  40.       keypad.setDisplay("DENIED")
  41.       computer.beep(100)
  42.     end
  43.   end
  44. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement