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

Turtle going in a circle but should not

Started by TyCamden, 10 October 2012 - 11:56 AM
TyCamden #1
Posted 10 October 2012 - 01:56 PM
My farming program is almost done. It loads seeds, measures a rectangular room, and begins farming it.

It is supposed to:
farm wheat and move forward until the column is done
move to the next column to the right

farm wheat and move forward (back towards the initial row) until the column is done
move to the next column to the right
repeat until done

What it does,,,
farm wheat and move forward until the column is done
move to the next column to the right
farm wheat and move forward (back towards the initial row) until the column is done
moves BACK to the FIRST COLUMN
repeats

I added some print commands so that when I "T"erminated the turtle I could sort of see what it is thinking.

It appears not to be changing the Facing variable properly, perhaps in the function changeDirection() ???

Help to fix would be much appreciated. I have tried and tried… Thanks !


--[[ make sure the following slots
have correct items
slots 1-12 clear
slot 13 has 64 coal/charcoal
slot 14 has 1 seed
slot 15 has 1 wheat
slot 16 has 1 cobblestone slab
--]]
function clearexcessseeds()
  turtle.up()
  turtle.turnRight()
  for X = 1, 11 do
    turtle.select(X)
    c = turtle.compareTo(14)
    if c == true then
	  turtle.drop(turtle.getItemCount(X))
    end
  end
  turtle.turnLeft()
  turtle.down()
end
function changeDirection()
  if turnDir == 1 then
    --[[ 1 = turning right --]]
    if facing == 1 then
	  --[[ 1 = original facing direction --]]
	  facing = 2
    end
    if facing == 2 then
	  --[[ 2 = right --]]
	  facing = 3
    end
    if facing == 3 then
	  --[[ 3 = opposite of original direction --]]
	  facing = 4
    end
    if facing == 4 then
	  --[[ 4 = left --]]
	  facing = 1
    end
  else
   --[[ 2 = turning left --]]
    if facing == 1 then
	  --[[ 1 = original facing direction --]]
	  facing = 4
    end
    if facing == 2 then
	  --[[ 2 = right --]]
	  facing = 1
    end
    if facing == 3 then
	  --[[ 3 = opposite of original direction --]]
	  facing = 2
    end
    if facing == 4 then
	  --[[ 4 = left --]]
	  facing = 3
    end
  end
end
--[[ set numeric var's --]]
fuellvl = 0
amtchar = 0
X = 0
seedlvl = 0
c = "string"
facing = 1
  --[[ 1 = original facing direction
	   2 = right
	   3 = opposite of original direction
	   4 = left --]]
turnDir = 0
  --[[ 0 = hasn't turned yet
	   1 = turning right
	   2 = turning left --]]
countColumn = 1
countRow = 1
countRowLock = 0
--[[ refuel section --]]
fuellvl = turtle.getFuelLevel()
if fuellvl<100 then
  amtchar = turtle.getItemCount(13)
  if amtchar > 1 then
    turtle.select(13)
    turtle.refuel(2)
    turtle.select(1)
  else
    if amtchar > 0 then
	  turtle.select(13)
	  turtle.refuel(1)
	  turtle.select(1)
    end
  end
end
--[[ get seeds section --]]
seedlvl = turtle.getItemCount(14)
if seedlvl < 64 then
  turtle.up()
  turtle.turnRight()
  turnDir = 1
  changeDirection()
  turtle.select(14)
  turtle.suck()
  seedlvl = turtle.getItemCount(14)
  turtle.turnLeft()
  turnDir = 2
  changeDirection()
  turtle.down()
end
clearexcessseeds()
--[[ move into farm room --]]
turtle.forward()
turtle.forward()
turtle.turnLeft()
turnDir = 2
changeDirection()
turtle.select(14)
--[[ find starting position --]]
while turtle.detect() == false do
  turtle.forward()
end
turtle.turnRight()
turnDir = 1
changeDirection()
keepFarming = 1
currentRowLoc = 0
--[[ measure RECTANGULAR room --]]
while turtle.detect() == false do
  turtle.forward()
  countRow = countRow + 1
end
countRowLock = 1
turtle.turnRight()
turnDir = 1
changeDirection()
while turtle.detect() == false do
  turtle.forward()
  countColumn = countColumn + 1
end
turtle.turnRight()
turnDir = 1
changeDirection()
for X = 1, countRow-1 do
  turtle.forward()
end
turtle.turnRight()
turnDir = 1
changeDirection()
for X = 1, countColumn-1 do
  turtle.forward()
end
turtle.turnRight()
turnDir = 1
changeDirection()
--[[ clear the farm and return --]]
for X = 1, countColumn-1 do
  for Y = 1, countRow-1 do
    turtle.select(16)
    if turtle.compareDown() == false then
	  turtle.digDown()
	  turtle.select(14)
	  turtle.placeDown()
    end
    turtle.forward()
  end
  print ("cleared a column")
  if facing == 1 then
    print ("facing value = ")
    print (facing)
    print ("facing same direction as start")
  else
    print ("facing value = ")
    print (facing)
    print ("facing opposite direction as start")
  end
  print ("turning around")
  if facing == 1 then
    turtle.turnRight()
    turnDir = 1
    changeDirection()
    turtle.forward()
    turtle.turnRight()
    turnDir = 1
    changeDirection()
  else
    turtle.turnLeft()
    turnDir = 2
    changeDirection()
    turtle.forward()
    turtle.turnLeft()
    turnDir = 2
    changeDirection()
  end
  print ("finished turning around")
  if facing == 1 then
    print ("facing value = ")
    print (facing)
    print ("facing same direction as start")
  else
    print ("facing value = ")
    print (facing)
    print ("facing opposite direction as start")
  end
end
--[[ Return turtle to starting position --]]
for X = 1, countColumn-1 do
  turtle.forward()
end
turtle.turnRight()
turnDir = 1
changeDirection()
turtle.turnRight()
turnDir = 1
changeDirection()
turtle.forward()
turtle.turnRight()
turnDir = 1
changeDirection()
turtle.forward()
turtle.forward()
turtle.turnRight()
turnDir = 1
changeDirection()
turtle.turnRight()
turnDir = 1
changeDirection()
keepFarming = 0
--[[ the turtle is back at his starting position
	  but still needs to reset inventory --]]
[/CODE]
TyCamden #2
Posted 21 October 2012 - 03:00 PM
Anyone ?
cheekycharlie101 #3
Posted 21 October 2012 - 03:17 PM
couldn't pick anything up, but are you sure you have added an option where it know's how many rows to do? if you only have it doing 2 rows then it will just go back to the first. also look for any infinite loops and end them
ChunLing #4
Posted 21 October 2012 - 10:07 PM
You should try having your functions take parameters and execute things. For instance, "changeDirection()" should take 1 or 2 as a parameter, and then turn the turtle left or right as well as updating the facing value, like so:
function changeDirection(trndrctn)
	if trndrctn == 1 then
		turtle.turnRight()
   	 facing = math.fmod(facing,4)+1
	else
		turtle.turnLeft()
   	 facing = math.fmod(facing+2,4)+1
	end
end
Then, instead of needing to call turtle.turnRight/Left() turnDir = 1/2 changeDirection() every time, you just call changeDirection(1) (or 2). That will help make your direction/facing tracking much easier to use consistently.

If you want to be fancy you could have it take the facing as an additional parameter and return the new facing, but as long as facing is global you don't really need to do that.