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

[lua] [question] How to use Parallel API in my code

Started by jamesscape2, 10 May 2012 - 09:45 AM
jamesscape2 #1
Posted 10 May 2012 - 11:45 AM
Hi what i am trying to do is have my master computer open all the doors on a rednet signal, but as well have the computer being able to open the door separately.

This is the Code for the Master Computer, it does Work but any modification u make will help.

Spoilerfunction UserCorrect()
print(" Username correct ")
sleep(sleepTime2)
write(" Enter password: ")
inputPass = read("*")
if inputPass == pass then
print(" Password Correct ")
sleep(sleepTime2)
rednet.broadcast("OPEN")
sleep(sleepTime3)
print(" Working ")
rednet.close("back")
elseif inputPass ~= pass then
print(" Password incorrect.. ")
sleep(sleepTime3)
term.clear()
term.setCursorPos(1,1)
print(" Computer Shutting down.. ")
sleep(sleepTime2)
end
end

function UserIncorrect()
if inputUser ~= user then
print(" Username incorrect.. ")
sleep(sleepTime3)
term.clear()
term.setCursorPos(1,1)
print(" Computer Shutting down.. ")
sleep(sleepTime2)
end
end

function BackArea()
if inputUser == backarea then
print(" Opening Door! ")
print(" Please Wait ")
sleep(sleepTime3)
term.clear()
term.setCursorPos(1,1)
print(" Door Open ")
sleep(sleepTime1)
rs.setOutput("left", true)
sleep(sleepTime2)
rs.setOutput("left", false)
print(" Door Closed. Thanks you! ")
print(sleepTime2)
end
end

function AccessToAllRooms()
if inputUser == allRooms then
rednet.open("back")
rednet.send(5,"OpenDoor")
rednet.send(7,"OpenDoor")
print(" Message Sent ")
rednet.close("back")
end
end



sleepTime1 = 3
sleepTime2 = 2
sleepTime3 = 3
sleepTime4 = 1
sleepTimeTest = 5
user = "jamesscape2"
pass = "basketball"
backarea = "OpenDoor"
allRooms = "AuthOnly"

term.clear()
term.setCursorPos(1,1)

print(" ———————————")
print(" - Welcome to the Master Control -")
print(" ———————————")

sleep(sleepTime1)

write(" Enter username: ")
inputUser = read("*")
if inputUser == user then
UserCorrect()
elseif
inputUser == backarea then
BackArea()
elseif
inputUser == allRooms then
AccessToAllRooms()
else
UserIncorrect()
end
sleep(5) – set to 20 later on
term.clear()
os.reboot()
This the code for the room which i want the master computer to open

Spoilerfunction UserCorrect()
print(" Username correct ")
sleep(sleepTime2)
write(" Enter password: ")
inputPass = read("*")
if inputPass == pass then
print(" Password Correct ")
sleep(sleepTime2)
rs.setOutput("left", true)
print(" Welcome Enter ")
sleep(sleepTime3)
rs.setOutput("left", false)
elseif inputPass ~= pass then
print(" Password incorrect.. ")
sleep(sleepTime3)
term.clear()
term.setCursorPos(1,1)
print(" Computer Shutting down.. ")
sleep(sleepTime2)
os.shutdown()
end
end

function UserIncorrect()
if inputUser ~= user then
print(" Username incorrect.. ")
sleep(sleepTime3)
term.clear()
term.setCursorPos(1,1)
print(" Computer Shutting down.. ")
sleep(sleepTime2)
os.shutdown()
end
end

function OpenDoor()
x,y,z=rednet.receive()
if y=="OpenDoor" then
rs.setOutput("left", true)
sleep(3)
rs.setOutput("left", false)
end
end

function Opening()
if inputUser == user then
UserCorrect()
else
UserIncorrect()
end
end



sleepTime1 = 3
sleepTime2 = 2
sleepTime3 = 3
sleepTimeTest = 5
user = "Override"
pass = "Override"

term.clear()
term.setCursorPos(1,1)



print(" ——————————- ")
print(" -Welcome to the Override Room-")
print(" ——————————- ")

sleep(sleepTime1)
rednet.open("right")
write(" Enter username: ")
inputUser = read("*")
Opening()
parallel.waitForAny(OpenDoor(), Opening())
os.reboot()


i know there is a problem but i dont know how to fix it. but any modification u make will help

Please HELP

thanks
Lyqyd #2
Posted 11 May 2012 - 01:17 AM
What's the problem you're having, exactly?
PixelToast #3
Posted 11 May 2012 - 03:16 AM
he wants to be able to allow the user to be in a menu to control the redstone output while having a master computer be able to change every computers output at once, im reviewing the code now to see how to implement this
Lyqyd #4
Posted 11 May 2012 - 05:38 AM
No, I can read just fine. I understand that's what he wants it to do. However, he didn't provide any information on what errors he's received, or what part of the code seems to be performing unexpectedly.
PixelToast #5
Posted 11 May 2012 - 07:32 AM
not an error, just wants coding advice to help him use the parallel api, right now i havent been succesfull in implementing it into his code, ill work on it tomaro its getting late
jamesscape2 #6
Posted 11 May 2012 - 11:22 AM
i think i hav fixed it this is what i have come up With and it works at the moment.

Spoilerfunction UserCorrect()
print(" Username correct ")
sleep(sleepTime2)
write(" Enter password: ")
inputPass = read("*")
if inputPass == pass then
print(" Password Correct ")
sleep(sleepTime2)
print(" Door Opening ")
rs.setOutput("left", true)
sleep(sleepTime3)
print(" Door Closing ")
rs.setOutput("left", false)
elseif inputPass ~= pass then
print(" Password incorrect.. ")
sleep(sleepTime3)
term.clear()
term.setCursorPos(1,1)
print(" Computer Shutting down.. ")
sleep(sleepTime2)

end
end

function UserIncorrect()
if inputUser ~= user then
print(" Username incorrect.. ")
sleep(sleepTime3)
term.clear()
term.setCursorPos(1,1)
print(" Computer Shutting down.. ")
sleep(sleepTime2)

end
end

function OpenDoor()
x,y,z=rednet.receive()

if y=="OpenDoor" then
term.clear()
term.setCursorPos(1,1)
print(" Opening in 5.. ")
sleep(1)
print(" Opening in 4.. ")
sleep(1)
print(" Opening in 3.. ")
sleep(1)
print(" Opening in 2.. ")
sleep(1)
print(" Opening in 1.. ")
sleep(1)
rs.setOutput("left", true)
print(" Door Open ")
sleep(3)
term.clear()
term.setCursorPos(1,1)
print(" Door Closing in 3.. ")
sleep(1)
print(" Door Closing in 2.. ")
sleep(1)
print(" Door Closing in 1.. ")
sleep(1)
rs.setOutput("left", false)
sleep(1)
end
os.reboot()
end

function Opening()
write(" Enter username: ")
inputUser = read("*")
if inputUser == user then
UserCorrect()
else
UserIncorrect()
end
os.reboot()
end

function Title()

print(" ——————————– ")
print(" - Welcome to the Override Room -")
print(" ——————————– ")

end



sleepTime1 = 3
sleepTime2 = 2
sleepTime3 = 3
sleepTimeTest = 5
user = "Override"
pass = "Override"
i = 1

term.clear()
term.setCursorPos(1,1)



Title()

rednet.open("right")

parallel.waitForAny(Opening, OpenDoor)






–sleep(sleepTime1)
–rednet.open("right")
–write(" Enter username: ")
–inputUser = read("*")
– Opening()

–os.reboot()

Spoiler
Spoilerwhat i did was create this into a function called Opening

write(" Enter username: ")
inputUser = read("*")
if inputUser == user then
UserCorrect()
else
UserIncorrect()
end
os.reboot()

and then just delete that section from here


sleep(sleepTime1)
rednet.open("right")
write(" Enter username: ")
inputUser = read("*")
Opening()
parallel.waitForAny(OpenDoor(), Opening())
os.reboot()



and just placed the

parallel.waitForAny(OpenDoor(), Opening()) in the code and changed which function is first to

parallel.waitForAny(Opening(), OpenDoor())


and that seams to be working hope u understand

thanks