Trying to set up an item detector.

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here
  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
When I want is an item traveling through a pipe (pneumatic or otherwise) to be detected along all its length to act like a sequencer of 32 different points to detect said item.

Is this possible?
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
I'm sorry but I don't think I understand what you are saying. There is a block added by RP2 called the item detector that will emit a redstone pulse when an item passes through. Buildcraft gates can be configured to do a similar thing for buildcraft pipes and they can output their signal to pipe wire which can extend the signal to other parts of a pipe. What are you detecting, and what 32 variables are you planning on determining based on that one item?
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
I have a pipe 32 blocks long and I want to detect the traveling of in item through the entire length and get a signal emitted at each point.

I was wrong about solving it, the item needs to stay at its current section until a redstone signal tells it to move to the next in line. A pipe can't do that.
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
Out of interest, what is this whole contraption for? Turtles might be able to do what you want. They can detect if there is stuff in their inventory and emit a redstone signal based on it, and they can also be programmed to pass their item onto the next line in the chain at a certain point, for example when they receive a redstone pulse.
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
It's to open up an area to the sky when there's a redstone pulse: one pulse opens the first one and the second pulse closes the first and opens the second.

I don't know anything about Turtles and it needs to run by itself on off EU.
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
If using Turtles, what code do I need to use to tell it to hold an item, emit a redstone signal and when a redstone signal is received, pass along the item to the next turtle in line?
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
Also, is the item important, or are you using it as a timing mechanism? Sorry, its still not competely clear. If it is a timing mechanism, you can cut it out because turtles and computers can count seconds. If you want to make a turtle wait for a duration before moving on to the next bit of code use this command, where t is a number representing time in seconds

Code:
sleep(t)
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
Here is what I'm trying to do:
zvzu.png

Each piston needs to be opened by a redstone signal and then close with a second signal that also opens the next piston in line.


I have absolutely no clue how to program in LUA.
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
Ok, this can be done quite easily with a turtle. The first thing to do is make the turtle turn left (type turn left in the command line) so it facing down the line of pistons. The basic function to turn each piston on sequentially will look like this (read the comments too, some of them show you where the code can be altered, and they also explain what the code does. If you copy and paste this you can leave the comments in the code because they are behind a -- which will make the computer ignore everything after it on that line)

Code:
local num = 32 -- Change this to however many pistons there are in the row
local delay = 2 -- Change this to a value in seconds for the duration for keeping one piston on
num = num -1 -- This is important if the turtle starts next to the first piston because otherwise the turtle will go one extra block. If the turtle starts one behind the first piston, delete this line
redstone.setOutput("right", true) --This sets the redstone on the right of the turtle to true. It will be true everywhere the turtle goes until it is turned off in the code
for i = 1, num do -- For loops repeat the code block below (the end of the block is declared by writing end). i stands for iterations I think and the number of iterations in this case (set after the equal sign) is between 1 and whatever num is.
  sleep(delay) -- Pauses the turtlle in its current state. Will keep the adjacent turtle powered
  turtle.forward() -- This make the turtle go forward
end -- closes the for loop
sleep(delay) -- pauses the turtle one last time before it returns back to the start
redstone.setOutput("right", false) -- this just turns off the redstone signal
for i = 1, num do -- this loop just returns the turtle back to where it started
  turtle.back()
end

It might not be the best program but I have tested it and it works.
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
Didn't work. For some reason, the turtle opened the piston and then did nothing. Thanks anyway. Hope I can figure this out or update Ultimate to 1.5.2 without RedPower.
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
Did you copy it exactly. I should also have mentioned that the turtle needs fuel to run. You can fuel a turtle by putting a fuel item (pretty much anything that burns in a furnace) such as charcoal/coal into the turtles inventory and then type refuel all. It will work after that. I thought that the thing to the left of the turtle was a charger (I think its from miscperipherals). They can charge turtles with EU but I have never used them so I couldn't be sure especially with that texture pack.
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
Yes, it's a charger. Copies Line by line and it powered the piston and that's it. Didn't do anything else.
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
have you ran the refuel program, it should tell you what the fuel level is. If it is >0 and there is nothing in the way of the turtle then I can't understand why it wont work. It worked in my test instance before I posted it.

There is one reason why it might not work that I can think of but only if you copied it by hand. The for loop on line 5 relies on the variable num. It sets the maximum number of iterations. num is defined on line one. Make sure that num is spelt exactly the same wherever it appears in the program. even changes in capitalisation will throw it off.

Did it throw any errors?
 

Jess887cp

New Member
Jul 29, 2019
922
2
1
The only thing I can think of would be a system where the item is spat through a piece of string before being sucked into a buffer.

A piece of string receives a block update whenever something interacts with it, so if you hook that up to a BUD connected to a toggle, with the toggle connected to an engine or something, you might be able to work something out.
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
have you ran the refuel program, it should tell you what the fuel level is. If it is >0 and there is nothing in the way of the turtle then I can't understand why it wont work. It worked in my test instance before I posted it.

There is one reason why it might not work that I can think of but only if you copied it by hand. The for loop on line 5 relies on the variable num. It sets the maximum number of iterations. num is defined on line one. Make sure that num is spelt exactly the same wherever it appears in the program. even changes in capitalisation will throw it off.

Did it throw any errors?


I think I know what I did wrong...I added 5 more turtles thinking the first one won't move. Will try again.

OK, it works. Thanks. Is there a way to have it automatically recharge, or do I have to do it manually? Also, Is it possible to have the turtle run it's programs when it receives a Redstone Signal?

Sorry if my questions are going fast, but is there also a way to detect when the turtle has returned to its beginning point? Or even to have it emit a redstone signal from its back side?
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
Is there a way to have it automatically recharge, or do I have to do it manually? Also, Is it possible to have the turtle run it's programs when it receives a Redstone Signal?

Yes to both things, although it will require editing the original program just a bit.
By the way, everything I say is based on the screenshot you posted earlier so some directions and other stuff might be wrong, but that shouldn't be too hard to tweak. To be honest, this is starting to get to be quite a big program. I am willing to help you but before I write any actual code for this application I will tell you about a couple of core concepts and then you can apply those.

First of all, are conditionals. This checks if a certain condition is satisfied and executes code based on that.
This is important for making this program activate when it receives a redstone signal. Especially when combined with events (another concept which I will talk about later)

to open a conditional you need this keyword> if
then you write the variable that you are checking. You can call another variable or just write something into it
then you write what you are checking it against. Use the following checks, == if you want to check if x does equal y, > if x is greater than y, < if x is less than y, ~= if x does not equal y, followed by the check variable
finish the condition statement by writing >then
then type the code that you want to execute if the condition is met followed by > end

I will give an example script that will demonstrate conditionals

local digit = 5
if digit > 2 then
print("digit is greater than 2")
elseif digit == 2 then
print("digit does equal 2")
else
print("digit is not greater than 2")
end

events are things that happen to the computer/turtle that can be detected. If you want the computer/turtle can wait to do its job until a defined event has happened using this command
local event = os.pullEvent(type of event)
type of event can be a string for example "redstone". If you type os.pullEvent("redstone") into a code, everything after it will only happen once the event has been triggered
you can follow this with a condition to check if the redstone input is true and then execute the main code if it is true

functions
functions are pieces of code that exist that can be called by the program. They make writing programs cleaner and easier, and if you need to execute the same piece of code several times in a program, put that code inside a function.
To make a function, write function followed by the name of the function and then a pair of parentheses. Then on the next line put in your code and close it by writing end. Here is an example

function message()
print("hello world")
end

when this function is called it will execute the code within, i.e hello world will appear on the screen.
You can put a variable inside the function to make the function do subtly different things each time it is called.

function message(printThis)
print(printThis)
end
message("Hello world")
message("I am doing stuff")
local anotherMessage = "You can pass variables too. This string is a variable"
message(anotherMessage)
---
will result in:
Hello world
I am doing stuff
You can pass variables too. This string is a variable

you can write a refueling function that will detect how much fuel the turtle has and if it is less than what it needs it will get fuel and refuel. You can put a chest full of fuel underneath the turtle at the turtle's starting point and the turtle can pull from that chest and use the fuel to refuel. So long as you keep the chest supplied it should be ok. Or you could export coal with your logistics system directly to the turtle, but I would recommend the chest method (only because that is what I do, turtles are very versatile and when you get to grips with it the coding you can get them to do tonnes of stuff in the way that you want/need).

finally, loops
I used a for loop in the program I wrote earlier
another type of loop is the while loop which checks a condition and runs if the condition is met, forever or until the condition is no longer met.
To set a program to run in perpetuity encase it in a while loop like this

while true do
code() -- whatever you want to code
end

and that will run forever. It is advisable to put a sleep() command at the end of the while loop (before writing end) because if the block is constantly doing stuff it might become a miniature lag machine. A setup like this is usually preferable for me at least for the programs I have written. An alternative is waiting for events.

while true do
code()
sleep(5)
end

In this case code could be waiting for a redstone event, checking to see if redstone is true and then running the code I wrote earlier with the refuel function put in at the beginning. If you try to make sure that the turtle has 100 fuel at all times I think that will serve you well. I may have missed something. I wrote this post, quite piecemeal so sorry if its fragmented but I am busy. If I missed anything or if it is still unclear here are some good links

http://computercraft.info/wiki/Category:APIs -- This link is important. There are tons of useful commands containing most of the stuff you will be wanting to do with computercraft (probably). Two important ones are redstone api and turtle api
http://computercraft.info/wiki/Turtle_(API) -- Contains movement command, inventory management commands and also refueling commands.
http://computercraft.info/wiki/Redstone_(API)
http://computercraft.info/wiki/Tutorials -- The introduction to coding bit is very useful and may well contain the answer to any question you have left.

Whoa this post is long, I sure hope it helps
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
Thanks for the long post and helping me, will try to write that code after I watch Sethbling's turtle tutorials and read the links you provided. If I have a problem, I might ask for your help later so as not to disturb you too much or be too annoying.

Oh, forgot something, are the programs saved internally or do I have to save them on disk? I don't want to be having to write the code over again if I remove the turtle.
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
Don't worry, if you save a program to a computer, that computer will remember it. Same goes for turtles. If you are on single player if you go into your minecraft folder and open the save folder, you should see a folder called Computer and inside that is a group of folders whose names are numbers. The numbers refer to the id of each computer in the world that has saved a new file. You can open those files with a text editor (and back them up). I use notepad++ by the way which is a really good text editor which is free. If you want to find out what the computer or turtles id is ingame just type in id.

IMPORTANT: Label your turtles and computers with this command, label set name. You can change name to anything you want. If you don't label your computer they will forget all their programs.