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:
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