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

Error - short new turtle program wont run at all

Started by TyCamden, 07 October 2012 - 01:38 PM
TyCamden #1
Posted 07 October 2012 - 03:38 PM
I am pretty new at ComputerCraft turtle programming. The following farm program that I started writing will not run. Help please ?


--[[ make sure the following slots
have correct items
slots 1-11 clear
slot 12 has 64 coal/charcoal
slot 13 has 1 seed
slot 14 has 1 wheat
slot 15 has 1 cobble
slot 16 has 1 stone
--]]
function clearexcessseeds()
  turtle.up()
  turtle.turnRight()
  for X = 1 to 11
	turtle.select(X)
	c = turtle.compareTo(13)
	if c = "true" then
	  turtle.drop(turtle.getItemCount(X))
	end
  next
  turtle.turnLeft()
  turtle.down()
end
--[[ set numeric var's --]]
fuellvl = 0
amtchar = 0
X = 0
seedlvl = 0
c = "string"
--[[ refuel section --]]
fuellvl = turtle.getFuelLevel()
if fuellvl<100 then
  amtchar = turtle.getItemCount(12)
  if amtchar > 1 then
	turtle.select(12)
	turtle.refuel(2)
	turtle.select(1)
  else
	if amtchar > 0 then
	  turtle.select(12)
	  turtle.refuel(1)
	  turtle.select(1)
	end
  end
end
--[[ get seeds section --]]
seedlvl = turtle.getItemCount(13)
if seedlvl < 64 then
  turtle.up()
  turtle.turnRight()
  turtle.select(13)
  turtle.suck()
  seedlvl = turtle.getItemCount(13)
  turtle.turnLeft()
  turtle.down()
end
clearexcessseeds()
--[[ the following will be the meat of the farm program
turtle.forward()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.digDown()
--]]
Doyle3694 #2
Posted 07 October 2012 - 04:07 PM
gash dang it unformated code, *takes face close to screen*
Kingdaro #3
Posted 07 October 2012 - 04:19 PM
There's a massive problem with this chunk here:
  for X = 1 to 11
		turtle.select(X)
		c = turtle.compareTo(13)
		if c = "true" then
The first line should be "for X=1, 11 do", and the last one shouldn't have quotations around true.
TyCamden #4
Posted 08 October 2012 - 05:28 PM
Thanks :D/>/>
TyCamden #5
Posted 08 October 2012 - 05:42 PM
I am still getting an error…

bios:206: [string "farm"]:17: 'then' expected

I dont know what this means or how to fix.

Latest code as follows:


--[[ make sure the following slots
have correct items
slots 1-11 clear
slot 12 has 64 coal/charcoal
slot 13 has 1 seed
slot 14 has 1 wheat
slot 15 has 1 cobble
slot 16 has 1 stone
--]]
function clearexcessseeds()
  turtle.up()
  turtle.turnRight()
  for X = 1, 11 do
    turtle.select(X)
    c = turtle.compareTo(13)
    if c = true then
	  turtle.drop(turtle.getItemCount(X))
    end
  end
  turtle.turnLeft()
  turtle.down()
end
--[[ set numeric var's --]]
fuellvl = 0
amtchar = 0
X = 0
seedlvl = 0
c = "string"
--[[ refuel section --]]
fuellvl = turtle.getFuelLevel()
if fuellvl<100 then
  amtchar = turtle.getItemCount(12)
  if amtchar > 1 then
    turtle.select(12)
    turtle.refuel(2)
    turtle.select(1)
  else
    if amtchar > 0 then
	  turtle.select(12)
	  turtle.refuel(1)
	  turtle.select(1)
    end
  end
end
--[[ get seeds section --]]
seedlvl = turtle.getItemCount(13)
if seedlvl < 64 then
  turtle.up()
  turtle.turnRight()
  turtle.select(13)
  turtle.suck()
  seedlvl = turtle.getItemCount(13)
  turtle.turnLeft()
  turtle.down()
end
clearexcessseeds()
--[[ the following will be the meat of the farm program
turtle.forward()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.digDown()
--]]
Lyqyd #6
Posted 08 October 2012 - 07:48 PM
if c = true then
should be:
if c == true then
TyCamden #7
Posted 09 October 2012 - 02:18 PM
Running now … I am on my way…

Thanks for all your help.

Hopefully I will learn each lesson Once, and learn it well.
TyCamden #8
Posted 09 October 2012 - 07:11 PM
New problem. It won't run. If I had to guess, it may have something to do with the goto command or label of target of that goto. Here is all code involved. Please give a looksie… 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()
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
--[[ clear the farm and return --]]
while keepFarming == 1 do
  turtle.select(16)
  if turtle.compareDown() == false then
    turtle.digDown()
    turtle.select(14)
    turtle.placeDown()
  end
  if turtle.detect() == false then
    if countRowLock == 0 then
	  turtle.forward()
	  countRow = countRow + 1
    else
	  if currentRowLoc > 1 then
	    turtle.forward()
	    if facing == 1 then
		  currentRowLoc = currentRowLoc + 1
	    else
		  currentRowLoc = currentRowLoc - 1
	    end
	  else
	    --[[  There is a hole there but we have gone
			   beyond the first column and are on row 1
			   so pretend there is no hole --]]
	    goto "pretendNoHole"
	  end if
    end
  else
    :pretendNoHole
    if countRowLock == 0 then
	  countRowLock = 1
	  currentRowLoc = countRow
    end if
    if facing == 1 then
	  --[[ the turtle is facing the way he began --]]
	  turtle.turnRight()
	  turnDir = 1
	  changeDirection()
	  if turtle.detect() == false then
	    turtle.forward()
	    turtle.turnRight()
	    turnDir = 1
	    changeDirection()
	    countColumn = countColumn + 1
	  else
		 --[[ the whole farm has been Farmed --]]
	    turtle.turnRight()
	    turnDir = 1
	    changeDirection()
	    if countRow-1 > 0 then
		  for X = 1, countRow-1 do
		    turtle.forward()
		  end
	    end
	    turtle.turnRight()
	    turnDir = 1
	    changeDirection()
	  end
    else
	  --[[ the turtle is facing the opposite of the way he began --]]
	  turtle.turnLeft()
	  turnDir = 2
	  changeDirection()
	  if turtle.detect() == false then
	    turtle.forward()
	    turtle.turnLeft()
	    turnDir = 2
	    changeDirection()
	    countColumn = countColumn + 1
	  else
		 --[[ the whole farm has been Farmed --]]
	    turtle.turnRight()
	    turnDir = 1
	    changeDirection()
	  end
	  if countColumn-1 > 0 then
	    for X = 1, countColumn-1 do
		  turtle.forward()
	    end
	  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
    end
  end
end
--[[ the turtle is back at his starting position
	  but still needs to reset inventory --]]
[/CODE]
TyCamden #9
Posted 09 October 2012 - 11:08 PM
See previous post, but also note…

I tried changing a portion of the above code to read as follows but that did not work either…


	    goto "pretendNoHole"
	  end if
    end
  else
    :: pretendNoHole ::
faubiguy #10
Posted 09 October 2012 - 11:21 PM
There is no goto in lua 5.1, which is used by ComputerCraft. It was added in 5.2. You'll probably need to change the control flow using if statement to get where you need to.
TyCamden #11
Posted 09 October 2012 - 11:28 PM
Oh i see. Ok, thanks !
TyCamden #12
Posted 09 October 2012 - 11:57 PM
Revised turtle program. Still wont run.

This time it says UNEXPECTED SYMBOL

If anyone sees it, please let me know. Thanks!

code…


--[[ 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
--[[ clear the farm and return --]]
while keepFarming == 1 do
  turtle.select(16)
  if turtle.compareDown() == false then
    turtle.digDown()
    turtle.select(14)
    turtle.placeDown()
  end
  if turtle.detect() == false then
    if countRowLock == 0 then
	  turtle.forward()
	  countRow = countRow + 1
    else
	  if currentRowLoc > 1 then
	    turtle.forward()
	    if facing == 1 then
		  currentRowLoc = currentRowLoc + 1
	    else
		  currentRowLoc = currentRowLoc - 1
	    end
	  else
	    --[[  There is a hole there but we have gone
			   beyond the first column and are on row 1
			   so pretend there is no hole --]]
	    if countRowLock == 0 then
		  countRowLock = 1
		  currentRowLoc = countRow
	    end if
	    if facing == 1 then
		  --[[ the turtle is facing the way he began --]]
		  turtle.turnRight()
		  turnDir = 1
		  changeDirection()
		  if turtle.detect() == false then
		    turtle.forward()
		    turtle.turnRight()
		    turnDir = 1
		    changeDirection()
		    countColumn = countColumn + 1
		  else
			 --[[ the whole farm has been Farmed --]]
		    turtle.turnRight()
		    turnDir = 1
		    changeDirection()
		    if countRow-1 > 0 then
			  for X = 1, countRow-1 do
			    turtle.forward()
			  end
		    end
		    turtle.turnRight()
		    turnDir = 1
		    changeDirection()
		  end
	    else
		  --[[ the turtle is facing the opposite of the way he began --]]
		  turtle.turnLeft()
		  turnDir = 2
		  changeDirection()
		  if turtle.detect() == false then
		    turtle.forward()
		    turtle.turnLeft()
		    turnDir = 2
		    changeDirection()
		    countColumn = countColumn + 1
		  else
			 --[[ the whole farm has been Farmed --]]
		    turtle.turnRight()
		    turnDir = 1
		    changeDirection()
		  end
		  if countColumn-1 > 0 then
		    for X = 1, countColumn-1 do
			  turtle.forward()
		    end
		  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
	    end
	  end if
    end
  else
    if countRowLock == 0 then
	  countRowLock = 1
	  currentRowLoc = countRow
    end if
    if facing == 1 then
	  --[[ the turtle is facing the way he began --]]
	  turtle.turnRight()
	  turnDir = 1
	  changeDirection()
	  if turtle.detect() == false then
	    turtle.forward()
	    turtle.turnRight()
	    turnDir = 1
	    changeDirection()
	    countColumn = countColumn + 1
	  else
		 --[[ the whole farm has been Farmed --]]
	    turtle.turnRight()
	    turnDir = 1
	    changeDirection()
	    if countRow-1 > 0 then
		  for X = 1, countRow-1 do
		    turtle.forward()
		  end
	    end
	    turtle.turnRight()
	    turnDir = 1
	    changeDirection()
	  end
    else
	  --[[ the turtle is facing the opposite of the way he began --]]
	  turtle.turnLeft()
	  turnDir = 2
	  changeDirection()
	  if turtle.detect() == false then
	    turtle.forward()
	    turtle.turnLeft()
	    turnDir = 2
	    changeDirection()
	    countColumn = countColumn + 1
	  else
		 --[[ the whole farm has been Farmed --]]
	    turtle.turnRight()
	    turnDir = 1
	    changeDirection()
	  end
	  if countColumn-1 > 0 then
	    for X = 1, countColumn-1 do
		  turtle.forward()
	    end
	  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
    end
  end
end
--[[ the turtle is back at his starting position
	  but still needs to reset inventory --]]
[/CODE]
Lyqyd #13
Posted 10 October 2012 - 01:12 AM
What is "end if" supposed to be? It's just end. Correct all of those, then let us know if there's a new error. If there is, please include the line number, as it makes errors much easier to find.
TyCamden #14
Posted 10 October 2012 - 12:18 PM
That was it ! "End If" is a leftover command from my old days with other programming Languages.

It now runs. It may not do exactly what I want it to, but this is a logic error on my part that I will work on.

Thanks everyone for helping me out on this.