Okay so I am rewordoing on a new system to do item counting (more specifically Ice … can you figure out what it's for yet?). Anyway, the computer is hooked up to one Bundled Cable that contains 7 cables in it.

The computer reads all of these, 4 of which are hooked up to my item detectors.

Well here's the problem, when testing the system to make sure the items are being counted correctly and the program works, I am running into the problem where the values that are being produced from the item detectors pulses in that sometimes the values are accuracter, sometimes they are double the value they should be. Here's the code I have for that computer


local powerStatus = false
local iceCheck = false
local powerOn = false
local iceCount = {}
local avgIceCount = {}
----------------------------------------------------------------------
function main()
 
for i = 1, 4 do
  iceCount[i] = 0
  avgIceCount[i] = 0
end

redstoneCheck()
 
while true do
  local event = os.pullEvent()
 
  if event == "timer" then
   iceAvgMath()
   display()
  end

  if event == "redstone" then
   redstoneCheck()
  end

end
end
----------------------------------------------------------------------
function redstoneCheck()
 
if rs.testBundledInput("right", colors.magenta) == false and powerOn == false then --if ice production is turned on (start timer)
  os.startTimer(60)
  powerOn = true
elseif rs.testBundledInput("right", colors.magenta) == true and powerOn == true then
  powerOn = false
end

if rs.testBundledInput("right", colors.lightBlue) == true then --if the ice check system fails (the item detector allowed the timer to expire)
  iceCheck = false
elseif rs.testBundledInput("right", colors.lightBlue) == false then
  iceCheck = true
end

if rs.testBundledInput("right", colors.lime) == true then --if the MFSU is empty
  powerStatus = false
elseif rs.testBundledInput("right", colors.lime) == false then
  powerStatus = true
end

if rs.testBundledInput("right", colors.lightGray) == true then --ice count for each snow golem (if the item detector triggers, increment 1)
  iceCount[4] = iceCount[4] + 1
end
if rs.testBundledInput("right", colors.cyan) == true then
  iceCount[3] = iceCount[3] + 1
end
if rs.testBundledInput("right", colors.purple) == true then
  iceCount[2] = iceCount[2] + 1
end
if rs.testBundledInput("right", colors.blue) == true then
  iceCount[1] = iceCount[1] + 1
end

display()
end
----------------------------------------------------------------------
function iceAvgMath()
 
if avgIceCount[1] == 0 and avgIceCount[2] == 0 and avgIceCount[3] == 0 and avgIceCount[4] == 0 then --if no avg is avaliable atm
  for i = 1, 4 do
   avgIceCount[i] = iceCount[i]
   --avgIceCount[i] = (iceCount[i] / 2)
  end
else --new average

  for i = 1, 4 do
   avgIceCount[i] = ((avgIceCount[i] + iceCount[i]) / 2)
   --avgIceCount[i] = ((avgIceCount[i] + (iceCount[i] / 2)) / 2)
  end
end

for i = 1, 4 do --resets values
  iceCount[i] = 0
end
 
if rs.testBundledInput("right", colors.magenta) == false then --reset the timer if the system is still running
  os.startTimer(60)
end
 
end
----------------------------------------------------------------------
function display()

term.clear()
term.setCursorPos(1,1)
 
print()
 
centerText("Ice Factory 1")
 
print()
print()
 
if powerStatus == true then
  centerText("Power Status = Online")
elseif powerStatus == false then
  centerText("Power Status = ERROR")
end
 
print()
 
if iceCheck == true and powerOn == true then
  centerText("Ice Production = Online")
elseif iceCheck == true and powerOn == false then
  centerText("Ice Production = Offline")
elseif iceCheck == false then
  centerText("Ice Production = ERROR")
end

print()
print()

centerText("Avg Creature 1 = "..avgIceCount[1])
print()
centerText("Avg Creature 2 = "..avgIceCount[2])
print()
centerText("Avg Creature 3 = "..avgIceCount[3])
print()
centerText("Avg Creature 4 = "..avgIceCount[4])

end
----------------------------------------------------------------------
function centerText(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2)-(text:len() / 2)), y2)
write(text)
end
----------------------------------------------------------------------
main()

Okay so as you see, in the iceAvgMath() function you see I have stuff like "avgIceCount = iceCount" and then "–avgIceCount = (iceCount / 2)".

The reason being is at first I thought I was geting the values right, but then I tested it again and the values were twice as large, so I put a divide by 2 in there and then it works properly, but then on another test the item counts were correct without dividing (I hope I just made sense there).

I have tried adding pulses to the lines to see if that would fix the problem and it does not.

I really don't want to implement a sleep call into the program, as this thing needs to be update in case one of the other redstone events is called.

So I am wondering if it's possible to find a way to produce the correct results everytime from the redstone pulses from the item detectors? (again hope this all makes sense, and thanks in advance for help)