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

Automatic Sheep Shearing

Started by DavEdward, 03 January 2013 - 06:41 AM
DavEdward #1
Posted 03 January 2013 - 07:41 AM
I'm still a bit new to LUA but after a number of attempts I successfully made a simple Sheep Shearing program to automate getting wool.

*This program requires a Shearing turtle from MiscPeripherals 2.3 or higher!*

There is one bug with this program which is that I tried to add a code to the program to have the turtle detect if the chest it was trying to put wool into was full it would stop working and wait for there to be space in the chest. However this code kept throwing an error that I didn't know how to correct so I commented out that peice of the code.

As long as you make sure the chest doesn't get over-filled, the program works great. If someone feels up to debugging that problem in my code please share!

For the setup, it's best to have two shearing turtles in a 5x3 or 6x3 space set up n a way that there is only 1 block between the turtles and walls keeping the sheep in such as below.


T = Turtle
S = Space

S S S S S
S T S T S
S S S S S
  or
S S S S S S
S T S S T S
S S S S S S

Here's the code:
Spoiler

-- Sheep shearing program for shearing turtle
-- Fuel must be provided in a chest directly above the turtle
-- Collected wool is placed in the chest directly under the turtle

cycles = 0
totalwool = 0


local function shear()
  turtle.attack()
  turtle.turnRight()
  turtle.attack()
  turtle.turnRight()
  turtle.attack()
  turtle.turnRight()
  turtle.attack()
  turtle.turnRight()
end

local function updateterm()
  term.clear()
  print("---------------------------------")
  write("Cycles: ")
  write(cycles)
  write(" | ")
  write("Total Wool: ")
  write(totalwool)
  print("---------------------------------")
  print("")
end

local function getFuel()
  turtle.select(16)
  while turtle.getItemCount(16) < 2 do
	redstone.setOutput("top", true)
	sleep(5)
	redstone.setOutput("top", false)
	turtle.suckUp(63)
	os.sleep(1)
	  if turtle.getItemCount(16) < 2 then
		print("I need more fuel in above chest")
		print("Waiting 30 seconds before checking again")
		os.sleep(30)
	  end
  end
end

local function checkFuel()
while turtle.getFuelLevel() < 10 do
  getFuel()
  turtle.select(16)
  turtle.refuel(1)
end
end

local function storeWool()
  storecheck = false
--  while storecheck == false do
	  for i=1,15,1 do
		if turtle.getItemCount(i) > 0 then
		  turtle.select(i)
	  totalwool = totalwool + turtle.getItemCount(i)
		  storecheck = not turtle.dropDown()
		end
	  end
--  end
  if storagespace == false then
	print("The inventory is full I can't store my harvest")
	print("Waiting 30 seconds before checking again")
	os.sleep(30)
  end
end

while true do
checkFuel()
shear()
storeWool()
cycles = cycles + 1
updateterm()
os.sleep(5)
end


Set up a coal chest above the turtles, and a storage chest below both turtles. The coal chests are optional. To my own experiance feeding a turtle a stack of charoal is enough to have it run for so long I filled a double-chest with wool.

If you have BuildCraft, Redpower, or similar, you may want to pull items out of the chests under the turtles and pipe it elsewhere. If desired it's easy enough to flip the code so the turtle puts items in the chest above them, and either tries to fetch coal from below, or just fill them yourself.


Edits:

Edit 1,2,3: Removed garbage code I forgot was in there during testing, fixed indentation some to reduce confusion.
Edit 4: Changed the line "storecheck = turtle.dropDown()" to "storecheck = not turtle.dropDown()" to see if that works. Left code commented out till i can test it.
unobtanium #2
Posted 03 January 2013 - 07:55 AM
Hello there,

i looked over your code and your bug with the "store into chest" part.
First you have one more "end" then you need. I added some notations behind the "ends".
Spoiler

local function storeWool()
	print("Starting function to store wool")
storecheck = false
--  while storecheck == false do
	for i=1,15,1 do
		  if turtle.getItemCount(i) > 0 then
	turtle.select(i)
totalwool = totalwool + turtle.getItemCount(i)
	write("Working on invintory slot:")
	print(i)
			storecheck = turtle.dropDown()
--		end -- ends if
	end [color=#666600][size=2]--[/size][/color] ends while
  end -- ends function. maybe this has to be else
	print("Reached part if chest is full")
  if storagespace == false then
	print("The inventory is full I can't store my harvest")
	print("Waiting 30 ticks before checking again")
	os.sleep(30)
  end -- ends if
end -- ??? :D/>/>/>/>/>/>

Now to your bug:
storecheck = turtle.dropDown()
This gives true back if they Turtle could place the whole stack into the chest. If the chest is full, it returns false.
At the beginning you ask in the while loop, if storecheck is false. If the first stack in slot 1 is sucessfully taken out, it stops the while loop and never take out slot 2-15. edit: It never ends the while loop if the wool successfully drop it into the chest, i think :/
And it will go into the else section and start saying that it needs more space in the chests :D/>

I hope i solved it. Change end to else and storecheck = turtle.dropDown() to storecheck = not turtle.dropDown()
And you dont need a while loop accually :D/> Put the while-else part into the for loop. I hope this make sense :P/>

UNOBTANIUM
Cranium #3
Posted 03 January 2013 - 08:02 AM
I'm pretty sure that shears can be used by regular turtles, can't they?
Draco18s #4
Posted 03 January 2013 - 08:39 AM
Hello there,

i looked over your code and your bug with the "store into chest" part.
First you have one more "end" then you need. I added some notations behind the "ends".
Spoiler

local function storeWool()
	print("Starting function to store wool")
storecheck = false
--  while storecheck == false do
	for i=1,15,1 do
		  if turtle.getItemCount(i) > 0 then
	turtle.select(i)
totalwool = totalwool + turtle.getItemCount(i)
	write("Working on invintory slot:")
	print(i)
			storecheck = turtle.dropDown()
--		end -- ends if
	end [color=#666600][size=2]--[/size][/color] ends while
  end -- ends function. maybe this has to be else
	print("Reached part if chest is full")
  if storagespace == false then
	print("The inventory is full I can't store my harvest")
	print("Waiting 30 ticks before checking again")
	os.sleep(30)
  end -- ends if
end -- ??? :D/>/>/>/>/>/>/>

You missed a for() loop.

Spoiler
local function storeWool()
	    print("Starting function to store wool")
	    storecheck = false
	    while storecheck == false do
		    for i=1,15,1 do
			    if turtle.getItemCount(i) > 0 then
				    turtle.select(i)
				    totalwool = totalwool + turtle.getItemCount(i)
				    write("Working on invintory slot:")
				    print(i)
				    storecheck = turtle.dropDown()
			    end --ends if turtle.getItemCount
		    end --ends for i 1->15
	    end --ends while storecheck
	    print("Reached part if chest is full")
	    if storagespace == false then
		    print("The inventory is full I can't store my harvest")
		    print("Waiting 30 seconds before checking again")
		    os.sleep(30)
	    end --ends if storagespace
end --ends function

DavEdward #5
Posted 03 January 2013 - 09:01 AM
Hello there,

i looked over your code and your bug with the "store into chest" part.
First you have one more "end" then you need. I added some notations behind the "ends".
Spoiler

local function storeWool()
	print("Starting function to store wool")
storecheck = false
--  while storecheck == false do
	for i=1,15,1 do
		  if turtle.getItemCount(i) > 0 then
	turtle.select(i)
totalwool = totalwool + turtle.getItemCount(i)
	write("Working on invintory slot:")
	print(i)
			storecheck = turtle.dropDown()
--		end -- ends if
	end [color=#666600][size=2]--[/size][/color] ends while
  end -- ends function. maybe this has to be else
	print("Reached part if chest is full")
  if storagespace == false then
	print("The inventory is full I can't store my harvest")
	print("Waiting 30 ticks before checking again")
	os.sleep(30)
  end -- ends if
end -- ??? :D/>/>/>/>/>/>/>

Now to your bug:
storecheck = turtle.dropDown()
This gives true back if they Turtle could place the whole stack into the chest. If the chest is full, it returns false.
At the beginning you ask in the while loop, if storecheck is false. If the first stack in slot 1 is sucessfully taken out, it stops the while loop and never take out slot 2-15. edit: It never ends the while loop if the wool successfully drop it into the chest, i think :/
And it will go into the else section and start saying that it needs more space in the chests :D/>

I hope i solved it. Change end to else and storecheck = turtle.dropDown() to storecheck = not turtle.dropDown()
And you dont need a while loop accually :D/> Put the while-else part into the for loop. I hope this make sense :P/>

UNOBTANIUM

As Draco18s pointed out there was a sneaky if statement in there for that extra end. However when I can test things I'll try swapping that storecheck to a "not turtle.dropDown()" instead.
DavEdward #6
Posted 03 January 2013 - 09:02 AM
I'm pretty sure that shears can be used by regular turtles, can't they?

Not to my knowledge. I have MiscPeripherals installed and one of it's featues is making a shearing turtle. I don't think CC can do it without that additional mod.
Mikeemoo #7
Posted 04 January 2013 - 03:41 AM
Sheers can be used by regular turtle, yes.

However, they of course start to break after a while which adds a bit of complication.
PixelToast #8
Posted 04 January 2013 - 03:54 AM
Sheers can be used by regular turtle, yes.

However, they of course start to break after a while which adds a bit of complication.
uhh, i dont see them here: http://computercraft.info/wiki/Turtle
and i dont see durablility anywhere
Mikeemoo #9
Posted 04 January 2013 - 03:58 AM
Sheers can be used by regular turtle, yes.

However, they of course start to break after a while which adds a bit of complication.
uhh, i dont see them here: http://computercraft.info/wiki/Turtle
and i dont see durablility anywhere

turtle.place() will use them if they're in the inventory.
PixelToast #10
Posted 04 January 2013 - 03:59 AM
*facepalm*
i fail
unobtanium #11
Posted 06 January 2013 - 02:24 AM
Wops. I failed too :D/>
PixelToast #12
Posted 06 January 2013 - 06:14 AM
i saw a sheep farm on geevancraft but it used block breakers i think
it was also on fire
Cranium #13
Posted 08 January 2013 - 05:54 AM
No, it was using deployers.
leftler #14
Posted 01 March 2013 - 06:05 PM
Here is a bit better placement pattern, this gives you all 16 colors gaurteneed with 4 turtles, the nice thing about this design is you don't need to wait for the sheep to be in the right place at the right time to be sheered.. You have to put a block above the turtles so the sheep don't jump on top of them and move out of their assigned slots, if you are not using some kind of pipe system (build craft, red power 2, ect) to empty the chests you could put the chests on top of the turtles and use them has the blocking blocks so you could reach them and empty the chests by hand.



Also, you can remove your fuel code, turning does not cost fuel, only forward, back, up and down does