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

Using multiple os.pullEvent("monitor_touch")

Started by Joe m, 21 September 2016 - 06:08 PM
Joe m #1
Posted 21 September 2016 - 08:08 PM
I'm having a problem with my SOMAR styled airlock for my underwater base.

It's supposed to flood the chamber with water (via buildcraft floodgates activated by redstone gates in the valve pipe) and then flush it out (via buildcraft pumps activated by redstone turning on engines that power the pumps)
(its supposed to wait untill its clicked on. then its supposed to do the flushing. then wait untill clicked to do the flooding and so on)

The full code is
local mon = peripheral.wrap("top")
mon.setTextScale(1)
while true do
  mon.setTextColor(colors.lightBlue)
  mon.setCursorPos(1,2)
  mon.write("click with OmniTool to access")
  event, side, x, y = os.pullEvent("monitor_touch")
  if x > 1 and x < 29 and y == 2 then
	print("airlock activated")
	mon.clear()
	mon.setCursorPos(1,2)
	mon.write("flushing")
	redstone.setOutput("back", true)
	print("flushing")
	sleep(30)
	print("flushed)
	redstone.setOutput("back", false)
  end
  sleep(0)
  mon.clear()
  mon.setCursorPos(1,2)
  mon.setTextColor(colors.lightBlue)
  mon.write("Click with OmniTool to access")
  sleep(0)
  event, side, x, y = os.pullEvent("monitor_touch")
  if x > 1 and x < 29 and y == 2 then  --The program (for some reason) stops here when its supposed to continue through to the next bit--
	print("airlock deactivated")
	mon.clear()
	mon.setCursorPos(1,2)
	mon.setTextColor(colors.red)
	mon.write("flooding")
	redstone.setOutput("bottom", true)
	print("flooding")
	sleep(10)
	print("flooded")
	redstone.setOutput("bottom", false)
  end
end

I've tried putting "event2, side2, x2, y2 = os.pullEvent("monitor_touch")" instead of "event, side, x, y = os.pullEvent("monitor_touch")" but it still ignored the last section of code.
I've also tried putting the other "click with omnitool to access" on a line higher ( like "mon.setCursorPos (1,1)" instead of (1,2) and changed the "y == 2" to "y==1". but that still didn't fix it.

Please help as I have no idea why it isn't doing the second half of the code.
Bomb Bloke #2
Posted 22 September 2016 - 01:38 AM
I can't see anything that'd cause the script to stop, as such. Do you perhaps mean it repeats the flushing action over and over? That might happen if you fail to click on the exact area it wants you to. Or perhaps it has something to do with you clicking with an OmniTool?

Just for kicks, try it like this:

Spoiler
local mon = peripheral.wrap("top")
mon.setTextScale(1)
mon.setTextColor(colors.lightBlue)
local x, y = mon.getSize()

redstone.setOutput("back", false)
redstone.setOutput("bottom", false)

local filled = true

while true do
	mon.clear()
	mon.setCursorPos(math.floor((x - 15) / 2) + 1, math.floor((y - 1) / 2) + 1)  -- Centering.
	mon.write("Click to access")
	
	os.pullEvent("monitor_touch")  -- Let's just wait for any click to the display, without caring where.
	
	mon.clear()
	mon.setCursorPos(math.floor((x - 11) / 2) + 1, math.floor((y - 1) / 2) + 1)
	
	if filled then
		print("Airlock activated.")

		mon.write("Flushing...")
		print("Flushing...")
		
		redstone.setOutput("back", true)
		sleep(30)
		redstone.setOutput("back", false)
		
		print("Flushed.")
		
		filled = false
	else
		print("Airlock deactivated.")
		
		mon.setTextColor(colors.red)
		mon.write("Flooding...")
		print("Flooding...")
		mon.setTextColor(colors.lightBlue)
		
		redstone.setOutput("bottom", true)
		sleep(10)
		redstone.setOutput("bottom", false)
		
		print("Flooded.")
		
		filled = true
	end
end