View difference between Paste ID: 24Pv7vfs and PB4ib0XV
SHOW: | | - or go back to the newest paste.
1-
local c = require("component")
1+
local c = require("component")
2-
local keypad = c.os_keypad
2+
local keypad = c.os_keypad
3-
local computer = require("computer")
3+
local computer = require("computer")
4-
local event = require("event")
4+
local event = require("event")
5-
5+
6-
local code = "12345678"
6+
local code = "12345678"                                        -- Your code (Max 8 digits to fit on keypad display)
7-
local user_code = ""
7+
local user_code = ""                                           -- Variable that stores input
8-
8+
9-
local run = true
9+
local run = true
10-
while run do
10+
while run do
11-
  local e, address, _, key = event.pull(1000)
11+
  local e, address, _, key = event.pull(1000)
12-
  if (e and e == "interrupted") then
12+
  if (e and e == "interrupted") then                           -- Kill program when ctrl + c is pressed
13-
    run = false
13+
    run = false
14-
  elseif (e and e == "keypad") then
14+
  elseif (e and e == "keypad") then
15-
    if (key and key == "*") then
15+
    if (key and key == "*") then                               -- Uses star button as reset key
16-
      user_code = ""
16+
      user_code = ""
17-
      keypad.setDisplay("")
17+
      keypad.setDisplay("")
18-
    elseif (key) then
18+
    elseif (key) then                                          -- On key other than reset key add it to user_code
19-
      user_code = user_code .. key
19+
      user_code = user_code .. key
20-
      keypad.setDisplay(user_code)
20+
      keypad.setDisplay(user_code)                             -- Update display to updated input string
21-
    end
21+
    end
22-
    if (string.len(code) == string.len(user_code)) then
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
23+
      if (code == user_code) then                              -- Test if they match
24-
        computer.beep(2000)
24+
        computer.beep(2000)
25-
        print("Code correct!")
25+
        print("Code correct!")
26-
        keypad.setDisplay("CORRECT")
26+
        keypad.setDisplay("CORRECT")
27-
        user_code = ""
27+
        user_code = ""
28-
      else
28+
      else
29-
        computer.beep(100)
29+
        computer.beep(100)
30-
        print("Wrong code!")
30+
        print("Wrong code!")
31-
        keypad.setDisplay("WRONG")
31+
        keypad.setDisplay("WRONG")
32-
        user_code = ""
32+
        user_code = ""
33-
      end
33+
      end
34-
    end
34+
    end
35-
  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