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

Having trouble getting a counting machine to work

Started by Vyvek, 01 October 2012 - 07:34 AM
Vyvek #1
Posted 01 October 2012 - 09:34 AM
Just picked up computer craft a few hours ago and figured out how to write on monitors, as that was my original purpose.

However, I found myself wanting to do more with it as its just too cool.

I searched the forums and found some programs and guidelines that would count redstone pulses from block detectors and display it like:
"1 cobblestone"
and each subsequent pulse would add a line under that as:
"2 cobblestone"
etc

While that's awesome, I want some static text on the monitor while just having a number value change on it.
For example a line I want to come up:

" Cobblestone [0/75]"

With the 0 counting up as that computer receives redstone signals. So after one cobblestone goes through, the same line rewrites itself to:

"Cobblestone [1/75]"

Using my limited knowledge, the program I came up with is:

local count = 0
local lastInput = false

while true do
term.setCursorPos(1,1)
write("Cobblestone [0/75]")
os.pullEvent("redstone")
local input = rs.getinput("<side redstone is coming from>")
if input ~= lastInput then
if input then
count = count + 1
term.clear()
term.setCursorPos(1,1)
write("Cobblestone ["..count"/75]")
end
end
end



When I run it though, an 11 comes up on my screen… so I'm failing pretty hard core at this and could use some help.
jag #2
Posted 01 October 2012 - 09:59 AM
At the bottom where you got
write("Cobblestone ["..count"/75]")
I think you forgot another double dots.

So it should be like this
write("Cobblestone ["..count.."/75]")
Vyvek #3
Posted 01 October 2012 - 10:08 AM
Heh, thanks for your input.

More seems to be wrong though as I run the program and "9" pops out now.
jag #4
Posted 01 October 2012 - 10:13 AM
And why are you doing
os.pullEvent("redstone")

and at the row below, you have the function rs.getInput spelled wrong. You spelled it with a lower case 'i'.
jag #5
Posted 01 October 2012 - 10:32 AM
When I tried your code and put a button in front of it and pressed the button, the 'counter' went up to 1 and then down to 0 again.
So I just rewrote your code:
Spoiler
local count = 0
term.clear()
term.setCursorPos(1,1)
print("Cobblestone ["..count.."/75]")

while true do
  if rs.getInput("front") and count < 75 then
	count = count + 1
    term.clear()
	term.setCursorPos(1,1)
	print("Cobblestone ["..count.."/75]")
	repeat sleep(.2) until rs.getInput("front") == false
  end
  sleep(.2)
end
Aslo on pastebin: FXA6B1Dm
Vyvek #6
Posted 01 October 2012 - 10:44 AM
Oh, thanks a ton, as that is exactly what I was looking for.

I see what I did wrong now.