Use sensor (OpenPeripheral) for open door

  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord
A

Arcy

Guest
Hi,

I have a program for open my door with sensor but my door is permanently opened.
For avoid this, I thinking implement coordinates of player to open or close door but I fail to get properties of object returned.

For players lists at proximity, I written :

[...]
while true do
list = sensor.getPlayers()

for i in pairs(list) do
p = list.name

if p == "Arcy" then
open = true​
else
open = false​
end

end

end
[...]


Thanks :)
 

Henry Link

Forum Addict
Dec 23, 2012
2,601
553
153
USA - East Coast
Should be pretty straight forward to test that part of the code. Just insert a print statement and observer the computer after you move close to the sensor and away from it. If it works you should see the changes on the screen. Also, I would avoid using a variable called open. Since it is part of API that is used for file manipulation.
Code:
for i in pairs(list) do
  p = list.name
  if p == "Arcy" then
    od = true
  else
    od = false
  end  print("Open = ", od)
end
 
A

Arcy

Guest
Erf, now, my program detect me but distance is completely ramdom :(

My door open or close at any distance.

local sensor = peripheral.wrap("top")
local openDoor = false

while true do
list = sensor.getPlayers()

for i in pairs(list) do
p = list.name

if (p == "Arcy")

openDoor = true
else
openDoor = false
end
end

if (openDoor) then

print(os.time().." Hello "..p)
else
print(os.time().." Nobody")
end

redstone.setOutput("left", openDoor)
os.sleep(1)
end
 

Henry Link

Forum Addict
Dec 23, 2012
2,601
553
153
USA - East Coast
The code looks correct. How are you connecting the redstone to the door? Also, what are using for the sensor? I see it is a device attached to the top, but I don't know what it is. A few screenshots of your setup might help. Also, it would make the code more readable is you used the code tags. Use the insert icon on the tool bar just to the left of undo/redo arrows.
 
A

Arcy

Guest
1478286602-minecraft-ftb.jpg

OpenPeripheralAddons 0.5.1 (<- Sensor / ID 2323)
OpenPeripheralCore 1.3
OpenPeripheralIntegration 0.5
ComputerCraft 1.75
(Feed the Beast Infinity)

Code:
local sensor = peripheral.wrap("top")
local openDoor = false

while true do
        list = sensor.getPlayers()

        for i in pairs(list) do
            p = list.name

           if (p == "Arcy")
                openDoor = true
            else
                openDoor = false
            end
        end

        if (openDoor) then
            print(os.time().." Hello "..p)
        else
            print(os.time().." Nobody")
        end

        redstone.setOutput("left", openDoor)
        os.sleep(1)
end

Currently, redstone lamp is the "door" (with this problem, I have choosen a another solution - "Remote" of WR-CBE Addons) but during testing, the real door (and lamp) remained open/lit.
 

Henry Link

Forum Addict
Dec 23, 2012
2,601
553
153
USA - East Coast
I'll try to do some testing of this later tonight. But, if you really don't care who the player is. There is player detector from Dracronic Evolution that will just give a redstone signal when players are near. If I remember correctly you can set the trigger distance as well.
 

Henry Link

Forum Addict
Dec 23, 2012
2,601
553
153
USA - East Coast
Well I figured out part of the problem. The sensor.getPlayers command never returns my name using your code above. Let me do some more digging. It always returns "Nobody".
 

Henry Link

Forum Addict
Dec 23, 2012
2,601
553
153
USA - East Coast
OK so I did figure out some stuff. The range is about 3 block. But it just seems to pause if there is no data.
http://pastebin.com/GD0qevGd will give you an idea how to get the data. Just edit the "jc2xs" and put in your player name.
I'm still might do some more poking around with it. But the main issue is more of detecting when no one is around rather than detecting the player.
 

Henry Link

Forum Addict
Dec 23, 2012
2,601
553
153
USA - East Coast
Alright... I'm done. Here is final code I got working. Range tested at 5 blocks.
http://pastebin.com/8K4g4zwZ

Basic issue is that when there is no player in range it doesn't return a table with data. So you have to detect if the table has data or not. If there is no data then close the door. Other wise you can go through the data using the pair loop. Ugh.. I'll probably never use the sensor.
 
A

Arcy

Guest
Hmmm, your last code is functional but redstone don't always react.

When computer say "Hello Arcy" (openDoor = true), the redstone is ON
But when is "Nobody" (openDoor = false), the redstone can be ON.

I see with draconic detector.

Thanks for your help :)
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
Glancing at the code I can see some errors.
First of, lack of the keyword "local" though it isn't an true error I really recommend using it especially if you are planning to make bigger problems
Code:
local sensor = peripheral.wrap("top")
local openDoor = false
local list={}
local opendoor,p=nil
while true do
   list = sensor.getPlayers()
   if not next(list) then
     openDoor = false
   end
   for i in pairs(list) do
     p = list[i].name
     if (p == "jc2xs") then
       openDoor = true
       --break out of the loop
       break
     else
       --if other players are in the range it won't open it for them
       openDoor=false
     end
   end
   if (openDoor) then
     print(os.time().." Hello "..p)
   else
     print(os.time().." Nobody")
   end
   redstone.setOutput("left", openDoor)
   os.sleep(1)
end
My changes are that it stops with looping over the table as soon as it found the correct player. I also changed it so that if there are players around it will also turns the redstone off. Before it would only turn the redstone off if no players where around

edit: just to clarify, I did not test any of the code I wrote.