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

AE2 processor crafting system with turtles

Started by astonish01, 17 July 2015 - 02:14 AM
astonish01 #1
Posted 17 July 2015 - 04:14 AM
So, I'm doing this program to craft processor automatically, but the program works properly till the part where it asks you which processor you want, I did just the calculation one atm, but when I answer "calculation" or "calc" (tried changing the answer) the program stops, simply stops.

write("Put presses in slots 13 to 16 (all 4 bottom ones) in order:Silicon;Calculation;Logic;Engineering")
sleep(10)
write("Done? ")
local dn = ( read() )
if dn == "yes" then
write("Wich processor? ")
local prc = ( read() )
elseif prc == "calc" then
write("How many? ")
local qtd = ( read() )
write("Put "..qtd.." silicon in slot 1, "..qtd.." pure certus quartz in slot 2, and "..qtd.." redstone in slot 3")
write("Ready? ")
local rdy = ( read() )
elseif rdy == "yes" then
turtle.select(13)
turtle.up()
turtle.forward()
turtle.dropDown()
turtle.back()
turtle.down()
turtle.select(1)
for i=1,qdt do
turtle.select(1)
turtle.drop()
sleep(15)
turtle.select(5)
turtle.suck()
end
turtle.up()
turtle.forward()
turtle.select(13)
turtle.suckDown()
turtle.select(14)
turtle.dropDown()
turtle.back()
turtle.down()
for i=1,qtd do
turtle.select(2)
turtle.drop()
sleep(15)
turtle.select(6)
turtle.suck()
end
turtle.up()
turtle.forward()
turtle.select(14)
turtle.suckDown()
for i=1,qtd do
turtle.select(5)
turtle.back()
turtle.down()
turtle.select(3)
turtle.drop()
turtle.down()
turtle.forward()
turtle.select(6)
turtle.dropUp()
turtle.back()
turtle.up()
sleep(15)
turtle.select(7)
turtle.suck()
turtle.up()
turtle.forward()
end
else
sleep(5)
end

or a link: http://pastebin.com/nxXDftTR

What the program does is: it uses the items in the turtle's inventory, to "drop" in the right places of the inscriber and craft the processors.
Bomb Bloke #2
Posted 17 July 2015 - 04:31 AM
if dn == "yes" then
	write("Wich processor? ")
	local prc = ( read() )
elseif prc == "calc" then  -- This line is only processed if dn was NOT "yes" (ELSEif!), and prc will only be defined if dn WAS "yes", so...

One way you could phrase things (of many):

write("Put presses in slots 13 to 16 (all 4 bottom ones) in order:Silicon;Calculation;Logic;Engineering")

write("Done? (say \"yes\")")

repeat local dn = read() until dn == "yes"

write("Wich processor? ")

local prc
repeat prc = read() until prc == "calc" or prc == "otherProcessor" or prc == "whatever"
astonish01 #3
Posted 17 July 2015 - 04:34 AM
if dn == "yes" then
	write("Wich processor? ")
	local prc = ( read() )
elseif prc == "calc" then  -- This line is only processed if dn was NOT "yes" (ELSEif!), and prc will only be defined if dn WAS "yes", so...

One way you could phrase things (of many):

write("Put presses in slots 13 to 16 (all 4 bottom ones) in order:Silicon;Calculation;Logic;Engineering")

write("Done? (say \"yes\")")

repeat local dn = read() until dn == "yes"

write("Wich processor? ")

local prc
repeat prc = read() until prc == "calc" or prc == "otherProcessor" or prc == "whatever"

Sorry for asking, but I would like to understand what you said rather than just copying and pasting, can you explain it for me?
Bomb Bloke #4
Posted 17 July 2015 - 04:43 AM
Sure, have a read through this document.
astonish01 #5
Posted 17 July 2015 - 04:47 AM
Thanks, I think I got the repeat loop, good to learn new things :D/>
astonish01 #6
Posted 17 July 2015 - 05:41 AM
The program works fine, thanks for the help! :D/>/>

EDIT: So, now, I need to make the code for the rest of the processors, but with my current understanding of the repeat loop I have no idea what to do to say that if the user gives a different answer of the until condition it runs another block.
EXAMPLE:

repeat local x = read() until x == "thing1" -- How can I make it run block2 if the answer is "thing2"
block1
????
????
????
block2
Also, if I answer anything else than the processor I want, the program "bugs".
Edited on 17 July 2015 - 04:38 AM
HPWebcamAble #7
Posted 17 July 2015 - 07:12 AM
Personally, I wouldn't use a repeat loop if you have more than one option.

Well, I'd use buttons, but text input is much easier, so we'll go with that for now.


For text input, this is how I'd do it:

while true do
  local input = read()

  if input == "thing1" then
    --# Do thing1
  elseif input == "thing2" then
    --# Do thing2
  else
    print("Not an option!")  --# print puts the text on the screen, then moves the cursor to the next line
  end
end
You can do this with a repeat loop, but in this style (get input, do stuff for each possibility), a while loop is usually used)

I'm assuming you know what a while-loop is. If not, its similar to the repeat loop, but with the repeat, the code is executed once before the condition is checked.
The while loop checks the condition BEFORE executing its code. In this case, it is checking if 'true' is 'true', which means it will run until we call 'break', which forces the loop to stop.
astonish01 #8
Posted 17 July 2015 - 08:46 AM
Personally, I wouldn't use a repeat loop if you have more than one option.

Well, I'd use buttons, but text input is much easier, so we'll go with that for now.


For text input, this is how I'd do it:

while true do
  local input = read()

  if input == "thing1" then
	--# Do thing1
  elseif input == "thing2" then
	--# Do thing2
  else
	print("Not an option!")  --# print puts the text on the screen, then moves the cursor to the next line
  end
end
You can do this with a repeat loop, but in this style (get input, do stuff for each possibility), a while loop is usually used)

I'm assuming you know what a while-loop is. If not, its similar to the repeat loop, but with the repeat, the code is executed once before the condition is checked.
The while loop checks the condition BEFORE executing its code. In this case, it is checking if 'true' is 'true', which means it will run until we call 'break', which forces the loop to stop.

Well, this is what I came up with atm (I tried ifelse, it doesnt work too)

print("Put presses in slots 13 to 16 (all 4 bottom ones) in order:Silicon;Calculation;Logic;Engineering ")
sleep(1)
write(" Done? ")
repeat local dn = read() until dn == "yes"
write("Wich processor? ")
local prc = ( read() )
if prc == "calculation" or "calc" then
print("How many? ")
local qtd = ( read() )
print("Put "..qtd.." silicon in slot 1, "..qtd.." pure certus quartz in slot 2, and "..qtd.." redstone in slot 3 ")
sleep(1)
write(" Ready? ")
repeat local rdy = read() until rdy == "yes"
turtle.select(13)
turtle.up()
turtle.forward()
turtle.dropDown()
turtle.back()
turtle.down()
turtle.select(1)
for i=1,qtd do
turtle.select(1)
turtle.drop()
sleep(3)
turtle.select(5)
turtle.suck()
end
turtle.up()
turtle.forward()
turtle.select(13)
turtle.suckDown()
turtle.select(14)
turtle.dropDown()
turtle.back()
turtle.down()
for i=1,qtd do
turtle.select(2)
turtle.drop()
sleep(3)
turtle.select(6)
turtle.suck()
end
turtle.up()
turtle.forward()
turtle.select(14)
turtle.suckDown()
for i=1,qtd do
turtle.select(5)
turtle.dropDown()
turtle.back()
turtle.down()
turtle.select(3)
turtle.drop()
turtle.down()
turtle.forward()
turtle.select(6)
turtle.dropUp()
turtle.back()
turtle.up()
sleep(3)
turtle.select(7)
turtle.suck()
turtle.up()
turtle.forward()
end
else
if prc == "logic" or "log" then
write("How many? ")
local qtd = ( read() )
print("Put "..qtd.." silicon in slot 1, "..qtd.." gold ingots in slot 2, and "..qtd.." redstone in slot 3 ")
sleep(1)
write(" Ready? ")
repeat local rdy = read() until rdy == "yes"
turtle.select(13)
turtle.up()
turtle.forward()
turtle.dropDown()
turtle.back()
turtle.down()
turtle.select(1)
for i=1,qtd do
turtle.select(1)
turtle.drop()
sleep(3)
turtle.select(5)
turtle.suck()
end
turtle.up()
turtle.forward()
turtle.select(13)
turtle.suckDown()
turtle.select(14)
turtle.dropDown()
turtle.back()
turtle.down()
for i=1,qtd do
turtle.select(2)
turtle.drop()
sleep(3)
turtle.select(6)
turtle.suck()
end
turtle.up()
turtle.forward()
turtle.select(14)
turtle.suckDown()
for i=1,qtd do
turtle.select(5)
turtle.dropDown()
turtle.back()
turtle.down()
turtle.select(3)
turtle.drop()
turtle.down()
turtle.forward()
turtle.select(6)
turtle.dropUp()
turtle.back()
turtle.up()
sleep(3)
turtle.select(7)
turtle.suck()
turtle.up()
turtle.forward()
end
else
if prc == "eng" or "engineering" then
write("How many? ")
local qtd = ( read() )
print("Put "..qtd.." silicon in slot 1, "..qtd.." diamonds in slot 2, and "..qtd.." redstone in slot 3 ")
sleep(1)
write(" Ready? ")
repeat local rdy = read() until rdy == "yes"
turtle.select(13)
turtle.up()
turtle.forward()
turtle.dropDown()
turtle.back()
turtle.down()
turtle.select(1)
for i=1,qtd do
turtle.select(1)
turtle.drop()
sleep(3)
turtle.select(5)
turtle.suck()
end
turtle.up()
turtle.forward()
turtle.select(13)
turtle.suckDown()
turtle.select(14)
turtle.dropDown()
turtle.back()
turtle.down()
for i=1,qtd do
turtle.select(2)
turtle.drop()
sleep(3)
turtle.select(6)
turtle.suck()
end
turtle.up()
turtle.forward()
turtle.select(14)
turtle.suckDown()
for i=1,qtd do
turtle.select(5)
turtle.dropDown()
turtle.back()
turtle.down()
turtle.select(3)
turtle.drop()
turtle.down()
turtle.forward()
turtle.select(6)
turtle.dropUp()
turtle.back()
turtle.up()
sleep(3)
turtle.select(7)
turtle.suck()
turtle.up()
turtle.forward()
end
else
print("INVALID INPUT")
end  
end
end

turtle.turnRight()
turtle.forward()
turtle.dropDown()
turtle.back()
turtle.turnLeft()
turtle.back()
turtle.down()
No matter what answer I give, it stills runs the first block
Edited on 17 July 2015 - 06:47 AM
flaghacker #9
Posted 17 July 2015 - 12:05 PM

if prc == "calculation" or "calc" then
Or doesn't work that way. Or takes a boolean on each side. Because anything other then nil or false is true in lua, "calc" is considered true and so the or always results in true.

Change your code to this:

if prc == "calculation" or prc == "calc" then
There are some other pkaces in your code where you did it wrong too, so go ahead and fix those too.