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

Redstone input to interrupt loop

Started by ocehtamote, 29 May 2014 - 05:11 AM
ocehtamote #1
Posted 29 May 2014 - 07:11 AM
I am trying to make a nuclear reactor terminal. I have the menu working and the inputs working. I also have the bundled outputs working. What I need help with is I need a what that when the computer receives an input from the red bundled cable to stop all outputs except on the white cable. Then when the red cable no longer is receiving a signal to continue with the program.
below is the current program I have written the current rs.testbundledinput works but only if there is a signal once the machine has been rebooted.

while true do


if rs.testBundledInput("back", colors.red) then
term.clear()
term.setCursorPos(1,1)
print("Reactor Temperature Alarm Active")
print("Performing Emergency Shutdown")
rs.setBundledOutput("back", 0)
rs.setBundledOutput("back", colors.white)
sleep(5)

else


term.clear()
term.setCursorPos(1,1)
print("Nuclear Reactor Control Terminal")
print("Please Input Numerical Command")
print("1. Reactor Startup")
print("2. Reactor Shutdown")
print("3. Alarm Test")

input = read()
sleep(0.2)



if input == "1" then
term.clear()
term.setCursorPos(1,1)
print("Reactor Startup Sequence Initiating")
sleep(3)
print("Performing Reactor Alarm Function Test")
sleep(1)
rs.setBundledOutput("back", colors.white)
sleep(4.5)
print("Performing Chamber Lockout")
sleep(1)
rs.setBundledOutput("back", colors.black)
sleep(3)
print("Activating Reactor Coolant Flow")
sleep(1)
rs.setBundledOutput("back", colors.blue+colors.black)
sleep(3)
print("Performing Reactor Startup")
sleep(1)
rs.setBundledOutput("back", colors.blue+colors.black+colors.green)
sleep(3)
print("Reactor Startup Sequence Complete")
sleep(3)
print("Returning To Reactor Control Menu")
sleep(3)

elseif input == "2" then
term.clear()
term.setCursorPos(1,1)
print("Performing Reactor Shutdown Sequence")
sleep(3)
print("Performing Reactor Shutdown")
sleep(1)
rs.setBundledOutput("back", colors.black+colors.blue)
sleep(3)
print("Deactivating Reactor Coolant Flow")
sleep(1)
rs.setBundledOutput("back", colors.black)
sleep(3)
print("Deactivating Reactor Chamber Lockout")
sleep(1)
rs.setBundledOutput("back", 0)
sleep(3)
print("Reactor Shutdown Sequence Complete")
sleep(3)
print("Returning To Reactor Control Menu")
sleep(5)

elseif input == "3" then
term.clear()
term.setCursorPos(1,1)
print("Alarm Will Sound Three Times")
sleep(2)
rs.setBundledOutput("back", colors.white)
sleep(5.5)
rs.setBundledOutput("back", 0)
print("Alarm Function Test Complete")
sleep(3)
print("Returning To Reactor Control Menu")
sleep(5)

elseif input == "t" then
term.clear()
term.setCursorPos(1,1)
print("Entering Debug Mode")
sleep(3)
term.clear()
term.setCursorPos(1,1)
break

else
print("That Is Not A Valid Input")
sleep(2)
end
end
end
CCJJSax #2
Posted 29 May 2014 - 07:34 AM
please use code tags. it makes it easier to read.

[code]
your code here
[/code]

I would suggest using os.pullEvent() for this. so you don't have to hit enter every time you enter a command. also whenever a redstone input changes, it will look through the loop again ( this includes bundled redstone.


while true do
  evt, p1, p2 = os.pullEvent()
  if colors.test(rs.getBundledInput("back"), colors.red)) then
    print("I'm going to do something")
  elseif p1 == "t" then
    print("you entered t")
  elseif p1 == "1" then
    print("you entered 1")
  end
end
CreeperGoBoom #3
Posted 29 May 2014 - 08:43 AM
i have something which is much more efficient and dynamic should you need to expand your code

instead of defining a new set of variables for each os.pullEvent event then trying to define more down the track, you can use this instead


event={os.pullEvent()} --places all events into a table
if event[1]=="modem_message" then --an example, event[1] is now always the event name
  if event[5]=="command" then
	--event[5] is now the message received from modem
  elseif event[1]=="redstone" then
  --see the ease and dynamics of focusing on a few events with much fewer lines?
  end
end

try out the below code, see if this method works for you
the major advantage of this method is that you can focus on MANY different events at once with ease and stability
works as a program, which will also help you determine what events are being triggered by what and what variables are being passed
try it with anything…
*key presses
*place a terminal glasses bridge on any side, grab a pair of terminal glasses and right click the bridge with the glasses, now in chat type something like:$$this or $$that, then see what comes up
*redstone pulses (only returns event name)
do anything that may trigger an event, everything will come up on screen

while true do
  event={os.pullEvent()}
  for i = 1,6 do -- most event have 3 to 5 vars, remembering the first var is always the name
	if event[i]~=nil then
	  print(event[i].." is var "..tostring(i))
	end
  end
end

here is the pastebin for the code:

http://pastebin.com/bRep48jC

for your interest, the above pastebin will never expire, happy event finding :D/>
Edited on 29 May 2014 - 07:00 AM
ocehtamote #4
Posted 29 May 2014 - 05:04 PM
I am sorry about the code tag that was my first post. Also this is my first program I have made so I'm not very good yet. is there a way you can explain how to implement the os.pullEvent into my code?
Thanks for the quick response to my first post.
CCJJSax #5
Posted 29 May 2014 - 07:54 PM
No problem. I remember when I was just a toddler in my coding days. I did the same thing on my first post here too. I'm going to add something in the code to make it auto refresh in case you have a way of automatically detecting if your reactor is getting out of hand. finally I did a little bit of cleanup for you, but it's still dirty.

http://pastebin.com/Cg5FuLa3

I really don't like how the forums here handles tabs. So I just ended up putting it on pastebin. It will expire in 2 weeks.
Edited on 29 May 2014 - 06:03 PM
ocehtamote #6
Posted 30 May 2014 - 02:15 PM
I just wanted to say thanks for putting it in the code for me. I ran the program and it recognizes when it gets a signal perfrctly but it doesnt recognize when the signal is off is there a way to fix that ?