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

Password protected Door + secundary button

Started by Max50005, 09 June 2012 - 05:50 PM
Max50005 #1
Posted 09 June 2012 - 07:50 PM
I need help with my door-lock-programm. Entering password to get inside and just press a button to get outside. I already tried those parts an their own and they work, however now I'm stuck with combining them.

I do have quite a bit of programming experience (although not in lua) but I never had to use multitasking.

Spoilerfunction pwd()
[indent=1]term.clear()
term.setCursorPos(1,1)

print("Please enter password:")
t=read("*")[/indent]
[indent=1]
if t == "chamber" then[/indent]
[indent=2]rs.setBundledOutput("bottom", 0)
os.sleep(5)
rs.setBundledOutput("bottom", 2048)[/indent]
[indent=1]else[/indent]
[indent=2]print("Wrong password!")
os.sleep(2)[/indent]
[indent=1]end[/indent]
end

function button()
[indent=1]event = os.pullEvent()[/indent]
[indent=1]if event == "redstone" and rs.getBundledInput("bottom") == 1 then[/indent]
[indent=2]rs.getBundledOutput("bottom", 0)
os.sleep(5)
rs.getBundledOutput("bottom", 2048)[/indent]
[indent=1]end[/indent]
end

while true do
[indent=1]parallel.waitForAny(pwd(), button())[/indent]
[indent=1]if t == "system" then break end[/indent]
end

I know their is a bit to clean up, but first it has to work.
As you see I tried to work with the parallel class, but I don't quite get how it works.
I would be nice if someone could help here and for later clean up, is it possible to assign return values, if a function is called by waitForAny/All?
archit #2
Posted 09 June 2012 - 08:56 PM
Not really understanding the logic in this one to be honest.

waitForAny is designed really to be used for allowing a constant source of input/output to be channeled from multiple functions from what I gathered.

You need to redesign your code so it's in a constant state of sending/receiving. Get rid of most of your sleep commands as well.

in quick pseudocode:

while true {
input password
if correct open door
}

while true {
if button pushed
open door
}

while true {
waitForAny(password_func, button_func)
}

This should get you going.
This link could also help http://www.computercraft.info/forums2/index.php?/topic/2030-tutorial-parallel-api/
MysticT #3
Posted 09 June 2012 - 09:54 PM
parallel.waitForAny will return when any of the functions returns/ends, so you just need to make the functions loop, and when you need to exit just stop/break one of them.
Also, there's some errors in the code, I fixed them and commented the changes:

function pwd()
  while true do
	term.clear()
	term.setCursorPos(1,1)
	print("Please enter password:")
	local pass = read("*")
	if pass == "chamber" then
	  rs.setBundledOutput("bottom", 0)
	  os.sleep(5)
	  rs.setBundledOutput("bottom", colors.blue) -- use colors.<color>, more readable
	elseif pass == "system" then
	  break -- break the loop to end the function
	else
	  print("Wrong password!")
	  sleep(2)
	end
  end
end

function button()
  local event = os.pullEvent("redstone") -- pull only redstone events
  if rs.testBundledInput("bottom", colors.white) then -- no need to check the event type. it's easier to use testBundledInput for this
	rs.setBundledOutput("bottom", 0) -- set, not get
	sleep(5)
	rs.setBundledOutput("bottom", colors.blue)
  end
end

parallel.waitForAny(pwd(), button())