Jump to content




Door Lock

lua computer networking

3 replies to this topic

#1 L00K3

  • Members
  • 16 posts

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


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.

#3 L00K3

  • Members
  • 16 posts

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".

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 02 February 2015 - 09:57 AM

I could likely comment further, but I'd need to see your updated code first.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users