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

Help a noob debug his first turtle program!

Started by ticklemestalin, 12 February 2014 - 09:41 AM
ticklemestalin #1
Posted 12 February 2014 - 10:41 AM
I'm in a single player game, and this is strictly for my use, although anyone is welcome to use or adapt it into whatever they want without giving credit.

This program should plant seedlings. I'm getting an error from the compiler that I don't understand:
"bios:338: [string "Plant"]:1: '=' expected"

Please feel free to give advice on the actual program as well. Thank you!


-- The purpose of this program is to plant oak seedlings
-- in every dirt patch in four 5x5 (25 seedlings each) squares.
-- The dirt squares are two spaces apart from each other in a
-- square.  It will then return the turtle to a chest, dump its
-- remaining inventory and return to a "home" position, ready
-- for the next planting.

-- rom/programs/http/pastebin get um2XtCPL Plant
-- turtle position must be 361, 278 facing north

-- LOOP COUNTERS
i = 5
k = 1
rowcount = 1
rowscounted = 0

-- VARIABLES
seedstock = 16
slotempty = false

-- FUNCTIONS

-- plantrow() plants one entire length of both squares, 5
-- seedlings (five iterations of plant down -> move forward), a
-- gap of 2 squares, then 5 more seedlings.  After each square
-- it makes sure it has enough seedlings to plant another square,
-- (seedstock) and if not it switches inventory spots.
function plantrow()
  i = 5
  turtle.select(seedstock)
  while (i > 0) do
	turtle.forward()
	turtle.placeDown()
	i = i-1
  end
	if turtle.getItemCount(seedstock) < 10 then
	seedstock = seedstock - 1
  end
  turtle.forward()
  turtle.forward()
  i = 5
  turtle.select(seedstock)
  while (i > 0) do
	turtle.forward()
	turtle.placeDown()
	i = i-1
  end
	if turtle.getItemCount(seedstock) < 5 then
	  seedstock = seedstock - 1
  end
end

-- endofrow() turns the turtle left or right depending on which
-- direction it just planted (rowcount), and positions it on
-- the next row.  It also determines when it has reached the gap
-- between squares (rowsplanted) and skips over it.
function endofrow()
  turtle.forward()
  if rowcount == 1 then
	turtle.turnLeft()
	if rowscounted == 5 then
	  turtle.forward()
	  turtle.forward()
	end	
	turtle.forward()
	turtle.turnLeft()
  end
  if rowcount == 2 then
	turtle.turnRight()
	turtle.forward()
	turtle.turnRight()
  end
  if rowcount == 1 then
	rowcount = 2
	else rowcount = 1
  end
end

-- dumpinventory() puts the turtle's entire inventory in a chest
-- below it.
function dumpinventory()
  while k < 17 do
	turtle.select(k)
	turtle.dropDown()
	k = k+1
  end  
end

-- Main Program
turtle.select(1)
turtle.refuel()

turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.up()

while rowsplanted < 10 do
  plantrow()
  endofrow()
  rowsplanted = rowsplanted + 1  
end

turtle.forward()
turtle.forward()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
dumpinventory()
turtle.forward()
turtle.down()
turtle.turnRight()
CometWolf #2
Posted 12 February 2014 - 11:41 AM
You sure that's the entire code? AS there is nothing wrong on line 1 as far as i can tell. You could however do a multi line comment instead with –[[]]–

--[[
This
is
all
considered
the same comment
--]]

As for the code itself, it could be shortened quite a bit with the use of for loops.

for variable = from, to, inc/dec
--[[
variable will store the current loop iteration
from is what number to start at
to is what number to end the loop at
and inc/dec is an optional argument spcifiying what you want to increase/decrease the variable by after each loop. It will default to 1
--]]
end

for i=1,6 do
  print(i)
end
--This will print 1,2,3,4,5,6

Also you should use elseif instead of multiple ifs, this way only the first one to be true will execute.

if condition then
  --derp
elseif otherCondition then
  --derp2
else -- none of the above
  --derp3
end


function endofrow()
  turtle.forward()
  if rowcount == 1 then
	    turtle.turnLeft()
	    if rowscounted == 5 then
		  turtle.forward()
		  turtle.forward()
	    end	
	    turtle.forward()
	    turtle.turnLeft()
	    rowcount = 2
	    else rowcount = 1
  elseif rowcount == 2 then
	    turtle.turnRight()
	    turtle.forward()
	    turtle.turnRight()
  end
end
ticklemestalin #3
Posted 12 February 2014 - 03:13 PM
Thanks for the reply CometWolf! This is indeed the entire program. I'd post the pastebin but I just hit my limit for the day. Once I have it working I'll incorporate your examples. The line 1 error is really confusing me, because I've gotten earlier iterations of the program to work just fine using the error line number suggestion from the compiler. Line 1 should just be commenting. Any other suggestions on what could be wrong?
Bomb Bloke #4
Posted 12 February 2014 - 04:52 PM
Are you sure this is the code actually saved on the turtle's drive?

Does rebooting the turtle make a difference?
ticklemestalin #5
Posted 12 February 2014 - 10:05 PM
I have destroyed and remade the turtle a few times testing it. I'll post the pastebin tomorrow and go through the exact steps I use to load it into the turtle's memory. I'm am brand new to computercraft, and it could easily be some kind of procedural mistake. Stay tuned.
ticklemestalin #6
Posted 13 February 2014 - 09:59 AM
Well, it my problems have resolved themselves somewhat:
I had a bad variable name which led to confusion between rowscounted and rowsplanted;
When I load a program into memory and then edit it and load it again, even if I put a number after the name to make it a different program, they both come up with the line #1 error. This is only fixed when I destroy the turtle, shut down the game and place the turtle again.

The upshot is the program is executing again, although I've made a mistake somewhere in the 5th iteration and it wanders increasingly off course. Back to the drawing board.

Thanks for looking at this with me, and I'm sure I'll be back to ask more questions!