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

Basic block detection

Started by QuietDove, 04 February 2018 - 08:50 PM
QuietDove #1
Posted 04 February 2018 - 09:50 PM
Hey there!

Complete novice here, so I understand that this is probably a very basic program!

I need to make a block detector that outputs a redstone signal when there isn't a block in front of it. I've tried the 'turtle.detect()' command but it didn't seem to work.

Thanks in advance.
Anavrins #2
Posted 04 February 2018 - 11:08 PM
turtle.detect should be enough for your needs, post the code you have so far so we can help you fix what isn't working.
QuietDove #3
Posted 05 February 2018 - 01:02 AM
Thanks for the reply,

I've literally just put this in https://imgur.com/a/yaq93

I'm a complete novice when it comes to this!
Bomb Bloke #4
Posted 05 February 2018 - 09:18 AM
It's one thing to have the turtle detect a block, but if you want it to do something based on a block's presence, then you have to specify what that action's going to be.

turtle.detect() returns a boolean representing either true or false depending on whether a block is found in front of the turtle when it's called.

rs.setOutput() accepts a boolean, and uses it to either activate or de-activate a redstone signal from a given side of the turtle.

For example, we can activate a rear signal if a block is present by doing:

rs.setOutput( "back", turtle.detect() )

For the opposite (activating a signal when a block is not present), we can inverse the bool:

rs.setOutput( "back", not turtle.detect() )

Since you likely want the turtle to continuously check the space in front of it while updating the redstone signal accordingly, the line should be wrapped within a while loop:

while true do    --# Starts a loop that repeats indefinitely.
	rs.setOutput( "back", not turtle.detect() )
	sleep(1)   --# Wait a second before continuing.
end
QuietDove #5
Posted 05 February 2018 - 05:22 PM
That sounds like what I want, I just need it to send a signal when the space directly in front is empty so another block can be placed there.

https://imgur.com/a/7qa8F

Is this correct?
Bomb Bloke #6
Posted 06 February 2018 - 08:20 AM
Looks good.
QuietDove #7
Posted 06 February 2018 - 02:18 PM
Thanks! This is working now!

How would I do this to make the redstone signal only activate for a single tick?
Bomb Bloke #8
Posted 07 February 2018 - 08:37 AM
That's not much more complex. The same concepts apply, just slightly re-organised:

while true do
	while turtle.detect() do sleep(1) end  --# Wait until the front is clear.
	
	rs.setOutput( "back", true )
	sleep(0.1)
	rs.setOutput( "back", false )
	
	while not turtle.detect() do sleep(1) end --# Wait for the block to re-appear.
end

Note that there are "server" ticks (0.05s, a twentieth of a second), and there are "redstone" ticks (0.1s, a tenth of a second). If you change the state of a redstone signal more often than the redstone tick speed, whatever's at the end of the wire may not register that the state changed at all.
QuietDove #9
Posted 07 February 2018 - 04:32 PM
Thanks so much! This is exactly what I was looking for.

Thanks for your help!