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

[SOLVED]Button Press Counter

Started by Domo, 12 January 2013 - 03:48 AM
Domo #1
Posted 12 January 2013 - 04:48 AM
Hey All,

First off, I want to thank all of you. I've been a long time lurker on these boards and the discussions on these forums have really helped.

I'm having an issue that seems like it should be very simple, but I can't seem to find an answer anywhere else. I'm testing a simple code to count button presses and it always seems to be off.


local i = 0
while true do
	os.pullEvent("redstone")
	i = i + 1
	print(i)
end

Not sure if this matters, but the 'physical' layout of wires is a button connected to a red alloy wire that connects to an insulated wire and then a bundled cable which connects to the computer's right side.

Am I overlooking something obvious or does the built in delay in the buttons screw up this possibility? I really need to be able to reliably register each of the user's button presses. I'd rather the user didn't have to wait for the button to pop back out each time.

Thanks for your help,

Domo
zekesonxx #2
Posted 12 January 2013 - 04:58 AM
This isn't possible due to the button's long holding time when pressed.

And what you have there will count both when the redstone turns on, AND when it turns off.
You need to do rs.getInput() so you can see whether or not it is actually turning on.
Alekso56 #3
Posted 12 January 2013 - 05:01 AM

local i = 0
while true do
	os.pullEvent("redstone")
	i = i + 1
	print(i)
end
You have to have an IF so it can know when and why it should do stuff.
i = 0
while true do
  os.pullEvent("redstone")
  term.clear()
  term.setCursorPos(1,1)
  if redstone.getInput("right") then
	i = i + 1
	print(i)
  end
end
zekesonxx #4
Posted 12 January 2013 - 05:02 AM
You pasted in already colored code.
 doesn't parse BB code, and you screwed youtself up.

Right-click and select "Paste as plain text"
Alekso56 #5
Posted 12 January 2013 - 05:08 AM
You pasted in already colored code.
 doesn't parse BB code, and you screwed youtself up.

Right-click and select "Paste as plain text"

i edited it…. and also *yourself
Domo #6
Posted 12 January 2013 - 05:14 AM
zekesonxx:

I really hope that's not the case… i feel like this can be done. It seems to be registering multiple presses, but that might just be the on and off of the button as I click it multiple times…

Alekso56:

I wasn't really concerned with resetting the terminal as this is just a test code that I'll use elsewhere later. I modified your code to incorporate the bundled cable and tried it, but this definitely doesn't register the multiple button presses


local i = 0
while true do
  os.pullEvent("redstone")
  if rs.testBundledInput("right", colors.orange) then
	i = i + 1
	print(i)
  end
end

Any other thoughts?
ChunLing #7
Posted 12 January 2013 - 05:19 AM
If you used a switch rather than a button, this would neatly solve both problems at once.
Domo #8
Posted 12 January 2013 - 05:22 AM
Yeah, I just tried that and it works perfectly. I really had wanted to use a button for aesthetic purposes, but I guess that's not a big deal.

Thanks everybody!