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

Simple Counter

Started by civilwargeeky, 08 July 2012 - 10:32 PM
civilwargeeky #1
Posted 09 July 2012 - 12:32 AM
So I was attempting to make a simple counter that counted redstone pulses. I though this would work, but apparently I did something wrong.
I apologize in advance for a beginner's mistake

red = rs.getInput("back")
i = 0
while red == true do
i = i+1
term.redirect(peripheral.wrap(right))
term.clear()
term.setCursorPos(1,1)
print(i)
term.restore
term.clear()
term.setCursorPos(1,1)
print("The value is "..i)
end
BigSHinyToys #2
Posted 09 July 2012 - 01:02 AM
This should do it.


local i = 0 -- local varibles are best here
local bPast = false
local this = peripheral.wrap("right")
while true do
    os.pullEvent("redstone")
    local bInput = rs.getInput("back")
    if bInput and not bPast then
	    bPast = true
	    i = i+1
    elseif not bInput then
	    bPast = false
    end
    term.redirect(this)
    term.clear()
    term.setCursorPos(1,1)
    print(i)
    term.restore()
    term.clear()
    term.setCursorPos(1,1)
    print("The value is "..i)
end
MysticT #3
Posted 09 July 2012 - 01:02 AM
You never update the "red" variable, so the loop never ends. There's some other errors.
Try with this:

term.redirect(peripheral.wrap("right")) -- redirect output

local count = 0 -- initialize the count to 0
local lastInput = rs.getInput("back") -- variable to store last redstone input

while true do -- infinite loop
  term.clear() -- clear the screen
  term.setCursorPos(1, 1)
  print(count) -- print the current count
  os.pullEvent("redstone") -- wait for redstone change
  local input = rs.getInput("back") -- get redstone input
  if input ~= lastInput then -- check if it changed (to know if it's a pulse)
    if input then -- if it's on
	  count = count + 1 -- add one to the count
    end
    lastInput = input -- update the last input
  end
end
civilwargeeky #4
Posted 09 July 2012 - 01:36 AM
Thank you BigSHinyToys! Worked great. Also thank you MysticT for giving me that, but when I tried your code, I recieved "Bios:355: bad argument: string expected, got nil." I just thought you should know.
MysticT #5
Posted 09 July 2012 - 05:42 PM
You copied something wrong, cause there's no way that the string "right" is not a string. Maybe you forgot the quotes.
Drsirdeath #6
Posted 12 July 2012 - 01:04 AM
Now is there a way to make the computer set off an output of redstone to the left once it hits a certien number of counts?
BigSHinyToys #7
Posted 20 July 2012 - 10:43 AM
I have modified this to out after 5 the total that it outs at is variable.
Spoiler

local i = 0 -- local varibles are best here
local bPast = false
local this = peripheral.wrap("right")
local outSide = "left"
local outAt = 5 -- Number that it outputs at.
while true do
    os.pullEvent("redstone")
    local bInput = rs.getInput("back")
    if bInput and not bPast then
		    bPast = true
		    i = i+1
    elseif not bInput then
		    bPast = false
    end
    term.redirect(this)
    term.clear()
    term.setCursorPos(1,1)
    print(i)
    term.restore()
    term.clear()
    term.setCursorPos(1,1)
    print("The value is "..i)
    -- new lines
    if i == outAt then
	    rs.setOutput(outSide,true)
	    break -- leaves the loop
    end
    -- end new lines
end
term.restore()
term.clear()
term.setCursorPos(1,1)
print("Program Ended. Shuting down in 5 seconds")
sleep(5)
os.shutdown()