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

[question]Count redstone impulses

Started by happy2pester, 10 March 2012 - 01:15 PM
happy2pester #1
Posted 10 March 2012 - 02:15 PM
Okay, so I'm working on a program, and I need a little help. I need this function to count any pulse on the RP2 Gray line within a minute. If the count reaches 8, then the function turns the yellow line on, and then off again a moment later. If the count does not reach 8 with that minute, then the function calls another function called bonespawn(), before resuming the count for another minute.

I've tried a few things, and can't get this to work. Any help?
Sebra #2
Posted 10 March 2012 - 02:44 PM
Any result of your efforts?
It seems you want other people do it for you.
happy2pester #3
Posted 10 March 2012 - 02:55 PM
Two seperate attempts here, neither of which worked.


function Redcount()
for i=1,60 do
  if rs.getBundledInput("back",pink)==32 then
   print(x)
   print("math")
   x=x+1
   print(x)
   sleep(0.375)
  end
end
end
function Redcount()
for i=1,30 do
  if rs.setBundledOutput("back",0)
   x=x+1
   print(x)
   sleep(0.375)
  end
end
end
Liraal #4
Posted 10 March 2012 - 04:37 PM
try this:

function counter()
for i=1,60,1 do
if rs.testBundeldInput("back", colors.gray) then

print(x)
print("math")
x=x+1
print(x)
sleep(0.375)

end
end
happy2pester #5
Posted 10 March 2012 - 09:13 PM
Alright, I'm really struggling with this. I need to get to get this function to listen on the gray line for 30 seconds, and count all the pulses during that time. There isn't a set number that it's counting to which is the problem - I could handle that. If it doesn't get to 8 within that 30 seconds, then it's to run a function called bonespawner, and if it does, then it just ends and the programs goes off to do something else. Can anyone help?
Liraal #6
Posted 10 March 2012 - 10:28 PM
Try this then:


local fixedvalue=8

function counter()
for i=1,30,1 do
if rs.testBundeldInput("back", colors.gray) then

print(x)
print("math")
x=x+1
print(x)

sleep(0.375)
end
if x<fixedvalue then bonespawner() end
end
Espen #7
Posted 10 March 2012 - 11:45 PM
Hey happy2pester,
I gave it a try, too. Here's the function code:

-- sSide - The side of the computer/turtle that is to be checked.
-- nColor - The color value to be checked for.
-- nTimeframe - The duration (in seconds) for how long the bundled input is being checked for pulses.
-- nTargetPulseCount - The number of pulses that have to be reached within the given time frame for the function to return true.

function checkPulse( sSide, nColor, nTargetPulseCount, nTimeframe )
  local nCounter = 0  -- Initialize pulse counter.
  local listenTimer = os.startTimer( nTimeframe )  -- Start timer.

  -- Event Loop.
  while true do
	local sEvent, param1 = os.pullEvent()  -- Wait for an event.
	
	if sEvent == "redstone" and rs.getBundledInput( sSide ) ~= nColor then
	  print( "Color turned OFF" )  -- DEBUG-OUTPUT
	end
	
	if sEvent == "redstone" and rs.getBundledInput( sSide ) == nColor then
	  nCounter = nCounter + 1
	  print( "Color turned ON, Count: "..nCounter )  -- DEBUG-OUTPUT
    end
	
	if sEvent == "timer" and param1 == listenTimer then
	  if nCounter < nTargetPulseCount then  -- Compare the amount of counted pulses with the desired target count.
		return false  -- Counted pulses were less than desired.
	  else
		return true  -- Counted pulses were at least as much as desired.
	  end
	end
	
  end
end

And here's an example with which you can test it.
The first line is basically like saying: "Check on the left side of the computer/turtle if the gray signal is pulsed at least 3 times within 10 seconds."

local bResult = checkPulse( "left", colors.gray, 3, 10 )
print( "The number of pulses were reached: "..tostring(bResult) )  -- Prints "true" or "false" depending on the outcome of checkPulse().

I have one question though:
Do you want to check if the bundled cable is set to color gray? Or if the color is part of of the cable (i.e. is only one of the colors that run through the cable)?
At the moment I check only if the comblete cable is set to the desired color. But if you acutally want the latter, then let me know and I'll gladly change it (or just add it as an additional option, so one can switch between the modes).

Cheers :mellow:/>/>
Edited on 10 March 2012 - 11:10 PM
happy2pester #8
Posted 11 March 2012 - 12:33 AM
Oh flipping GODDAMIT that is annoying.

I just finished ripping up part of my machine and rewiring it to include a physical counting system controlled by another terminal. That is annoying. If only i'd checked this thread in the last hour.

Ah well, if this doesn't work, I'll put it back. Thank you very much Espen for your help anyway