This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
pingwel's profile picture

Noob question for a door locking system

Started by pingwel, 15 June 2014 - 05:17 PM
pingwel #1
Posted 15 June 2014 - 07:17 PM
Hello there i have this small program i can't get to work. im really new at this and have just been trying something.

The idea of this code is having a main computer (command) that will be able to send a command to the computer (lockdown)
but if the command arnt sending the command the lockdown computer will use the sensor next to it instead and allow people to come throw the door until it again will get the command to lockdown.

i do hope it's understandble (sry for my english)

Spoiler

m = peripheral.wrap("left")
rednet.open("back")

--while true do	
function command()
  local a, b = rednet.receive()
  if b =="lockdown on" then
	print(B)/>/>/>/>
	redstone.setOutput("right", true)
elseif b =="lockdown off" then
	print(B)/>/>/>/>
	redstone.setOutput("right", false)
  end
end

function sensor()
  print("sensing")
  names = m.getPlayerNames()
  if names[1] == nil then
	redstone.setOutput("right", true)
	else for i = 1, #names do
	  redstone.setOutput("right", false)
	  sleep (2)
	  print(names[i])
	end
  end
end

while true do
  if command == true then
  command()
	else
  sensor()
  end
end
TheOddByte #2
Posted 15 June 2014 - 10:57 PM
Ok first of all, what error are you getting?
And your indention seems kinda messed up :P/> ( But that may be the boards fault )

And you can't have the same name for a variable and a function.

function command()
    ...
end

while true do
    if command == true then
        command()
    else
        sensor()
    end
end
But I don't see a variable called command in your code though :P/>
Try doing this

local command_enabled = true

...

while true do
    if command_enabled then
        command()
    else
        sensor()
    end
end

( The three dots indicate code :P/> )
pingwel #3
Posted 16 June 2014 - 03:48 PM
Ok first of all, what error are you getting?
And your indention seems kinda messed up :P/> ( But that may be the boards fault )

And you can't have the same name for a variable and a function.

function command()
	...
end

while true do
	if command == true then
		command()
	else
		sensor()
	end
end
But I don't see a variable called command in your code though :P/>
Try doing this

local command_enabled = true

...

while true do
	if command_enabled then
		command()
	else
		sensor()
	end
end

( The three dots indicate code :P/> )

thanks for the reply.
i tryed adding what you said and if command_enabled are true then it will only use the command function and not use the sensor.
and when its on false it will only respond on the sensor and not use the command

and i dont really get an error when using the program.
The problem is that i want it to be sort of standby on the sensor and keep Looping on that until it get a redstone messege so it will be "taken over" by the command function instead. and when its off that it will go back to stanby on sensor
TheOddByte #4
Posted 17 June 2014 - 12:19 AM
Do you mean Rednet message? And anyway.. You could use a timer and os.pullEvent to change your loop a little to achieve this.
The timer would basically update it all the time, and os.pullEvent would check if either the timer event was pulled or if you received a rednet message.


local updateTimer = os.startTimer( 2 ) -- Start the timer for the first time

...

while true do
    local e = { os.pullEvent() }
    if e[1] == "rednet_message" then
        --[[
            Here e[1] is the event, in this case rednet_message, e[2] is the id, e[3] is the message
            and e[4] would be the distance if I got all of them in correct order
        --]]
        if e[3] == "enable_command" then -- If it recieves the message 'enable_command' it will turn the boolean 'command_enabled' to true
            command_enabled = true
        elseif e[3] == "disable_command" then
            command_enabled = false -- Added this just incase you want to turn of command
            updateTimer = os.startTimer( 2 ) -- Start the timer again since we disabled command
        end

    elseif e[1] == "timer" then
        if e[2] == updateTimer then
            updateTimer = os.startTimer( 2 ) -- Restart the timer
        end
    end

    if command_enabled then
        command()
    else
        sensor()
    end
end
pingwel #5
Posted 17 June 2014 - 02:27 PM
i have added the code you made there and i kinda understand what it should do and how its done. but it dosnt return to sensor after command_enabled have been set to false
TheOddByte #6
Posted 17 June 2014 - 02:54 PM
i have added the code you made there and i kinda understand what it should do and how its done. but it dosnt return to sensor after command_enabled have been set to false
Try doing this as a debug in the event part

local e = { os.pullEvent() }
for i, v in ipairs( e ) do
    print( i .. ": " .. v )
end

...
to check if I put the indexes correct for the rednet part :P/>
If it was correct it would kinda print this out

1: rednet_message
2: <id>
3: <message received>
4: <distance>
pingwel #7
Posted 17 June 2014 - 04:55 PM
i have added the code you made there and i kinda understand what it should do and how its done. but it dosnt return to sensor after command_enabled have been set to false
Try doing this as a debug in the event part

local e = { os.pullEvent() }
for i, v in ipairs( e ) do
	print( i .. ": " .. v )
end

...
to check if I put the indexes correct for the rednet part :P/>
If it was correct it would kinda print this out

1: rednet_message
2: <id>
3: <message received>
4: <distance>
when i give a command it only outputs (1: redstone) and nothing els
TheOddByte #8
Posted 17 June 2014 - 07:08 PM
when i give a command it only outputs (1: redstone) and nothing else
Try sending a message to the computer with this code without changing the redstone input, for example

rednet.send( "<ID of computer with teh code above>", "Test message, should show up in index 3" )
if it shows 1: redstone it means that the redstone input has changed http://www.computerc...Redstone_(event)
go and check out os.pullEvent on the wiki to get a better understanding of it > http://www.computerc...ki/Os.pullEvent
SpoilerDoes this work for you? Please read the comments I've added to get a better understanding of what I did

--[[
    Changes to script:
        *Changed variables and functions to local, it's uneccessary 
        for them to be global unless you plan on accessing them in
        another script.

        *Removed the command function and put the code into the event part

        *Removed the sleep call
--]]

local m = peripheral.wrap("left")
local command_enabled = true;
local updateTimer = os.startTimer( 2 )
rednet.open( "back" )



local function sensor()
    print("sensing")
    local names = m.getPlayerNames()
    if names[1] == nil then
        redstone.setOutput("right", true)
    else 
        rs.redstone.setOutput( "right", false ) -- I don't know why you set the output to false repeateadly, it's better todo it right before the loop
        for i = 1, #names do
            -- Removed the sleep here since that clears the event que and stops the timer
            print(names[i])
        end
    end
end

while true do
    local e = { os.pullEvent() }
    if e[1] == "rednet_message" then

        --# Check if it's an enable or disable message
        if e[3] == "enable_command" then
            command_enabled = true;
        elseif e[3] == "disable_command" then
            command_enabled = false;
        end

        --# Handle command if it's enabled
        if command_enabled then
            if e[3] == "lockdown on" then
                print( e[3] )
                rs.redstone.setOutput( "right", true )
            elseif e[3] == "lockdown off" then
                print( e[3] )
                rs.redstone.setOutput( "right", false )
            end
        end

    elseif e[1] == "timer" then
        if e[2] == updateTimer then
            updateTimer = os.startTimer( 2 )
            --# Use the sensor here instead if the command is not enabled
            if not command_enabled then
                sensor()
            end
        end
    end
end

Also, please post your current code if you're still having problems
Edited on 17 June 2014 - 05:32 PM
pingwel #9
Posted 17 June 2014 - 07:15 PM
when i give a command it only outputs (1: redstone) and nothing else
Try sending a message to the computer with this code without changing the redstone input, for example

rednet.send( "<ID of computer with teh code above>", "Test message, should show up in index 3" )
if it shows 1: redstone it means that the redstone input has changed http://www.computerc...Redstone_(event)
go and check out os.pullEvent on the wiki to get a better understanding of it > http://www.computerc...ki/Os.pullEvent

okay i did the new rednet.send command and now it says:
1: timer
2: 0
1: timer
2: 1
TheOddByte #10
Posted 17 June 2014 - 07:37 PM
when i give a command it only outputs (1: redstone) and nothing else
Try sending a message to the computer with this code without changing the redstone input, for example

rednet.send( "<ID of computer with teh code above>", "Test message, should show up in index 3" )
if it shows 1: redstone it means that the redstone input has changed http://www.computerc...Redstone_(event)
go and check out os.pullEvent on the wiki to get a better understanding of it > http://www.computerc...ki/Os.pullEvent

okay i did the new rednet.send command and now it says:
1: timer
2: 0
1: timer
2: 1
Have you tried the code in the spoiler? Are you sure you have a modem attached aswell? the timer updates about every 2 seconds, but it would be more accurate with 1.6 I believe( or maybe that was just for turtles ) since 1 minecraft second is 0.8 :P/>
Also, do you have a modem attached to the computer who sends to this computer? It may be some silly mistake :P/>
pingwel #11
Posted 17 June 2014 - 07:53 PM
Yes i have the modem on both computers xD but im just really new at this so im not 100% sure what to do when and what the diffrend stuff are completly :/
so in some way i need some training/help with learning it :)/>
TheOddByte #12
Posted 17 June 2014 - 08:12 PM
Yes i have the modem on both computers xD but im just really new at this so im not 100% sure what to do when and what the diffrend stuff are completly :/
so in some way i need some training/help with learning it :)/>
Well make sure both modems are wrapped, if you're still new to all this it would be best to start with using the rednet api for modems since that's very basic, then when you feel like you've got a hang of that you should move on to the modem api which is a bit more advanced.
Check out the wiki and the tutorials section to learn more, mess around with some code, check out other useful links you may find on google or even youtube tutorials.
These are some great ways to learn.

Note: To see that the modems are enabled they should look like this( notice that it's red )