Advertisement
s3ptum

Door sensor

Oct 10th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. -- Credit to this for getting me started: http://www.youtube.com/watch?v=34R4KfLmMSY
  2. os.loadAPI("ocs/apis/sensors")
  3.  
  4. -- From: http://www.youtube.com/watch?v=34R4KfLmMSY
  5. function printDict(data)
  6. for i,v in pairs(data) do
  7. print(tostring(i).." - "..tostring(v))
  8. end
  9. end
  10.  
  11. function getDistance(sensorData,targetData)
  12. distanceData = {}
  13. distanceData["xDistance"] = math.abs(sensorData["xCoord"] - targetData["xCoord"])
  14. distanceData["yDistance"] = math.abs((sensorData["yCoord"] - 2) - targetData["yCoord"]) -- Note: the - 2 is because the sensor is above my door
  15. distanceData["zDistance"] = math.abs(sensorData["zCoord"] - targetData["zCoord"])
  16.  
  17. -- Add the largest distance to the array
  18. distanceData["distance"] = distanceData["xDistance"]
  19. if distanceData["yDistance"] > distanceData["distance"] then
  20. distanceData["distance"] = distanceData["yDistance"]
  21. elseif distanceData["zDistance"] > distanceData["distance"] then
  22. distanceData["distance"] = distanceData["zDistance"]
  23. end
  24.  
  25. return distanceData
  26. end
  27.  
  28. function isPlayerClose()
  29. -- Get list of targets in range of probe
  30. targets = sensors.getAvailableTargetsforProbe(controller,sensorName,probeName)
  31.  
  32. -- Loop through this list of targets getting the info about each one
  33. for i=1,# targets do
  34. -- Get the target data, i.e. x,y,z coods and type
  35. targetData = sensors.getSensorReadingAsDict(controller,sensorName,targets[i],probeName)
  36.  
  37. -- If the target is a player, i.e. has the name "vq" then check if it is close
  38. if targetData["name"] == "vq" then
  39. -- Get the distances to the target
  40. distanceData = getDistance(sensorData,targetData)
  41.  
  42. -- If a player is close enough return true
  43. if distanceData["distance"] < 4 then
  44. return true
  45. end
  46. end
  47. end
  48.  
  49. -- If no players were close return false
  50. return false
  51. end
  52.  
  53.  
  54. -- Get the side the controller is on
  55. controller = sensors.getController()
  56.  
  57. -- Get sensors, e.g. FactoryDoor. Useful to find the names of your sensors
  58. -- data = sensors.getSensors(controller)
  59. -- printDict(data)
  60.  
  61. -- Set the sensor name for the one you wish to control
  62. sensorName = "Sensor"
  63.  
  64. -- Get information about the sensor, e.g. its name, location, range
  65. sensorData = sensors.getSensorInfo(controller,sensorName)
  66.  
  67. -- Change sensor range
  68. sensors.setSensorRange(controller,sensorName,"3")
  69.  
  70. -- Get probes, e.g. TargetInfo, Players, etc
  71. -- IMPORTANT: the program will fail unless the line below is called, Im not sure why but I guess it is a bug
  72. data = sensors.getProbes(controller,sensorName)
  73. -- printDict(data)
  74. probeName = "TargetInfo"
  75.  
  76. -- Loop the program so it keeps checking for targets, making sure it sleeps when in any infinite loop
  77. while true do
  78. if isPlayerClose() then
  79. redstone.setOutput("back",true)
  80.  
  81. -- Keep the door open until the player gets a certain distance away
  82. while isPlayerClose() do
  83. sleep(.5)
  84. end
  85. redstone.setOutput("back",false)
  86. end
  87. sleep(.5)
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement