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

Turtle that Detects if Block is Destroyed

Started by kmgagnon, 18 January 2013 - 09:22 AM
kmgagnon #1
Posted 18 January 2013 - 10:22 AM
So I am trying to create a program on a trutle that detects that if the block below it (Iron Door) is destroyed. If it is it will send a redstone signal to an alarm (Industrial Alarm from Industrial Craft). I can not seem to get it to work. Please help! Here is what I have:


if turtle.detectDown()
print ("Clear")
end
else
rs.setOutput("left", true)
sleep(1)
rs.setOutput("left", false)
sleep(2)

Please Help and Thank you in advanced!
theoriginalbit #2
Posted 18 January 2013 - 10:27 AM
You are missing a then at the end of the if statement…

However here is an infinitely looping program that checks for a block, and if its not there will play the alarm until one comes back (however this can be fooled with any block)

local outputOn = false
while true do
  if turtle.detectDown() and not outputOn then
	outputOn = true
	rs.setOutput( "left", true )
  elseif not turtle.detectDown() and outputOn then
	outputOn = false
	rs.setOutput( "left", false )
  end
  sleep(0.1)
end

Smarter code that cant be fooled ( place iron door in slot 1 )

local outputOn = false
turtle.select( 1 )
while true do
  if not turtle.compareDown() and not outputOn then
	outputOn = true
	rs.setOutput( "left", true )
  elseif turtle.compareDown() and outputOn then
	outputOn = false
	rs.setOutput( "left", false )
  end
  sleep(0.1)
end
Edited on 18 January 2013 - 09:28 AM
KaoS #3
Posted 18 January 2013 - 10:47 AM

while turtle.detectDown() do
  sleep(0.5)
end
rs.setOutput("left",true)

nice 'n simple. do you really need the pulse? I would prefer a constant signal myself

EDIT: if you really need it to be constantly re-usable (which you shouldn't if you think about it)

while true do
  rs.setOutput("left",not turtle.detectDown())
  sleep(0.5)
end
would work too
Edited on 18 January 2013 - 09:50 AM