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

Door Lock

Started by L00K3, 24 January 2015 - 10:24 AM
L00K3 #1
Posted 24 January 2015 - 11:24 AM
I'm making a program that will control a 3 x 3 piston door and uses coroutines, but crashes on load.

This is the code:


--Ask's for password
function main()
io.write("Enter Password: ")
passwordEntered = io.read()

if (passwordEntered == passwordSet) then
   passwordCorrect = true
end

while (passwordCorrect == false) do
   io.write("Password Incorrect, Re-Enter Password:")
  
   if (passwordEntered == passwordSet) then
  passwordCorrect = true
   end
end
end--Test for a signal from a second computer, locks if signal is true
function signaltest()
label = cheackPasswordForST
if (passwordCorrect == true) then
  print("Signal Tester Started")
  while (signalRecieved == false) do
   if (redstone.getOutput("top", true) == true) then
    signalRecieved = true
   end
  end
else
  sleep(2)
  goto = cheackPasswordForST
end

end-- Asks on host computer if user wants to lock door
function userInput()
label = cheackPasswordForUI
if (passwordCorrect == true) then
  print("User Input Started")
  local stateReset = true  while (stateReset == true) do
   io.write("Lock (y/n):")
    stateRecieved = io.read()
   if (stateRecieved == "n") then
    stateReset = true
   else
    signalRecieved = true
    stateReset = false
   end
  end
else
  sleep(2)
  goto = cheackPasswordForUI
end

end
--Opens door on correct password, locks if user asks to lock door
function doorOpen()

label = testForOpen
if (passwordCorrect == true) then
  print("Door Opened")
  redstone.setOutput("back", true)
else
  sleep(2)
  goto = testForOpen
end

label = testForSignal
if (signalRecieved == true) then
  print("Door Closed")
  redstone.setOutput("back", false)
  redstone.setOutput("bottom", true)
  redstone.setOutput("bottom", false)
  os.reboot()
else
  goto = testForSignal
end
end-- Global Variables
passwordEntered = "na"
passwordSet = "admin"
stateRecieved = "n"
signalRecieved = false
passwordCorrect = false
threadMain = "nil"
threadST = "nil"
threadUI = "nil"
threadDO = "nil"
--Sets Threads Up
threadMain = coroutine.create(main)
  coroutine.resume(threadMain)
threadDO = coroutine.create(doorOpen)
  coroutine.resume(threadDO)
threadST = coroutine.create(signaltest)
  coroutine.resume(threadST)
threadUI = coroutine.create(userInput)
  coroutine.resume(threadUI)-- Loops to prevent program from closing
while true do
end
Bomb Bloke #2
Posted 24 January 2015 - 01:29 PM
At a glance, this is the most obvious problem:

threadMain = coroutine.create(main)
  coroutine.resume(threadMain)
threadDO = coroutine.create(doorOpen)
  coroutine.resume(threadDO)
threadST = coroutine.create(signaltest)
  coroutine.resume(threadST)
threadUI = coroutine.create(userInput)
  coroutine.resume(threadUI)-- Loops to prevent program from closing
while true do
end

"Start a co-routine, run it until it yields, then do the same with the next and so on. Once they've all yielded or returned once, don't bother to run them again, just start an infinite loop that does nothing."

Sounds like you want to replace that whole thing with:

parallel.waitForAll(main,doorOpen,signaltest,userInput)

… which will automatically build co-routines out of your function pointers, and keep each one running until it returns.

Also, be advised that "goto" is not a reserved keyword in Lua 5.1 (the version in use by ComputerCraft), and nor is "label". Stick with while/repeat/for loops for that sort of thing.
L00K3 #3
Posted 02 February 2015 - 09:45 AM
thanks but it exits after main has finished (i think possibly it could have run the others in the background) but i need to take input from "userInput".
Bomb Bloke #4
Posted 02 February 2015 - 09:57 AM
I could likely comment further, but I'd need to see your updated code first.