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

Lua Program for a "Increment Gate"like program

Started by FZ_R3dst0n3r12, 18 February 2013 - 04:56 PM
FZ_R3dst0n3r12 #1
Posted 18 February 2013 - 05:56 PM
Title: Lua Program for a "Increment Gate"like program
I'm currently having issues on my program on having a variable increase from a value of 0 to infinity as long as a redstone pulse is detected. So it works by detecting a redstone pulse then adds 1 to the value but I'm encountering errors about syntax and my loop. May someone please post a sample code I could follow? It must also display the variable on a monitor with the text "Days:"Variable". Thanks!
Lyqyd #2
Posted 19 February 2013 - 07:30 AM
How often are you trying to poll the input to see if it's still on? You were probably looping too fast. I would expect you'd want to keep track of the time it was on, rather than just incrementing a counter while it was on.


local side ="back"
local prevState = rs.getInput(side)
local startTime, totalTime = nil, 0
if prevState then startTime = os.clock() end
while true do
  os.pullEvent("redstone")
  if prevState ~= rs.getInput(side) then
    prevState = rs.getInput(side)
    if prevState then
      startTime = os.clock()
    else
      totalTime = os.clock() - startTime + totalTime
      term.clear()
      term.setCursorPos(1,1)
      print(totalTime)
    end
  end
end

That should get you started in the right direction.
FZ_R3dst0n3r12 #3
Posted 28 February 2013 - 07:06 PM
I was trying to poll a pulse per tick.The program above pulls from the computer time because of the os.clock() function? I was trying to emulate redpower's counter so that whenever the computer "gets" a pulse, it increments a counter that will be displayed to a wrapped peripheral(A Monitor).
Doyle3694 #4
Posted 28 February 2013 - 07:23 PM
The real magic is os.pullEvent("redstone"), which will wait for a redstone change