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

Receive pulse from bundled cables and while help

Started by unter_hosen, 21 September 2017 - 11:54 PM
unter_hosen #1
Posted 22 September 2017 - 01:54 AM
Hi Guys

I hope someone would be kind enough to help. I have zero expertise with lua so please bare with me on this one.

Basically my requirement is something like this

when the program runs for the first time I would like it to pulse some cables in a particular order, which I think works with the code below. After this is where I have no clue where to look.

What I would like to happen is this.

When the program starts, it pulses the cables in a particular order, which are connected to retreivers, which are in turn connected to chests, then when a pulse is received on a different color (could be any color) from an item detector, it then pulses white, but it only needs to do this on 10 occasions, so I know i need some kind of counting mechanism, but I have no idea how to incorporate that.

After 10 pulses from an item detector, I then need the program to wait until a pulse is received via the bundled cable on an entirely different color cable and then restart the program again. So i am guessing I need a while clause of some sort, but……… :S

Can someone point me in the right direction at all?


This is how far I got


term.clear()
term.SetCursorPos(1, 1)
print "Beginning Sort"
rs.SetBundledOutput("back", 1)
sleep "1"
rs.SetBundledOutput("back", 4)
sleep "1"
rs.SetBundledOutput("back", 4)
sleep "1"
rs.SetBundledOutput("back", 1)
sleep "1"
rs.SetBundledOutput("back", 1)
sleep "1"
rs.SetBundledOutput("back", 1)
sleep "1"
rs.SetBundledOutput("back", 4)
print "Ended sorting output"
Edited on 22 September 2017 - 12:57 AM
Bomb Bloke #2
Posted 22 September 2017 - 04:40 AM
when the program runs for the first time I would like it to pulse some cables in a particular order, which I think works with the code below.

Probably not. First off, the sleep() function expects numbers, and by wrapping your 1's in quotes you're passing them in as strings (text representations). If you want to sleep for a second for eg, you'd generally do sleep(1).

Then there's the point that you're setting your output to the same thing multiple times in a row. For example, say you do this:

rs.SetBundledOutput("back", 1)
sleep(1)
rs.SetBundledOutput("back", 1)
sleep(1)
rs.SetBundledOutput("back", 1)
sleep(1)

This'll turn on the white wire and leave it on for a full three seconds. Is that how long you wanted your "pulse" to last? Or did you intend to perform three separate pulses over this time? Assuming the latter case, you might instead do something like:

rs.SetBundledOutput("back", 1)  --# Enable white only.
sleep(0.5)
rs.SetBundledOutput("back", 0)  --# Disable all output.
sleep(0.5)
rs.SetBundledOutput("back", 1)
sleep(0.5)
rs.SetBundledOutput("back", 0)
sleep(0.5)
rs.SetBundledOutput("back", 1)
sleep(0.5)
rs.SetBundledOutput("back", 0)
sleep(0.5)

This could be simplified by bundling the repeated bits into a function at the top of your code:

local function pulse( colour )
	rs.SetBundledOutput("back", colour)
	sleep(0.5)
	rs.SetBundledOutput("back", 0)
	sleep(0.5)
end

… which would allow you to later do:

pulse(1)
pulse(4)
pulse(4)
pulse(1)
pulse(1)
pulse(1)
pulse(4)

Now let's say you want the code to be able to pause until a certain colour activates. We might build another function for that, making use of a "while" loop which repeats so long as the colour is off:

local function waitForColour( colour )
	while not rs.testBundledInput( "back", colour ) do   --# So long as the desired colour isn't active on the back...
		os.pullEvent("redstone")                     --# ... keep yielding (doing nothing) until a redstone state change occurs.
	end
end

And a "for" loop is generally used to repeat something a set number of times:

local function pulseList()
	pulse(1)
	pulse(4)
	pulse(4)
	pulse(1)
	pulse(1)
	pulse(1)
	pulse(4)
end

pulseList()  --# Run through the list once.

--# Then do it another ten times:
for i = 1, 10 do
	waitForColour( whateverYourItemDetectorColourValueIs )
	pulseList()
end

Then one final loop lets us repeat the whole thing forever:

local function waitForColour( colour )
	while not rs.testBundledInput( "back", colour ) do   --# So long as the desired colour isn't active on the back...
		os.pullEvent("redstone")                     --# ... keep yielding (doing nothing) until a redstone state change occurs.
	end
end

local function pulse( colour )
	rs.SetBundledOutput("back", colour)
	sleep(0.5)
	rs.SetBundledOutput("back", 0)
	sleep(0.5)
end

local function pulseList()
	pulse(1)
	pulse(4)
	pulse(4)
	pulse(1)
	pulse(1)
	pulse(1)
	pulse(4)
end

while true do  --# A loop that repeats indefinitely.
	pulseList()  --# Run through the list once.

	--# Then do it another ten times:
	for i = 1, 10 do
		waitForColour( whateverYourItemDetectorColourValueIs )
		pulseList()
	end
	
	--# Wait for the colour that signals a restart:
	waitForColour( whateverYourOtherSpecialColourIs )
end
unter_hosen #3
Posted 23 September 2017 - 04:02 AM
Hey BB

Thanks so much for your detailed response. Hey, if there was a "kudos" button that I could press that gave you like 10 AUD I would click it. In all honesty, I have not had chance to test it all yet, mainly because I have been playing darts tonight and I am far too drunk to try and even load this up. I will give it a go on Sunday night. On a side note, if there is a donate button to the forum, please point me in the direction,.

However, thanks again, I am just blown away.

Unter
Lupus590 #4
Posted 23 September 2017 - 11:31 AM
Thanks so much for your detailed response. Hey, if there was a "kudos" button that I could press that gave you like 10 AUD I would click it.

http://www.computerc...utation-points/



On a side note, if there is a donate button to the forum, please point me in the direction.

This donate button is for the mod arthor not BB.
http://www.computercraft.info/donate/
Edited on 23 September 2017 - 09:34 AM
unter_hosen #5
Posted 26 September 2017 - 01:26 AM
Well i put 10 USD to the mod, hope it helps. Shame BB doesnt get it for putting the time in to explain the code so well.

Just one quick thing, how would I add something in that acts like a redstone timer, basically I want to continually pulse a retriever for an item, which is on the same pipe network as the item detector, but I only want that to happen during the for loop.

Thanks

Unter