Advertisement
Guest User

Keypad Enter pin start

a guest
Nov 26th, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.53 KB | None | 0 0
  1. -- Variables -----------------------------------------------
  2. local password = "123"
  3. local keypadtable = {{3,2,"1"}, {4,2,"2"}, {5,2,"3"}, {3,3,"4"}, {4,3,"5"}, {5,3,"6"}, {3,4,"7"}, {4,4,"8"}, {5,4,"9"}}
  4. local dooropendelay = 4
  5. local errordelay = 3
  6. local monitorlocation = 'back'
  7. local redstonesignalside = 'right'
  8.  
  9. ------------------------------------------------------------
  10.  
  11. local monitor = peripheral.wrap(monitorlocation)
  12. local maxx, maxy = monitor.getSize()
  13.  
  14. local function idleScreen ()
  15.   monitor.clear()
  16.   monitor.setCursorPos(2,2)
  17.   monitor.write("Enter")
  18.   monitor.setCursorPos(3,3)
  19.   monitor.write("PIN")
  20. end
  21.  
  22. local function loginOK ()
  23.   monitor.clear()
  24.   monitor.setCursorPos(1,3)
  25.   monitor.write("Welcome")
  26. end
  27.  
  28. local function loginFail ()
  29.   monitor.clear()
  30.   monitor.setCursorPos(2,3)
  31.   monitor.write("Error")
  32. end
  33.  
  34. local function printKeyPad ()
  35.   monitor.clear()
  36.   for i=1,#keypadtable do
  37.         monitor.setCursorPos(keypadtable[i][1],keypadtable[i][2])
  38.         monitor.write(keypadtable[i][3])
  39.   end
  40. end
  41.  
  42. local function getKeyFromPad (xclick, yclick)
  43.   for i=1,#keypadtable do
  44.         if ((keypadtable[i][1] == xclick) and (keypadtable[i][2] == yclick)) then
  45.           return (keypadtable[i][3])
  46.         end
  47.   end
  48.   return ('')
  49. end
  50.  
  51. local function openDoor ()
  52.   redstone.setOutput (redstonesignalside, true)
  53. end
  54.  
  55. local function closeDoor ()
  56.   redstone.setOutput (redstonesignalside, false)
  57. end
  58.  
  59. -- MAIN ------------------------------------------------------------
  60.  
  61. while true do
  62.   idleScreen()
  63.   local pwentered = ''
  64.   local interact = false
  65.  
  66.   -- Get first keypress and show pinpad
  67.   local event, monside, xpos, ypos = os.pullEvent()
  68.   if event == 'monitor_touch' then
  69.         interact = true
  70.         print ('* Starting keypad entry')
  71.         printKeyPad()
  72.   end
  73.  
  74.   while (tonumber(string.len(pwentered)) < tonumber(string.len(password))) and interact do
  75.         local pressed = ''
  76.         local event, monside, xpos, ypos = os.pullEvent()
  77.  
  78.         if (event == 'monitor_touch') and interact then
  79.          local pressed = tostring(getKeyFromPad (xpos, ypos))
  80.          if pressed ~= "" then
  81.           pwentered = pwentered .. pressed
  82.           print ("* Pressed: " .. pressed)
  83.          end
  84.         end
  85.   end
  86.  
  87.   if (pwentered == password) then
  88.         interact = false
  89.         print ('* Login SUCCESS')
  90.         loginOK ()
  91.         openDoor ()
  92.         sleep (dooropendelay)
  93.         closeDoor ()
  94.   else
  95.         print ('* Login FAIL')
  96.         loginFail ()
  97.         sleep (errordelay)
  98.   end
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement