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

Help! turtle wont stop going down D:

Started by Sparky456, 23 May 2016 - 08:57 AM
Sparky456 #1
Posted 23 May 2016 - 10:57 AM
So i'm making a program to get a turtle to return to a position but it gets stuck going up/down and goes past the Y cord is there a way to stop this?


local f=fs.open("/data/homepos", "r")
local tbl=textutils.unserialise(f.readAll())
f.close()
rednet.open("right")
local curx, cury, curz=gps.locate()
local distx=curx-tbl.x
local disty=cury-tbl.y
local distz=curz-tbl.z
local distsum=distx+distz+disty
local absdist=math.abs(distsum)
if (math.abs(distsum)) == 0 then
  rednet.send(10,"Already Home")
end
if (math.abs(distsum)) ~= 0 then
  rednet.send(10,"not home","I am now returning home!")
    y = cury-tbl.y
	  if y ~= 0 then
		   if y < 0 then
			 print("I'm too far down")
		   end 
		   while y < 0 do
			   if turtle.detectUp() == true then
				 turtle.digUp()
				 turtle.up()
			   elseif turtle.detectUp() == false then
				 turtle.up()  
			 end
		   if y > 0 then
			 print("I'm too far up")
		   end 
		   while y >0 do
			   if turtle.detectDown() == true then
				 turtle.digDown()
				 turtle.down()
			   elseif turtle.detectDown() == false then
				 turtle.down()
	    end	  
	  if cury-tbl.y == 0 then
	    print("I'm at the right hight")
	    sleep(1)
	  end
	  end	   
    end
  end
end
Waitdev_ #2
Posted 23 May 2016 - 12:15 PM
Just a tip: If a turtle keeps going down but you can't stop it, hold ctrl+t for a few seconds. That will stop the program.

For the code though, I'm not sure about that. Sorry.
KingofGamesYami #3
Posted 23 May 2016 - 02:13 PM
It may help if you changed your 'y' value when you moved up or down.
Sparky456 #4
Posted 23 May 2016 - 03:29 PM
It may help if you changed your 'y' value when you moved up or down.

ok so I've added that but now it doesn't move D:

local f=fs.open("/data/homepos", "r")
local tbl=textutils.unserialise(f.readAll())
f.close()
rednet.open("right")
local curx, cury, curz=gps.locate()
local distx=curx-tbl.x
local disty=cury-tbl.y
local distz=curz-tbl.z
local distsum=distx+distz+disty
local absdist=math.abs(distsum)
if (math.abs(distsum)) == 0 then
  rednet.send(10,"Already Home")
end
if (math.abs(distsum)) ~= 0 then
  rednet.send(10,"not home","I am now returning home!")
end
local y = cury-tbl.y
if y ~= 0 then
if y < 0 then
  print("I'm too far down")
end
while y < 0 do
  if turtle.detectUp() == true then
	turtle.digUp()
	turtle.up()
	y = y + 1
	sleep(0.3)
  end
end
while y < 0 do
  if turtle.detectUp() == false then
	turtle.up()
	y = y + 1
	sleep(0.3)
  end
end
if y > 0 then
  print("I'm too far up")
end
while y > 0 do
  if turtle.detectDown() == true then
	turtle.digDown()
	turtle.Down()
	y = y - 1
	sleep(0.3)
  end
end
while y > 0 do
  if turtle.detectDown() == false then
	turtle.Down()
	y = y - 1
	sleep(0.3)
  end
end  
if y == 0 then
  print("I'm at the right hight")
  sleep(1)
end

Ok so I'm an idiot I've put a capital for down :(/>
Edited on 23 May 2016 - 01:33 PM
Sparky456 #5
Posted 23 May 2016 - 05:57 PM
how do i end the loop at line 110?

local f=fs.open("/data/homepos", "r")
local tbl=textutils.unserialise(f.readAll())
f.close()
rednet.open("right")
local curx, cury, curz=gps.locate()
local distx=curx-tbl.x
local disty=cury-tbl.y
local distz=curz-tbl.z
local distsum=distx+distz+disty
local absdist=math.abs(distsum)
if (math.abs(distsum)) == 0 then
  rednet.send(10,"Already Home")
end
if (math.abs(distsum)) ~= 0 then
  rednet.send(10,"not home","I am now returning home!")
end
local y = cury-tbl.y
if y ~= 0 then
if y < 0 then
  print("I'm too far down") 
end
while y < 0 do
  if turtle.detectUp() == true then
    turtle.digUp()
    turtle.up()
    y = y + 1
    sleep(0.3)
else
    turtle.up()
    y = y + 1
    sleep(0.3) 
  end
end
if y > 0 then
  print("I'm too far up")
end 
while y > 0 do
  if turtle.detectDown() == true then
    turtle.digDown()
    turtle.down()
    y = y - 1
    sleep(0.3)
  else
    turtle.down()
    y = y - 1
    sleep(0.3)
  end
end  
if y == 0 then
  print("I'm at the right hight")
  sleep(1)
end
end
local z = curz-tbl.z
local oz = curz-tbl.z
if z ~= 0 then
  if z < 0 then
    print("I'm too far North") 
  end
  if z < 0 then
    if turtle.detect() == true then
	  turtle.dig()
   turtle.forward()
    else
	  turtle.forward()
    end
  end
  local cx, cy, cz=gps.locate()
    if cz-tbl.z < oz then
   turtle.turnRight()
   turtle.turnRight()
else
	  sleep(0.3)
    end
  print("I'm Facing the right way")
end
local bz = 0
local ax, ay, az=gps.locate()
  if az-tbl.z+3 < 0 then
    while az-tbl.z+3 < 0 do
	  if turtle.detect() == true then
	    turtle.dig()
	 turtle.forward()
	    az = az + 1
	  else
	    turtle.forward()
	    az = az + 1
	  end
    end
   if turtle.detect() == true then
	 while true do
	   turtle.turnRight()
	   turtle.forward()
	   turtle.turnLeft()
	   if turtle.detect() == false then
		   turtle.forward()
		   sleep(0.1)
		   turtle.forward()
		   turtle.turnLeft()
		 end
	   end
   else
	 turtle.forward()
	 sleep(0.1)
	 turtle.forward()
	 turtle.turnLeft()
   end
  print("I'm in postion for tube entry!")
end
Dragon53535 #6
Posted 23 May 2016 - 06:29 PM
I'm assuming you mean, do until I want it to stop, in which case you kind of have to know what you want to do to make it stop. You can set a boolean to true, and set it to false when input is received. That requires a slightly different setup for the loop. You could just have a counter that counts up, and when it reaches a certain number, exit the loop using the break keyword. But without entirely knowing what you want to do exactly it's hard to fully help.

I'm only of course assuming this because I assume you've got all your ends correctly in place.