nap14hockey

RFID

Sep 6th, 2014
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1. ---------------------------------------------------------------------------------------
  2. -- MineCraft ComputerCraft - RFID Door lock (use Immibis's Peripherals)
  3. -- Written by Ty94, ComputerCraft Info Forums
  4. -- Source: http://www.computercraft.info/forums2/index.php?/topic/3984-rfid-door-lock-use-immibiss-peripherals/
  5. ---------------------------------------------------------------------------------------
  6. --If you want this to start at every boot of a computer run the following line of code in the startup
  7. -- shell.run("RFID","door")
  8.  
  9. --The door stays open as long as you stay in the area of the reader
  10. --                        !!! Make sure there is no creeper behind you !!!
  11. -- Set up your own door system
  12.  
  13. doorPass = "wsxedcrfvbgtnhzmjuzvrnzcif" --Max Length 82 chars
  14. cardLabel = "Ident: Front Door"  --Max Length 20 chars
  15. --Make sure the following sides are equal to your build
  16.  
  17. readerSide = "left"
  18. writerSide = "right"
  19. redstoneSide = "bottom"
  20.  
  21. --(Don“t change)
  22. tArgs = {...}
  23. lastTimer = {}
  24.  
  25. -- Check the arguments given
  26. if #tArgs < 1 then
  27. print("Usage:")
  28. print("RFID write")
  29. print("RFID door")
  30. return
  31. end
  32.  
  33. mode = tArgs[1]
  34. shell.run("clear")
  35.  
  36. if mode == "write" then
  37. writer = peripheral.wrap(writerSide)
  38. print("Enter writing mode")
  39. print("Insert card in the writer")
  40. while not writer.isPresent() do --Waits until a card is in the writer
  41.   sleep(0)
  42. end
  43.  
  44. if writer.encode(doorPass, cardLabel) then --Start write to card
  45.   print("Start writing Pass to card")
  46. else --Error handling
  47.   if writer.isCoded() then
  48.    print("Card is already coded\nInsert a blank card")
  49.   elseif #cardLabel > 20 then
  50.    print("Card Label is too long max. 20 char")
  51.   elseif #doorPass > 82 then
  52.    print("Data is too long max. 82 char")
  53.   else
  54.    print("Leave the card in the writer")
  55.   end
  56.   return
  57. end
  58. os.pullEvent("rfid_written") --Wait until the card is written
  59. print("RFID card is coded")
  60. print("Take the card out of the writer")
  61.  
  62. while writer.isCoded() do --Ends the program when the card is taken out of the writer
  63.   sleep(0)
  64. end
  65. shell.run("clear")
  66.  
  67. ------------------------------------------------------
  68. ------------------------------------------------------
  69. elseif mode == "door" then
  70. reader = peripheral.wrap(readerSide)
  71. print("Door Contol is running")
  72. while true do
  73.   reader.scan() --Creats an "rfid_detected" when a card is in range
  74.   event, p1 = os.pullEvent()
  75.   if event == "rfid_detected" then
  76.    if p1 == doorPass then
  77.         rs.setOutput(redstoneSide, true)
  78.         lastTimer = os.startTimer(1)
  79.    end
  80.   elseif event == "timer" and lastTimer == p1 then
  81.    rs.setOutput(redstoneSide, false)
  82.   end
  83. end
  84. end
Advertisement
Add Comment
Please, Sign In to add comment