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

[Question][Solved] Working with Red power bundled cable and Item Detectors. A counting machine.

Started by _zael, 26 October 2012 - 03:36 PM
_zael #1
Posted 26 October 2012 - 05:36 PM
Hello.

I have been trying to find some information on working with bundled cable and item counters for my sorting machine.
I have written a program that will wait for a redstone event and then check which colour (or colours) are active in the bundled cable.
The program will then increment the count for the corresponding item in the table.

This all works fine when the system is sorting slowly, and the pulses are infrequent.
Slow Count Pulse 5 Copper, 5 Tin

However when a whole stack of items go through the item detectors and the the item counters continuously pulse 30+ times the program starts double counting items.
Fast Count Pulse 32 Copper, 32 Tin, 32 Iron

Is this because the redstone event is triggering for both the on and off states of each colour?

Here is my Bundled Cable Layout, each item detector has a separate colour.

Any assistance in finding the cause of this would be appreciated

This is what I have written so far for testing.


side = "right"  -- bundled cable side

Items = {
	{name = "Dirt", cable = colours.white, count = 0},
	{name = "Coal", cable = colours.black, count = 0},
	{name = "Cobblestone", cable = colours.lightGray, count = 0},
	{name = "Copper", cable = colours.gray, count = 0},
	{name = "Redstone", cable = colours.red, count = 0},
	{name = "Lapis", cable = colours.green, count = 0},
	{name = "Nikolite", cable = colours.blue, count = 0},
	{name = "Silver", cable = colours.yellow, count = 0},
	{name = "Gold", cable = colours.lightBlue, count = 0},
	{name = "Uranium", cable = colours.magenta, count = 0},
	{name = "Iron", cable = colours.lime, count = 0 },
	{name = "Glowstone", cable = colours.brown, count = 0},
	{name = "Tin", cable = colours.pink, count = 0},
	{name = "Log", cable = colours.cyan, count = 0},
	{name = "Rubber", cable = colours.orange, count = 0},
	{name = "Diamond", cable = colours.purple, count = 0}
}

function counter()
local input = rs.getBundledInput(side)  -- grab cable state
	for i = 1, 16, 1 do
		if colours.test( input, Items[i].cable) then  --check each colour for active
			Items[i].count = Items[i].count + 1  -- increase count for active colour
			print(Items[i].name .. " - " .. Items[i].count)
		end
	end
end

--Main
repeat
	event, param1, param2 = os.pullEvent() --wait for event
	if event == "redstone" then
		counter()
	end
until event == "char" and param1 == "x"
Ditto8353 #2
Posted 26 October 2012 - 05:44 PM
Using Dirt and Coal for the example:
A Dirt block is counted, causing white to go high.
The program counts the dirt.
Directly afterwards a Coal is counted, causing black to high.
The program goes to count the coal BEFORE white goes low again.
The program double-counts the previous Dirt block.
White goes low, raising a redstone event BEFORE black goes low.
The program double-counts the previous Coal.
Ditto8353 #3
Posted 26 October 2012 - 05:50 PM
As for solving it…
You could store the previous state of each wire in your Items table.
initialize all to 0
On redstone event newWireStates = wire states as measured
If newWireState > oldWireState then count it
oldWireStates = newWireStates
_zael #4
Posted 26 October 2012 - 06:41 PM
Hi Ditto.

Thank you very much for the response.

So it was the events for on and off causing the double count. I see.

Your solution works perfectly.


Here is the modified code for anyone searching with the same issue.

side = "right"  -- bundled cable side
detector = {
	{name = "Dirt", cable = colours.white, state = 0, count = 0},
	{name = "Coal", cable  = colours.black, state = 0, count = 0},
	{name = "Cobblestone", cable = colours.lightGrey, state = 0, count = 0},
	{name = "Copper", cable = colours.grey, state = 0, count = 0},
	{name = "Redstone", cable = colours.red, state = 0, count = 0},
	{name = "Lapis", cable = colours.green, state = 0, count = 0},
	{name = "Nikolite", cable = colours.blue, state = 0, count = 0},
	{name = "Silver", cable = colours.yellow, state = 0, count = 0},
	{name = "Gold", cable = colours.lightBlue, state = 0, count = 0},
	{name = "Uranium", cable = colours.magenta, state = 0, count = 0},
	{name = "Iron", cable = colours.lime, state = 0, count = 0 },
	{name = "Glowstone", cable = colours.brown, state = 0, count = 0},
	{name = "Tin", cable = colours.pink, state = 0, count = 0},
	{name = "Log", cable = colours.cyan, state = 0, count = 0},
	{name = "Rubber", cable = colours.orange, state = 0, count = 0},
	{name = "Diamond", cable = colours.purple, state = 0, count = 0}
}

function counter()
local input = rs.getBundledInput(side)  -- grab cable state
	for i = 1, 16, 1 do
		if colours.test(input, detector[i].cable) and detector[i].state == 0 then  --check each colour for active and has not been active on last check.
             detector[i].count = detector[i].count + 1 -- increase count for each active colour
             detector[i].state = 1 -- sets active cables to on state
             print(detector[i].name .. " - " .. detector[i].count)
		else
             detector[i].state = 0 -- set inactive cables to off state
        end
	end
end

--Main
repeat
	event, param1, param2 = os.pullEvent()
	if event == "redstone" then
		counter()
	end
until event == "char" and param1 == "x"
Ditto8353 #5
Posted 26 October 2012 - 07:05 PM
I'm glad I could help, but there may be a more efficient solution, so don't hesitate to get creative.