Jump to content




First Program attempt - stops at a point with no indication of an error


5 replies to this topic

#1 jeddell

  • Members
  • 3 posts

Posted 26 April 2017 - 09:05 AM

Hi everyone,

Been studying computercraft for a total of two weeks now, specifically writing a program that will get a turtle to:
1. Measure the depth, length and width of a quarry
2. build a wall around the inside of said quarry and while doing that
a. restock its inventory each time it runs out, remembering where it ran out and returning to that location once it is refilled its inventory
b. check / refuel at the diagonally opposite corners

once done, return home.

I have the program on a game disk.
CC version is 1.58 (I'm using a hopper and chest to fill the turtle and running a while loop to check slot 16 for 64 blocks)

Ive been watching BGSamuel's tutorials on mining bots and I'm trying to adapt it to build rather than dig.

Id appreciate any help on trying fix the bugs here.

My experience with lua is about the same amount of time that ive been studying computercraft so if I could ask you to keep you responses at a basic level, id really appreciate it.

heres the link to the script:
https://pastebin.com/RME81LCV

cheers

Jeddell

PS, Just noticed some function havent been listed at the top of the script. will fix that directly.

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 26 April 2017 - 10:17 AM

View Postjeddell, on 26 April 2017 - 09:05 AM, said:

Id appreciate any help on trying fix the bugs here.

It'd help if you better described them. At what "point" is your turtle stopping?

#3 jeddell

  • Members
  • 3 posts

Posted 26 April 2017 - 12:01 PM

My apologies, I can pinpoint that location a little better than that.. The turtle completed the left() function at the start of the wall() function, after that it is supposed to do the moveUp() function and it didnt. below is the moveup function:

function moveUp()
  yCoord = yCoord +1
  moved = false
  while not(moved) do
    moved = turtle.up()
  end
end


#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 27 April 2017 - 12:58 AM

If moveUp() is being called (and you can confirm that by putting a print call or two in it), but the turtle is not moving upwards, then I'd be inclined to suspect a fuel shortage... you do try to refuel before reaching that point in the code, but you don't stop to check if it was successful or not.

#5 jeddell

  • Members
  • 3 posts

Posted 27 April 2017 - 10:38 PM

Thanks for the reply. I placed some print call in the refuel function and the turtle states that the refuel was successful, However it still stopped at the end of the left() function and didnt moveUP() When I opened the turtle the print calls stating refueling was successful, it didnt take any fuel (63 coal still in slot one when there should have been 62) and the fuel check was zero. so it seems that this pesky refuel function us still causing me grief....

function refuel()
  if turtle.getFuelLevel() < 10 then
	turtle.refuel(1)
print("Refuel successful")
  else
	print("did not refuel")
  end
end

it obviously checks for the fuel level because it sends the print call. do I need to select slot 1 first? I would have thought that command was redundant in 1.58 because of the the inclusion of the 'fuel slot'

Update
So it appears that I do need to select slot 1 to do the refueling. Slot one being where the fuel for the turtle is located. adding that line into the refuel function solved that issue. Once this happened, I then found two more things that the turtle is doing that I really didn't want it to do. The first issue is in the slots function. I set the function up so that as it empties slot 2, it will move to slot 3 and continue. 's' is the variable that stores the slot currently being used. What I found however is that if the next slot is also empty, the turtle will move forward one space before it checks the next slot. It will keep on doing this until is gets to slot 16 at which time will switch to the restock function. The problem is there are now 'x' number of empty spaces in the wall that should have blocks. I need Bob (Thats my turtle!) to stop moving forward until its found a slot with some blocks in it and if it reaches slot 16 and that is empty, the go on to the restock function. below is the code for the slots function:

function slots()
  if turtle.getItemCount(s) == 0 then
   if s < 16 then
	 turtle.select(s+1)
     s = s+1
   else
	if s == 16 then
	 if turtle.getItemCount(16) < 1 then
	   refuel2()
	   restock()
     end
   end
 end
  os.sleep(0.5)
end
end

Edited by jeddell, 28 April 2017 - 05:55 AM.


#6 Larry84

  • Members
  • 51 posts
  • LocationItaly

Posted 28 April 2017 - 08:58 AM

Try putting the function in a while loop, then use break to stop the turtle checking for items when it founds a slot with items or it reaches sot 16.
Also, I suggest using elseif statements.
Something like this:
function slots()
  while turtle.getItemCount(s) == 0 do	--Until it detects there is nothing in the selected slot, it continues doing this
	if s < 16 then			--If the slot is less than 16, it selects the next one
	  turtle.select(s+1)
	  s = s+1
	elseif s == 16 then	  --if the selected slot is the 16th, it will refuel and restock
	  refuel2()
	  restock()
	  break
	end
  end
  os.sleep(0.5) --Why are you sleeping here?
end

Edited by Larry84, 28 April 2017 - 09:02 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users