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

EMC- Counting redstone pulse

Started by Jester, 24 December 2015 - 09:24 PM
Jester #1
Posted 24 December 2015 - 10:24 PM
Hi

I'm making an emc/item counter with CC and redstone pipes.

One pipeline is for the MK1(left side)
The other pipeline is for the MK2 (right side)
I have 2 redstone pipes which are connected on the pipeline of the EMC-Flowers.
Like this a redstone signal will be pulsed when an item is in the redstone pipe.

The Redstone pulse is read by the computer. It counts the amount of items flowing through it. –(this happens on both sides)
Now I want to make a pulse counter on CC.

This is my code, but whenever an item passes through the redstone pipe, it counts for 2 points and not 1.

Also look at the photo of the setup


os.pullEvent = os.pullEventRaw
local pulses = 0
while true do
local event,p1 = os.pullEvent()
if event == "redstone" then
pulses = pulses + 1
end
term.clear()
term.setCursorPos(1, 1)
term.write("Counter")
term.setCursorPos(9, 1)
term.write(pulses)
end
LBPHacker #2
Posted 24 December 2015 - 10:46 PM
That's because a redstone event fires when a redstone input turns on and when one turns off. Check if the input is on after receiving the event, only increment the counter if it is.
Edited on 24 December 2015 - 09:47 PM
Jester #3
Posted 24 December 2015 - 10:49 PM
Yeah, How do I fix it in the code ?
LBPHacker #4
Posted 24 December 2015 - 10:55 PM
Well, show us what you tried to fix it. How would you check if the input is on? I'll give you a hint, check the redstone API page and see if you can find something there that reads input states.
Jester #5
Posted 25 December 2015 - 08:34 PM
Like this ?


os.pullEvent = os.pullEventRaw
local pulses = 0
local rsState = false
while true do
if rs.getInput(left) = rsState then
rsState = rs.getInput(left)
if rsState == false then
pulses = pulses + 1
end
end
end
end
return pulses
term.clear()
term.setCursorPos(1, 1)
term.write("Counter")
term.setCursorPos(9, 1)
term.write(pulses)
end
end
end
Edited on 25 December 2015 - 07:35 PM
LBPHacker #6
Posted 25 December 2015 - 09:21 PM
I have not the slightest idea what you did with those ends, but the part that increments pulses on the falling edge of the redstone signal should work just fine.

EDIT: You should have kept the os.pullEvent call though. Without that, your program never yields and you get a yield timeout crash.
Edited on 25 December 2015 - 08:23 PM