Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Automatic door opener v2: By -Molinko
- local cmdArgs = {...} --cmd line arguments
- os.loadAPI("ocs/apis/sensor")
- if #cmdArgs < 2 then
- print("Usage: 'progName' 'redstoneOutSide' ',' 'radius'")
- print("redstoneOutputSide cant be same as sensor side..")
- end
- local proxSide
- local dSide = tostring(cmdArgs[1]) -- door output side
- local radius = tonumber(cmdArgs[2]) -- sensitivity to proximity
- local sides = rs.getSides()
- local offset = {}
- --[[ Functions ]]--
- local function findPeripheral(type)
- for key, side in pairs(sides) do
- if peripheral.isPresent(side) and peripheral.getType(side) == type then
- return side
- end
- end
- end
- local function findSensor()
- local side = findPeripheral("sensor")
- if side then
- proxSide = side
- return sensor.wrap(side)
- else
- error("Sensor wasn't found..")
- end
- end
- local prox = findSensor()--wrap sensor(prox)
- local function findAdvancedMonitor()
- local side = findPeripheral("monitor")
- if side then
- local test = peripheral.wrap(side)
- if test.isColor() then
- return peripheral.wrap(side)
- else
- error("Monitor is not an 'Advanced Monitor'..")
- end
- else
- error("No monitor was found")
- end
- end
- tMon = findAdvancedMonitor()-- wrap advanced monitor
- local function mPrint(text)
- tMon.write("\n"..text)
- end
- --PROBLEM...
- local function getOffset()--not storing value of Position
- local offset = {"X","Y","Z"}-- in offset{}
- local target = prox.getTargets()
- for k,v in pairs(target) do
- for i = 1,1 do
- offset.i = v.Position.i
- end
- end
- return offset.X, offset.Y, offset.Z
- end
- local function calibrate()
- tMon.setTextScale(.5)
- tMon.setCursorPos(1,2)
- mPrint("Touch screen when")
- mPrint("standing in door location.")
- local event = os.pullEvent("monitor_touch")
- if event == "monitor_touch" then
- offset = getOffset()
- tMon.clear()
- end
- return offset
- end
- --calibration cmd arg
- if "calibrate" == tostring(cmdArgs[1]) then
- calibrate()
- end
- --Problem: not receiving offset from calibration(nil value)
- --define radius at offset
- local function dist(pos)--rpoblem relatd to calibrate()
- local xd = pos.X - offset.X
- local yd = pos.Y - offset.Y
- local zd = pos.Z - offset.Z
- return math.sqrt(xd*xd + yd*yd + zd*zd)
- end
- --[[ Main Loop ]]--
- while true do
- local dOpen = false -- no signal to door
- local targets = prox.getTargets()
- for k,v in pairs(targets) do
- if dist(v.Position) < radius then
- dOpen = true-- if target within radius
- end
- end
- rs.setOutput(dSide, dOpen)--open or close door
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement