Posted 29 June 2012 - 09:21 PM
How do I make a redstone pulse counter?
Thanks
Thanks
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 likeA pulse counterwhile 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
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 helpfullCall 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 helpfullCall 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
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
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
I'm just going to drop a new code because I don't feel like checking theirs.would this work for a counter for say the number of people who went over a pressure plate?
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