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

Programm hangs up after os.pullEvent()

Started by Xande, 31 December 2014 - 05:49 PM
Xande #1
Posted 31 December 2014 - 06:49 PM
Hello there,

I am currently coding my first turtle mining programm, which scanns the environment and displays all
the given information about it(the blocks around the turtle) and the turtle(FuelLevel) out on a Monitor.

Now i am trying to make a programm, who allows the user to move the turtle when he
click on one of the printet Blocks.

The programm i made for that works fine, if i start it alone, but whenever i try to open it with
shell.run("ProgrammName")
the programm just hang up and i need to terminat it.

The code whit which i have trouble with:
Spoiler

--Programm Name movement
while true do
local event, side, x, y = os.pullEvent()
if event == "monitor_touch" then
  --Move Down
  if (x == 9 and y == 13) or (x == 10 and y == 13) then
   rednet.send(5, "Down")
   break
  end  
  --Move Right
  if (x == 15 and y == 8) or (x == 16 and y == 8) then
   rednet.send(5, "Right")
   break
  end
  --Move forward
  if(x == 9 and y == 8) or (x == 10 and y == 8) then
   rednet.send(5, "Forward")
   break
  end
  --Move Left
  if (x == 3 and y == 8) or (x == 4 and y == 8) then
   rednet.send(5, "Left")
   break
  end
  --Move UP
  if (x == 9 and y == 3) or (x == 10 and y == 3) then
   rednet.send(5, "Up")
   break
  end
  --Move Back
  if (x ==  8 and y == 17) or (x == 9 and y == 17) or (x == 10 and y == 17) or (x == 11 and y == 17) then
   rednet.send(5, "Back")
   break
  end  
end
end


an here the code i use to run the code above:
Spoiler

--Programm Name Viewer
-- #4 Cpu // Turtel #5
rednet.open("back")
monitor = peripheral.wrap("left")
term.setBackgroundColor(32)
term.clear()
term.setCursorPos(1,1)
monitor.setTextScale(1)
while rednet.isOpen("back") == true do

  local ID, MSG, PORT = rednet.receive()

  if MSG == "Detect" then
	shell.run("draw")
  end
  if MSG == "FuelLevel" then
	shell.run("getTank")
  end
  if MSG == "End" then
	rednet.close("back")
  end
  if MSG == "Move" then
	shell.run("movement")
  end

end

The Problem i have, is that it seems like the code hang up at

local event, side, x, y = os.pullEvent()

And if it is helpful here's the main "Turtelcode" which is used to call the different functions:
Spoiler

--Programm Name TView
rednet.open("right")
--Functions
function mMovement()
  rednet.send(4,"Move")
  shell.run("mMovement")
end
function getFuelLevel()
  rednet.send(4, "FuelLevel")
  local TANK = turtle.getFuelLevel()
  if TANK == 0 then
	shell.run("refuel")
  else
	rednet.send(4, TANK)
  end  
end
function detect()
  rednet.send(4, "Detect")
  --detect top
  shell.run("detectt")
  --detect Left
  turtle.turnLeft()
  shell.run("detectf")
  --detect forward
  turtle.turnRight()
  shell.run("detectf")
  --detect Right
  turtle.turnRight()
  shell.run("detectf")
  --turn normal
  turtle.turnLeft()
  -- detect bottom
  shell.run("detectb")

end
--Main
  local bRunning = true

  while bRunning == true do

	detect()
	getFuelLevel()
	mMovement()
	bRunning = false
  
  end

rednet.send(4, "End")	  

And the code which actually moves the turtle:
Spoiler

--Programm Name mMovement
local ID, MSG, PORT = rednet.receive()
if MSG == "Forward" then
  if turtle.detect() == true then
	turtle.dig()
	turtle.forward()
  else
	turtle.forward()
  end
end
if MSG == "Back" then
  turtle.turnRight()
  turtle.turnRight()
  if turtle.detect() == true then
	turtle.dig()
	turtle.forward()
  else
	turtle.forward()
  end
end
if MSG == "Left" then
  turtle.turnLeft()
  if turtle.detect() == true then
	turtle.dig()
	turtle.forward()
  else
	turtle.forward()
  end
end
if MSG == "Right" then
  turtle.turnRight()
  if turtle.detect() == true then
	turtle.dig()
	turtle.forward()
  else
	turtle.forward()
  end
end
if MSG == "Up" then
  if turtle.detectUp() == true then
	turtle.digUp()
	turtle.up()
  else
	turtle.up()
  end
end
if MSG == "Down" then
  if turtle.detectDown() == true then
	turtle.digDown()
	turtle.down()
  else
	turtle.down()
  end
end

I'll already stuck at this problem since 5 hours now and still haven't found the reason why it behave like this especially because it does work, when i start the programm(movement) separately.

Thanks in advance
NanoBob #2
Posted 31 December 2014 - 07:54 PM
Well the thing is that os.pullEvent always waits for an event to be pulled. So it will not continue, it hanging up at that point is intetional.
Xande #3
Posted 31 December 2014 - 08:06 PM
I do know, that os.pullEvent() waits for an event and in this case the event is "monitor_touch", the problem is,
that if i "touch" the monitor, the event does not get triggered.

For example, if i touch the screen at some point ( x = 9 and y = 13 for example) the code should continue with

rednet.send(5, "Down")
   break

but it does not continue.
Lyqyd #4
Posted 31 December 2014 - 09:12 PM
Just to confirm, you are using an advanced monitor, and right-clicking within the appropriate area on the screen?
NanoBob #5
Posted 31 December 2014 - 10:00 PM
What you could try to do when debugging it, whenever the event gets pulled print the x and y position so you're sure it got triggered.
Xande #6
Posted 31 December 2014 - 10:10 PM
Yes, i do use a advanced monitor as seen in the picture below
Spoiler

And i am also right-clicking in the in the approptiate area.

Could this have to do something with the fact, that i am using CC 1.5?
Bomb Bloke #7
Posted 31 December 2014 - 11:55 PM
Just to confirm, you are using an advanced monitor, and right-clicking within the appropriate area on the screen?

He's saying that the click-detection code is in its own script. Running that directly works, but executing it via shell.run() inside the main script apparently does not.

My guess is that it's either hanging up in the "draw" or "getTank" scripts, before it ever gets around to starting the "movement" script.
Xande #8
Posted 01 January 2015 - 01:10 AM
Just to confirm, you are using an advanced monitor, and right-clicking within the appropriate area on the screen?

He's saying that the click-detection code is in its own script. Running that directly works, but executing it via shell.run() inside the main script apparently does not.

My guess is that it's either hanging up in the "draw" or "getTank" scripts, before it ever gets around to starting the "movement" script.

I teste it before, because i already had suspicion like that but the programm stopped exactly at

local event, side, x, y = os.pullEvent()

This means that i was able to run the programm "movement".
Bomb Bloke #9
Posted 01 January 2015 - 01:53 AM
Tested how? By placing a print statement directly above that line, or…?
Xande #10
Posted 01 January 2015 - 12:22 PM
I tested it by adding to print statements as following in the code

print("movement started")
local event, side, x, y = os.pullEvent()
if event == "monitor_touch" then
  print("Touch detected at"..x.." X and "..y.." Y")

Whenever i started the program over shell.run() it just printed out "movement started" but does not printed out
"Touch detect at ( x- pos) X and (y - pos) Y" when i am touching the sceen.
Edited on 01 January 2015 - 11:41 AM
Bomb Bloke #11
Posted 01 January 2015 - 01:16 PM
What if you rig things to print whenever any event occurs, then try eg typing?
Xande #12
Posted 01 January 2015 - 02:13 PM
Thanks a lot,

i was able to find the problem.

I had to add

local EVENT = os.pullEvent()

into Main "Viewer" program.