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

too long without yielding error

Started by neiromaru, 12 October 2013 - 09:55 AM
neiromaru #1
Posted 12 October 2013 - 11:55 AM
I'm trying to set up a turtle that will place a specific mystcraft book into a portal book receptacle based on a specific button being pressed. (it also does some fancy stuff with activating drawbridges to expose the portal, and controls a router with redstone because turtles can't suck out of book receptacles in this version.)
I know very little about programming, but the method I came up with was to loop an if/else if with an else if for each frequency of the bundled cable.
Unfortunately when I run it it goes for a few seconds then quits and returns "startup:11: Too long without yielding"

I would greatly appreciate any help or advice in rewriting this to something that will actually work.

http://pastebin.com/JkGsCBSM
Spoiler

while true do
if rs.getBundledInput("back") ==1	   --getting the input from a button connected with rednet cable
then rs.setBundledOutput("right", 9)	  --Activating drawbridges to expose the portal, and disabling the router
turtle.select(1)		   --Selecting the appropriate book for this button
turtle.drop()				   --Placing the book
os.sleep(10)			   --Delay for people to enter the portal
rs.setBundledOutput("right", 1)		--Enabling the router to suck the book out (drawbridges still activated)
os.sleep(.1)			  --delay to make sure book is pulled out
turtle.suckDown()			   --pulling the book out of the router back into the turtle
rs.setBundledOutput("right", 0)		  --resetting the drawbridges
else if rs.getBundledInput("back") ==2
then rs.setBundledOutput("right", 9)
turtle.select(2)
turtle.drop()
os.sleep(10)
rs.setBundledOutput("right", 1)
os.sleep(.1)
turtle.suckDown()
rs.setBundledOutput("right", 0)
else if rs.getBundledInput("back") ==4
then rs.setBundledOutput("right", 9)
turtle.select(3)
turtle.drop()
os.sleep(10)
rs.setBundledOutput("right", 1)
os.sleep(.1)
turtle.suckDown()
rs.setBundledOutput("right", 0)
else if rs.getBundledInput("back") ==8
then rs.setBundledOutput("right", 9)
turtle.select(4)
turtle.drop()
os.sleep(10)
rs.setBundledOutput("right", 1)
os.sleep(.1)
turtle.suckDown()
rs.setBundledOutput("right", 0)
else if rs.getBundledInput("back") ==16
then rs.setBundledOutput("right", 9)
turtle.select(5)
turtle.drop()
os.sleep(10)
rs.setBundledOutput("right", 1)
os.sleep(.1)
turtle.suckDown()
rs.setBundledOutput("right", 0)
else if rs.getBundledInput("back") ==32
then rs.setBundledOutput("right", 9)
turtle.select(6)
turtle.drop()
os.sleep(10)
rs.setBundledOutput("right", 1)
os.sleep(.1)
turtle.suckDown()
rs.setBundledOutput("right", 0)
else if rs.getBundledInput("back") ==64
then rs.setBundledOutput("right", 9)
turtle.select(7)
turtle.drop()
os.sleep(10)
rs.setBundledOutput("right", 1)
os.sleep(.1)
turtle.suckDown()
rs.setBundledOutput("right", 0)
else if rs.getBundledInput("back") ==128
then rs.setBundledOutput("right", 9)
turtle.select(8)
turtle.drop()
os.sleep(10)
rs.setBundledOutput("right", 1)
os.sleep(.1)
turtle.suckDown()
rs.setBundledOutput("right", 0)
if rs.getBundledInput("back") ==256
then rs.setBundledOutput("right", 9)
turtle.select(9)
turtle.drop()
os.sleep(10)
rs.setBundledOutput("right", 1)
os.sleep(.1)
turtle.suckDown()
rs.setBundledOutput("right", 0)
else if rs.getBundledInput("back") ==512
then rs.setBundledOutput("right", 9)
turtle.select(10)
turtle.drop()
os.sleep(10)
rs.setBundledOutput("right", 1)
os.sleep(.1)
turtle.suckDown()
rs.setBundledOutput("right", 0)
else if rs.getBundledInput("back") ==1024
then rs.setBundledOutput("right", 9)
turtle.select(11)
turtle.drop()
os.sleep(10)
rs.setBundledOutput("right", 1)
os.sleep(.1)
turtle.suckDown()
rs.setBundledOutput("right", 0)
else if rs.getBundledInput("back") ==2048
then rs.setBundledOutput("right", 9)
turtle.select(12)
turtle.drop()
os.sleep(10)
rs.setBundledOutput("right", 1)
os.sleep(.1)
turtle.suckDown()
rs.setBundledOutput("right", 0)
else if rs.getBundledInput("back") ==4096
then rs.setBundledOutput("right", 9)
turtle.select(13)
turtle.drop()
os.sleep(10)
rs.setBundledOutput("right", 1)
os.sleep(.1)
turtle.suckDown()
rs.setBundledOutput("right", 0)
else if rs.getBundledInput("back") ==8192
then rs.setBundledOutput("right", 9)
turtle.select(14)
turtle.drop()
os.sleep(10)
rs.setBundledOutput("right", 1)
os.sleep(.1)
turtle.suckDown()
rs.setBundledOutput("right", 0)
else if rs.getBundledInput("back") ==16384
then rs.setBundledOutput("right", 9)
turtle.select(15)
turtle.drop()
os.sleep(10)
rs.setBundledOutput("right", 1)
os.sleep(.1)
turtle.suckDown()
rs.setBundledOutput("right", 0)
else if rs.getBundledInput("back") ==32768
then rs.setBundledOutput("right", 9)
turtle.select(16)
turtle.drop()
os.sleep(10)
rs.setBundledOutput("right", 1)
os.sleep(.1)
turtle.suckDown()
rs.setBundledOutput("right", 0)
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
Lyqyd #2
Posted 12 October 2013 - 02:50 PM
Split into new topic.

Here's a fixed version of your code using elseif instead of else if, and using os.pullEvent to wait for a change in redstone before checking the redstone input.


while true do
	--# wait for a change in redstone
	os.pullEvent("redstone")
	if rs.getBundledInput("back") == 1 then
		rs.setBundledOutput("right", 9)
		turtle.select(1)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)  
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	--# elseif is a keyword, and is highly useful, so you need only one end for the whole if block.
	elseif rs.getBundledInput("back") == 2 then
		rs.setBundledOutput("right", 9)
		turtle.select(2)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	elseif rs.getBundledInput("back") == 4 then
		rs.setBundledOutput("right", 9)
		turtle.select(3)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	elseif rs.getBundledInput("back") == 8 then
		rs.setBundledOutput("right", 9)
		turtle.select(4)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	elseif rs.getBundledInput("back") == 16 then
		rs.setBundledOutput("right", 9)
		turtle.select(5)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	elseif rs.getBundledInput("back") == 32 then
		rs.setBundledOutput("right", 9)
		turtle.select(6)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	elseif rs.getBundledInput("back") == 64 then
		rs.setBundledOutput("right", 9)
		turtle.select(7)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	elseif rs.getBundledInput("back") == 128 then
		rs.setBundledOutput("right", 9)
		turtle.select(8)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	elseif rs.getBundledInput("back") == 256 then
		rs.setBundledOutput("right", 9)
		turtle.select(9)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	elseif rs.getBundledInput("back") == 512 then
		rs.setBundledOutput("right", 9)
		turtle.select(10)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	elseif rs.getBundledInput("back") == 1024 then
		rs.setBundledOutput("right", 9)
		turtle.select(11)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	elseif rs.getBundledInput("back") == 2048 then
		rs.setBundledOutput("right", 9)
		turtle.select(12)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	elseif rs.getBundledInput("back") == 4096 then
		rs.setBundledOutput("right", 9)
		turtle.select(13)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	elseif rs.getBundledInput("back") == 8192 then
		rs.setBundledOutput("right", 9)
		turtle.select(14)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	elseif rs.getBundledInput("back") == 16384 then
		rs.setBundledOutput("right", 9)
		turtle.select(15)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	elseif rs.getBundledInput("back") == 32768 then
		rs.setBundledOutput("right", 9)
		turtle.select(16)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	end
end

Here's a version of your code that uses a little bit of math to cut down on the amount of code you need:


while true do
	os.pullEvent("redstone") --# wait for a change in redstone
	if rs.getBundledInput("back") > 0 then
		rs.setBundledOutput("right", 9)
		--# use bundled input to determine which slot to select.
		turtle.select(math.floor(math.log(rs.getBundledInput("back")) / math.log(2)) + 1)
		turtle.drop()
		os.sleep(10)
		rs.setBundledOutput("right", 1)
		os.sleep(.1)  
		turtle.suckDown()
		rs.setBundledOutput("right", 0)
	end
end
neiromaru #3
Posted 12 October 2013 - 03:39 PM
That works perfectly, thank you so much!
I knew there must be a way to use logs to simplify the code, but I had no idea how to code that.