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

Interrupts with Redstone Signals

Started by AtroCty, 09 October 2013 - 12:34 PM
AtroCty #1
Posted 09 October 2013 - 02:34 PM
Title: Interrupts with Redstone Signals

Hey guys,

I have a rather simple question for you:
I want to make a quiz-program, which will starts upon a redstone signal, and lasts only as long the red-stone signal is applied.
If the redstone signal is gone, programm gets a reset and wait until the singal is applied again.

How can I do that?
I know that you can wait for an Event, but then I can't wait for a secound event, which allow me the specify if the player made the correct answer ?

Best regards
AtroCty
hpnisse #2
Posted 09 October 2013 - 04:20 PM
I have an idea, but I don't know if it works!


function waitFor()
event, p1, p2, p3 = os.pullEvent()
  if event == "redstone" then
   'FUNCTION when redstone changed'
  elseif event == "monitor_touch" then -- Or any other event that you choose
   'FUNCTION when someone touched the monitor or what ever you want accually'
  end
end

Link to events that you can use!
It will trigger the
Just do the following functions and then it will work I think!
electrodude512 #3
Posted 09 October 2013 - 04:41 PM
I have an idea, but I don't know if it works!


function waitFor()
event, p1, p2, p3 = os.pullEvent()
  if event == "redstone" then
   'FUNCTION when redstone changed'
  elseif event == "monitor_touch" then -- Or any other event that you choose
   'FUNCTION when someone touched the monitor or what ever you want accually'
  end
end

Link to events that you can use!
It will trigger the
Just do the following functions and then it will work I think!

That will work (except for the fact that you don't start comments with ' in lua), but I would recommend the following because it allows you to easily add more events that have different amounts of parameters and because it makes event local. The main difference is that my method stuffs all the parameters to your event into a table which you can index instead of only taking a fixed number of, and possibly not enough, return values.

function eventHandler()
	local event = {os.pullEventRaw()}
	if event[1] == "redstone" then
		print("Redstone changed")
	elseif event[1] == "monitor_touch" then	-- Or any other event that you choose
		print("You touched the monitor at x=" .. tonumber(event[3]) .. ", y=" .. tonumber(event[4]))
	elseif event[1] == "terminate" then
		print("Bye!")
		return
	end
end
AtroCty #4
Posted 10 October 2013 - 11:04 AM
So there is no possibility to trigger global events ?
Like this

function mymain()
...code
...
pullEvent() -- Stop all code when Redstone Signal is on and do this (jump to line)
pullEvent() -- Stop all code when Redstone Signal is off and do this (jump to line)

Just like a classic interrupt in microcontrollers.
Bomb Bloke #5
Posted 10 October 2013 - 11:24 AM
No, events won't automatically interupt your code and force it to go off and do something else.

To be clear, though, contrary to your first post you CAN wait for a secondary event while waiting for user input, and the two examples above outline how it's done.

Edit: Well, you could also do it by running functions in parallel, and having one wait for changes in the redstone signal while the other does everything else. Although it'd be closer to what I think you're thinking of, I only mention this for completeness, as the above solutions are likely better.
PixelToast #6
Posted 10 October 2013 - 11:36 AM

while not rs.getInput("back") do
  os.pullEvent("redstone")
end
parallel.waitForAny(function()
  while rs.getInput("back") do
    os.pullEvent("redstone")
  end
  error("times up!",0)
end,function()
  -- put your quiz here
end)
AtroCty #7
Posted 10 October 2013 - 01:57 PM

while not rs.getInput("back") do
  os.pullEvent("redstone")
end
parallel.waitForAny(function()
  while rs.getInput("back") do
	os.pullEvent("redstone")
  end
  error("times up!",0)
end,function()
  -- put your quiz here
end)

I had something like this at beginning, problem here, that it terminates the programm and won't re-initialize it, even though you put it in the startup file.
PixelToast #8
Posted 10 October 2013 - 03:46 PM
while true do
  while not rs.getInput("back") do
    os.pullEvent("redstone")
  end
  parallel.waitForAny(function()
    while rs.getInput("back") do
      os.pullEvent("redstone")
    end
  end,function()
    -- put your quiz here
  end)
end
AtroCty #9
Posted 10 October 2013 - 04:58 PM
while true do
  while not rs.getInput("back") do
	os.pullEvent("redstone")
  end
  parallel.waitForAny(function()
	while rs.getInput("back") do
	  os.pullEvent("redstone")
	end
  end,function()
	-- put your quiz here
  end)
end

Feeling realy stupid. Thanks mate!