se7enek

Untitled

Sep 14th, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 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 = 'left'
  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.  
  65. -- Get first keypress and show pinpad
  66. local event, monside, xpos, ypos = os.pullEvent()
  67. if event == 'monitor_touch' then
  68. print ('* Starting keypad entry')
  69. printKeyPad()
  70. end
  71.  
  72. while (tonumber(string.len(pwentered)) < tonumber(string.len(password))) do
  73. local pressed = ''
  74. local event, monside, xpos, ypos = os.pullEvent()
  75.  
  76. if event == 'monitor_touch' then
  77. local pressed = tostring(getKeyFromPad (xpos, ypos))
  78. if pressed ~= "" then
  79. pwentered = pwentered .. pressed
  80. print ("* Pressed: " .. pressed)
  81. end
  82. end
  83. end
  84.  
  85. if (pwentered == password) then
  86. print ('* Login SUCCESS')
  87. loginOK ()
  88. openDoor ()
  89. sleep (dooropendelay)
  90. closeDoor ()
  91. else
  92. print ('* Login FAIL')
  93. loginFail ()
  94. sleep (errordelay)
  95. end
  96. end
Add Comment
Please, Sign In to add comment