Advertisement
Guest User

keypad.lua

a guest
Apr 6th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.86 KB | None | 0 0
  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"
  7. local user_code = ""
  8.  
  9. local run = true
  10. while run do
  11.   local e, address, _, key = event.pull(1000)
  12.   if (e and e == "interrupted") then
  13.     run = false
  14.   elseif (e and e == "keypad") then
  15.     if (key and key == "*") then
  16.       user_code = ""
  17.       keypad.setDisplay("")
  18.     elseif (key) then
  19.       user_code = user_code .. key
  20.       keypad.setDisplay(user_code)
  21.     end
  22.     if (string.len(code) == string.len(user_code)) then
  23.       if (code == user_code) then
  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.   end
  36. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement