Advertisement
ApexdaUser

Motion Sensor thing i need to test

Apr 15th, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. local component = require("component")
  2. local sides = require("sides")
  3. local event = require("event")
  4. local term = require("term")
  5. local thread = require("thread")
  6. local rs = component.redstone
  7. local gpu = component.gpu
  8.  
  9.  
  10. -- Config
  11. local delay, isOpen = 5, (rs.getOutput(sides.right) > 0) and true or false
  12. local timer = nil
  13.  
  14. term.clear()
  15. term.setCursor(1, 1)
  16. gpu.setForeground(0x00FF00)
  17. print("--------------------------- ToNELvision MICROSYTEMS ---------------------------\n")
  18. gpu.setForeground(0xFFFFFF)
  19. print("Welcome. Please enter one of the displayed commands. It may take a few seconds for the command to be interpreted.\n")
  20. gpu.setForeground(0xE6AB38)
  21. print("Open\nClose")
  22. term.setCursor(1, 9)
  23.  
  24.  
  25. local inputThread
  26. inputThread = thread.create(function()
  27. while true do
  28. local input = term.read(nil, false)
  29. if type(input) == "string" then
  30. input = input:sub(1, -2)
  31. elseif type(input) == "boolean" and input == false then
  32. print("interrupted")
  33. inputThread:kill()
  34. end
  35.  
  36. term.clearLine()
  37.  
  38. if input == "open" or input == "Open" then
  39. if not isOpen then
  40. rs.setOutput(sides.right, 15)
  41. isOpen = true
  42. if timer then event.cancel(timer) end
  43. timer = event.timer(delay, function()
  44. if isOpen then
  45. rs.setOutput(sides.right, 0)
  46. isOpen = false
  47. timer = nil
  48. end
  49. end)
  50. end
  51. elseif input == "close" or input == "Close" then
  52. if isOpen then
  53. rs.setOutput(sides.right, 0)
  54. event.cancel(timer)
  55. isOpen = false
  56. end
  57. elseif input == "quit" or input == "Quit" then
  58. inputThread:kill()
  59. end
  60. end
  61. end)
  62.  
  63. local motionThread = thread.create(function()
  64. while true do
  65. local _, _, x, y, name = event.pull("motion")
  66. if not isOpen then
  67. rs.setOutput(sides.right, 15)
  68. isOpen = true
  69. if timer then event.cancel(timer) end
  70. timer = event.timer(delay, function()
  71. if isOpen then
  72. rs.setOutput(sides.right, 0)
  73. isOpen = false
  74. timer = nil
  75. end
  76. end)
  77. else
  78. event.cancel(timer)
  79. timer = event.timer(delay, function()
  80. if isOpen then
  81. rs.setOutput(sides.right, 0)
  82. isOpen = false
  83. timer = nil
  84. end
  85. end)
  86. end
  87. end
  88. end)
  89.  
  90. thread.waitForAny({inputThread, motionThread})
  91. print("\nProgram Exit")
  92. os.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement