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

Help with loops and if statements

Started by gorila665, 29 June 2013 - 06:45 AM
gorila665 #1
Posted 29 June 2013 - 08:45 AM
Title: Help with loops and if statements

Hello!
I am new to computercraft and I have only made a tunnel digging program.
I'm trying to make a woodcuting progrma were the turtle waits, when he receives a redstone signal he will go forward once and cut the tree.
If he doesn't get a redstone signal he will wait 10 secconds and try again.
This is the code I tried:
——————————————————————————
if turtle.getInput(left) then
(All commands for cuting the tree I didn't have time to write it all)
else
sleep(10)
end
—————————————————————————–
Please help me!
Lyqyd #2
Posted 29 June 2013 - 02:28 PM
Split into new topic.

Looks like you're not actually using a loop, so it would only check once and then exit the program. Try putting `while true do` before the code and `end` after it to put it in a loop.
gorila665 #3
Posted 30 June 2013 - 05:48 AM
I need it to loop all the time and when it see a redstone signal it will cut the tree, how can I do it?
And also when I just try this code it will always cut the tree even if there is no signal.
Apfeldstrudel #4
Posted 30 June 2013 - 06:35 AM
 while true do
   os.pullEvent("redstone")
   do stuff
end

Should work :D/>, welcome to the forums!
Jappards #5
Posted 30 June 2013 - 09:28 AM
This code might even be better:

while true do
local RedstoneInput = rs.getInput("left")--you need to use rs or redstone
if RedstoneInput == true then--checks to see if the redstone is on
  (do stuff)
end
sleep(10)
end
This code does the thing you specify in "do stuff" as long as a redstone signal is being applied to it, that means that you can just wire the redstone being connected to a lever and make it constantly cut trees, but with Xyexs`s code (no offence) you need to wire the redstone up to a clock.
You can chose what code you like the best, i don`t mind your decision.

Also: you can use CTRL+T to terminate loops.
Read more here:http://computercraft.info/wiki/Redstone_(API)
gorila665 #6
Posted 30 June 2013 - 11:22 AM
Many thanks to both of you! :D/>