Skip to main content

Reply to "Using an optocoupler to connect insulated rail to input pin of shift register"

A couple of thoughts regarding debouncing for those who are just learning about this.  If your code is not time-critical, you can use software to just loop for 20ms or so after it sees a contact closure, before moving along with whatever your code needs to do.  If your code *is* very time critical -- i.e. you don't want to use "blocking" code that holds up other operations while you sit around waiting for a switch to stop bouncing -- you can still do this in code using timers.  There are a ton of good tutorials online that you can use to learn about how to do this.

Bear in mind that every switch bounces differently -- 20ms may be plenty for some switches, but others may bounce longer than this, and some may seem to never bounce.  It's easy enough to write a little loop of code that will help you tweak your bounce timer, or if you have an oscilloscope, you can watch the switch bounce and see how long it takes, and adjust your code accordingly.  Of course allow for plenty more than the longest bounce time that you observe.

Also note that a switch can appear to "bounce" when it releases.  When the contacts separate, there is a moment when they are coming apart that can be read as open at one instant and closed at another instant and then open again.  This happened to me, so in my case I simply put a 20ms delay once I saw a pushbutton being released, to give it time to fully release before moving on with my code.

Also bear in mind that you can monitor for contact closures via "polling" where you keep looking at an Arduino (or shift register) input within a loop and wait for it to go low and then call a function to deal with it, or you can use an Arduino interrupt to automatically call a function when an input state changes -- but it's tricky to do software debouncing within an Interrupt Service Routine because you can't use the delay() command in an ISR -- generally you just set a flag that your code will notice has been set at some point.  Interrupts are best avoided by beginners unless you are really interested in learning about them.

The way that you handle bounce is a factor of how important it is for your code to keep going (to not "block" by using delays), and how willing you are to learn some new programming techniques.  If you set up a little proof-of-concept pushbutton (or relay) and small piece of test code, it's fun to experiment and you'll learn a lot in the process, as I did.

Randy

OGR Publishing, Inc., 1310 Eastside Centre Ct, Suite 6, Mountain Home, AR 72653
800-980-OGRR (6477)
www.ogaugerr.com

×
×
×
×
×