Advertisement
Guest User

Keypad showing computercraft

a guest
Nov 26th, 2018
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.   printKeyPad()
  63.   local pwentered = ''
  64.  
  65.   while (tonumber(string.len(pwentered)) < tonumber(string.len(password))) do
  66.         local pressed = ''
  67.         local event, monside, xpos, ypos = os.pullEvent()
  68.  
  69.         if event == 'monitor_touch' then
  70.          local pressed = tostring(getKeyFromPad (xpos, ypos))
  71.          if pressed ~= "" then
  72.           pwentered = pwentered .. pressed
  73.           print ("* Pressed: " .. pressed)
  74.          end
  75.         end
  76.   end
  77.  
  78.   if (pwentered == password) then
  79.         print ('* Login SUCCESS')
  80.         loginOK ()
  81.         openDoor ()
  82.         sleep (dooropendelay)
  83.         closeDoor ()
  84.   else
  85.         print ('* Login FAIL')
  86.         loginFail ()
  87.         sleep (errordelay)
  88.   end
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement