Hi We are using counters with switches on digital inputs to check for rapid changes in the switch. We would like the counter to count up when the switch goes from unpressed to pressed and count down (to negative numbers) when it goes
from pressed to unpressed. So far the counter appears to only count up on either state change.
Any thoughts about how to get it to count down when the switch is released?
If you want a switch that counts both up and down, you really need something like a quadrature encoder, or a gray code encoder. Either of these has at least two separate switches. The simplest quadrature encoder is for rotation, and has one switch “on” for angles between 0 and 180 degrees, “off” for 180 to 360. The other switch is rotated 90 degrees, so it will be “on” for 90 to 270 degrees, and “off” for 270 through 0 to 90 degrees. With this sort of setup, you can tell which way the wheel is rotating based on which switch is “leading” the other towards each state. If the first switch “leads”, rotation is in the positive direction, if the second switch “leads”, rotation is in the negative direction.
As GeeTwo stated, something more is really needed for what you are saying, it is possible to either put two switches, or the encoders that he suggested.
I am attaching a snippet that would count up and down as described, - note that it will go 1 or 0 always.
I am also attaching a snippet of what it could look like with the second switch.
I don’t have access to LabVIEW for FRC right now, so I am posting a vi that could have the switch read wired into it. I have made it in the form of an FGV, but the point is just the memory of the last value should be used (through either shift register or feedback node)
Not to be rude, but this is the Java sub-forum, so some who do not have experience with LabVIEW may not be able to make heads or tails of the code you posted. If I get time I may be able to translate what you posted into Java pseudocode though:) .
Thanks for pointing that out, Zaque. I guess I got a little ahead of myself. This is still a generic counter utility based on edges of a boolean input that could come from a DIO read, but I am sure there are other ways (like the one posted by jhersh)
To accomplish an equivalent of what I posted in Java, it would require static variables. i.e.
Thanks everyone for all the replies. The Java code that sets the up and down source makes the most sense to me so will try that first. I’ll report back my findings.