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

Redstone Pulse Counter

Started by jga620, 29 June 2012 - 07:21 PM
jga620 #1
Posted 29 June 2012 - 09:21 PM
How do I make a redstone pulse counter?
Thanks
MysticT #2
Posted 29 June 2012 - 10:35 PM
Well, what have you done so far? If you don't try to do it yourself you won't learn anything.
Try reading about the rs api and events.
infopointonline #3
Posted 24 July 2012 - 03:22 AM
Really Need Help On This! I am Building a Bank System that when the pulse counter has not received a signal in 5 seconds, it will take that total and add it to the customers bank balance. Thus, not in loop. Even Though Some Don't Want To Write Code, This Is Last Step I Have To Complete My Banking Program in combination with Redpower 2 Machines! I Dont know where to start! Please HELP!
Noodle #4
Posted 24 July 2012 - 03:34 AM
A pulse counter


while true do
  if rs.getInput(side) then
    rsc = rsc + 1 -- RSC = Red Stone Counter
  end
  if rsc == 3 then
    print("alert") -- Something like that.
  end
end
infopointonline #5
Posted 24 July 2012 - 03:53 AM
A pulse counter


while true do
  if rs.getInput(side) then
	rsc = rsc + 1 -- RSC = Red Stone Counter
  end
  if rsc == 3 then
	print("alert") -- Something like that.
  end
end
I might be able to expand on that, My program is based on using arithmetic to add and subtract on a .dat file, It might be possible that every-time a signal is sent it will add +1 to the dat file like

whiletruedo
if rs.getInput("top")then
local file = fs.open("placeholder.dat", "r")
local text = fs.readAll()
file = io.open("Placeholder.dat", "w")

add = 1
file:write(text+add)
file:close()
else
print("No Credits Were Received!")

end

UPDATED : NO LUCK WITH ANY VERSION OF CODE SO FAR! STILL IN NEED OF HELP!
infopointonline #6
Posted 25 July 2012 - 06:20 PM
Still No Luck, I Need A Program That waits to receive pulses, then Counts The Pulses and stops counting after 5 sec of no pulses(Dont Know How To Code, Adds them to one unified number(dont know how to code, then i need to take that number and add it to the .dat file( This Part Is Easy For Me)! Please Help Lua Gods!
Lyqyd #7
Posted 25 July 2012 - 06:52 PM
Call this as pulseTotal = pulseCount("right") or whatever side it should be, pulseTotal would have the total pulses.


local function pulseCount(side)
 local pulses = 0
 local rsState = false
 local pulseTimeout = os.startTimer(5)
 while true do
  local e, p = os.pullEvent()
  --If the timer is up, break the loop.
  if e == "timer" and p == pulseTimeout then
   break
  elseif e == "redstone" then
   --If the redstone input on the specified side has changed, update the state variable.
   if rs.getInput(side) ~= rsState then
    rsState = rs.getInput(side)
    --If the input just went low, the pulse just ended. Increment the counter.
    if rsState == false then
     pulses = pulses + 1
    end
   end
  end
 end
 return pulses
end
infopointonline #8
Posted 26 July 2012 - 01:45 AM
Call this as pulseTotal = pulseCount("right") or whatever side it should be, pulseTotal would have the total pulses.


local function pulseCount(side)
local pulses = 0
local rsState = false
local pulseTimeout = os.startTimer(5)
while true do
  local e, p = os.pullEvent()
  --If the timer is up, break the loop.
  if e == "timer" and p == pulseTimeout then
   break
  elseif e == "redstone" then
   --If the redstone input on the specified side has changed, update the state variable.
   if rs.getInput(side) ~= rsState then
	rsState = rs.getInput(side)
	--If the input just went low, the pulse just ended. Increment the counter.
	if rsState == false then
	 pulses = pulses + 1
	end
   end
  end
end
return pulses
end
Did not work, give me a error about needing another "end" in the coding and when i did it, it just blinked once and broke! The Program Did not run at all. If Anyone else wants to try in the actual game that would be helpfull
Lyqyd #9
Posted 26 July 2012 - 02:55 AM
Call this as pulseTotal = pulseCount("right") or whatever side it should be, pulseTotal would have the total pulses.


local function pulseCount(side)
local pulses = 0
local rsState = false
local pulseTimeout = os.startTimer(5)
while true do
  local e, p = os.pullEvent()
  --If the timer is up, break the loop.
  if e == "timer" and p == pulseTimeout then
   break
  elseif e == "redstone" then
   --If the redstone input on the specified side has changed, update the state variable.
   if rs.getInput(side) ~= rsState then
	rsState = rs.getInput(side)
	--If the input just went low, the pulse just ended. Increment the counter.
	if rsState == false then
	 pulses = pulses + 1
	end
   end
  end
end
return pulses
end
Did not work, give me a error about needing another "end" in the coding and when i did it, it just blinked once and broke! The Program Did not run at all. If Anyone else wants to try in the actual game that would be helpfull

Works fine here. Added another line to fix a slight bug in it. If you can't get it to work on your end, that's not my problem.


local function pulseCount(side)
    local pulses = 0
    local rsState = false
    local pulseTimeout = os.startTimer(5)
    while true do
        local e, p = os.pullEvent()
        --If the timer is up, break the loop.
        if e == "timer" and p == pulseTimeout then
            break
        elseif e == "redstone" then
            --If the redstone input on the specified side has changed, update the state variable.
            if rs.getInput(side) ~= rsState then
                pulseTimeout = os.startTimer(5)
                rsState = rs.getInput(side)
                --If the input just went low, the pulse just ended. Increment the counter.
                if rsState == false then
                    pulses = pulses + 1
                end
            end
        end
    end
    return pulses
end
infopointonline #10
Posted 26 July 2012 - 03:44 AM
Finaly Got This To Work For My Program, Not Exactly Like I wanted but it does solve the main goal of my program! Take a Look

print("Get Ready To Deposit Credits!")
print("What is The Planned Deposit Amount?")
write("$")
depositamount = read()
print("Deposit Now, You Have The Following Seconds To Deposit!"..depositamount)
sleep(5)
local timer1 = os.startTimer(depositamount)
credits = 0
pulsestate = true
while true do
local receive, timeout = os.pullEvent()
if receive == "timer" and "timeout" then
print("Done Reciving Credits! Timed out!")
break
else
if receive == "redstone" then
if rs.getInput("top") ~= pulsestate then
pulsestate = rs.getInput("top")
if pulsestate == true then
credits = credits+1
end
end
end
end
end
print(credits)
sleep(1)
local file = fs.open("example.dat", "r")
if file then
local text = file.readAll()
print("Balance Before Is $"..text)
file.close()
else
print("File Dosen't Exist!")
end
sleep(1)
local file = fs.open("example.dat", "r")
local text2 = file.readAll()
file = io.open("example.dat", "w")
file:write(text2+credits)
file:close()
print("Completed!")
local file = fs.open("example.dat", "r")
local text3 = file.readAll()
print("The Final Balance Is $"..text3)
sleep(5)
term.clear()
term.setCursorPos(1,1)
shell.run("program")
end
polkinghornbd #11
Posted 04 February 2013 - 05:55 AM
would this work for a counter for say the number of people who went over a pressure plate?
Kingdaro #12
Posted 04 February 2013 - 06:02 AM
would this work for a counter for say the number of people who went over a pressure plate?
I'm just going to drop a new code because I don't feel like checking theirs.


local count = 0
local side = 'left'
while true do
  term.clear()
  term.setCursorPos(1,1)
  print('People passed: '..count)
  os.pullEvent('redstone')
  if rs.getInput(side) then
    count = count + 1
  end
end

Side is the side of the pressure plate / redstone signal.