I'm trying to make a program able to detect if I am in an area so a door opens.
The door is a carriage translocator from JAKJ redstone in motion. It works with a redstone signal.
I've derped around to find a correct way to exploit the OpenCCSensors' sensor (with the proximity sensor card).
Here's my code:
print("start")
whitelist = {"furest","arthuremen1"}
print("whitelist done")
os.loadAPI("ocs/apis/sensor")
sensor = peripheral.wrap("left")
-- vérifie si le joueur détecté est whitelist
function checkexist(nickname)
for key, nom in pairs(whitelist) do
if nom == nickname then
return true
end
end
return false
end
--Ouvre la porte
function Ouvre()
rs.setOutput("bottom", true)
sleep(0.2)
rs.setOutput("bottom", false)
end
--Ferme la porte
function Ferme()
rs.setOutput("right", true)
sleep(0.2)
rs.setOutput("right", false)
end
--Distance du centre de détection
local offset = {
X = 0,
Y = 8,
Z = 0
}
local rayon = 3
--Calcule la disa=tance
function distance(pos)
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
function nearby()
print("check")
t = sensor.getTargets()
for name, details in pairs(t) do
print(name)
if details.Name == "Player" then
local x = sensor.getTargetDetails(name)
print(x.Username)
if checkexist(x.Username) and distance(details.Position) < rayon then
print(x.Username .. " is there!")
return true
else
return false
end
else
return false
end
end
end
while true do
if nearby() == true then
Ouvre()
elseif nearby() == false then
Ferme()
end
end
The last 7 lines are the program itself, the rest are just functions that are called.
So it's simple, is the player is in the area, it sends a signal to 1 translocator to open the door. If the player stays in this area, it should continue to form a pulse (useless but it's the most efficient way I've find).
If the player is gone, it will send a redstone pulse to the bottom so the second carriage translocator closes the door.
But in fact, when I'm nearby the door sometimes it opens sometimes not. And when I go 5-10 blocks away, the computer no longer try to check the area and the program don't work anymore…
It also stops by itself even if I don't move…
That's kinda odd because it's a loop that nothing could be able to break. But it do…
Thank you for the help :)/>
Furest-