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

Wait for a redstone signal, while still able to accept user input

Started by CCJJSax, 15 December 2012 - 10:03 AM
CCJJSax #1
Posted 15 December 2012 - 11:03 AM
I am pretty new to Lua, almost everything I know is from learning in the past week. Right now I have a system that basically manages 3 outputs. It's probably pretty basic for a lot of you. but I am having 2 problems, and one thing that I can't seem to be able to find on the internet. I'll put the 2 problems I'm having if you want to try and figure out what's going on, but I will keep this post about the other thing. I'll put the problems in orange in the code.

Basically what happens is whenever something fills up, it lets off a redstone signal. I want that signal to lead into the computer and then display that it's full and not allow you to type in certain commands. But the only way that I know to get a redstone input is on a loop and restricts users from typing anything or interacting with the computer.

this is the main code. It's called "visproduction"
term.write("What would you like to do?")
print ("")

local input = read()

if input == "help" then
term.clear()
term.setCursorPos(1, 1)
print ("Options are as follows:")
print ("")
print ("'Start' or 'Start vis production'")
print ("'Stop' or 'Stop vis production'")
print ("'Boost' or 'Boost vis production'")
print ("'Clear'")
print ("'estop'")
print ("'exit'")
print ("'shut down'")
– print ("'check vis production status'") –this is how I want to check to see how it's running.
sleep(1)
shell.run("visproduction")
end

if input == "start" then
rs.setBundledOutput("back",1)
sleep(1)
shell.run("visproduction")
end

if input == "Start" then
rs.setBundledOutput("back",1)
sleep(1)
shell.run("visproduction")
end

if input == "start vis production" then
rs.setBundledOutput("back",1)
sleep(1)
shell.run("visproduction")
end

if input == "Start vis production" then
rs.setBundledOutput("back",1)
sleep(1)
shell.run("visproduction")
end


if input == "stop" then
rs.setBundledOutput("back",0)
sleep(1)
shell.run("visproduction")
end

if input == "Stop" then
rs.setBundledOutput("back",0)
sleep(1)
shell.run("visproduction")
end

if input == "stop vis production" then
rs.setBundledOutput("back",0)
sleep(1)
shell.run("visproduction")
end

if input == "Stop vis production" then
rs.setBundledOutput("back",0)
sleep(1)
shell.run("visproduction")
end

if input == "boost" then
rs.setBundledOutput("back",3)
sleep(1)
rs.setBundledOutput("back",1)
shell.run("visproduction")
end

if input == "Boost" then
rs.setBundledOutput("back",3)
sleep(1)
rs.setBundledOutput("back",1)
shell.run("visproduction")
end

if input == "boost vis production" then
rs.setBundledOutput("back",3)
sleep(1)
rs.setBundledOutput("back",1)
shell.run("visproduction")
end

if input == "Boost vis production" then
rs.setBundledOutput("back",3)
sleep(1)
rs.setBundledOutput("back",1)
shell.run("visproduction")
end

if input == "estop" then
rs.setBundledOutput("back",4)
shell.run("visproduction")
end

if input == "Estop" then
rs.setBundledOutput("back",4)
shell.run("visproduction")
end

if input == "clear" then
term.clear()
term.setCursorPos(1, 1)
shell.run("visproduction")
end

if input == "edit" then
print("Only authorized users can edit this program. Password Required")
sleep(2)
shell.run("editpass")
end

–this part comes back as the else code, and doesn't do shell.exit()
if input == "exit" then
shell.exit()
end


if input == "shut down" then
print("Shutting down Production OS. Please wait as it saves and safely shuts down the system.")
sleep(3)
print("Saving settings")
sleep(1)
print("Shutting down secondary processes")
sleep(0.5)
print("Shutting Down")
sleep(4)
os.shutdown()

else
print("not a valid operation")
shell.run("visproduction")
end


and here is the "editpass" program code.

correctpass = "Secret1"

write("Insert password here: ")

pass = read()

if pass == (correctpass) then
print("Correct, You may now edit this program.")
sleep(1)
print("saving content to prevent data loss")
sleep(1)
print("Shutting down secondary processes")
sleep(0.5)
print("Restarting. You will be in the editing screen upon startup")
sleep(4)
shell.run("edit","visproduction") –when I'm in the edit screen, then exit with ctrl E, it's like it runs visproduction, and then I –typed "e" as a code, so it returns as another else.


else
print("Incorrect")
sleep(1)
shell.run("visproduction")
end

This is my first forum post so I hope it's formatted correctly and not too much. Thanks for your help in advance.
remiX #2
Posted 15 December 2012 - 12:31 PM
With every if statement for a different choice use elseif. like this:

if input == "help" then
    print("blah help blah")
elseif input == "exit" then
    print("Bye")
    os.shutdown()
else
    print("Invalid option")
end

It's pretty self explanatory, but all your if's are not the way to do it.
Do that and then post if you still need help :)/>

PS: Use code tags :)/> [*code]Your code here[*/code] (Without the *)
Dlcruz129 #3
Posted 15 December 2012 - 12:57 PM
Put the redstone signal code into a function, and the input code in a function. Then use parallel.waitForAny(func1,func2)
Sammich Lord #4
Posted 15 December 2012 - 12:59 PM
Formatting your code and using CODE tags helps us all out a lot.
CCJJSax #5
Posted 15 December 2012 - 02:52 PM
With every if statement for a different choice use elseif. like this:

if input == "help" then
	print("blah help blah")
elseif input == "exit" then
	print("Bye")
	os.shutdown()
else
	print("Invalid option")
end

It's pretty self explanatory, but all your if's are not the way to do it.
Do that and then post if you still need help :)/>

PS: Use code tags :)/> [*code]Your code here[*/code] (Without the *)

Thanks. Though these ifs after ifs are functional, the system I am using is working, but I have been contemplating changing to the else ifs.

I don't want my whole system to shut down, I just want to exit the program to the directory (I think that's what it is called, if not, then it's the default boot screen)

And thanks for the info on using code tags! :D/> this was my first post so I was unaware of that function.
CCJJSax #6
Posted 15 December 2012 - 02:53 PM
Formatting your code and using CODE tags helps us all out a lot.

Thanks :D/> this was my first post and I didn't know, so learning about this stuff will help out.
theoriginalbit #7
Posted 15 December 2012 - 03:22 PM
I don't want my whole system to shut down, I just want to exit the program to the directory (I think that's what it is called, if not, then it's the default boot screen)

in that case replace os.shutdown() with error() and it will exit your program to the terminal :)/>
CCJJSax #8
Posted 15 December 2012 - 06:00 PM
I don't want my whole system to shut down, I just want to exit the program to the directory (I think that's what it is called, if not, then it's the default boot screen)

in that case replace os.shutdown() with error() and it will exit your program to the terminal :)/>

Thanks for the help, for some reason when I switched to an elseif system, shell.exit() worked. Thanks for the suggestion!