Go Down

Topic: Possible use of function. (Read 180 times) previous topic - next topic

jarrod0987

I am working with debouncing of simple switches and am wondering if I can make a simple debounce function that can just be easily applied to any button without the function having to be relabeled and rewritten over and over.

This is what I currently have:

Code: [Select]

boolean debounceUp(boolean last)
{
  boolean current = digitalRead(switchPinup);
  if (last != current)
  {delay(5);
  current = digitalRead(switchPinup);
  }
  return current;
 



The problem is if I want to add another switch I have to write the same Function again to reference a different switch pin etc.
Is there way to give the function the ability to take multiple inputs at the same time and give multiple outputs? Such as different debounced button states? The purpose is just to simplify the code for 8 - 10 buttons which need debouncing.


Thank You

Peter_n

#1
Aug 05, 2014, 05:37 am Last Edit: Aug 05, 2014, 05:41 am by Peter_n Reason: 1
Have a look at this : http://www.adafruit.com/blog/2009/10/20/example-code-for-multi-button-checker-with-debouncing/

But you can avoid such code in your sketch and use a beatiful library instead : http://playground.arduino.cc/code/bounce

jarrod0987

I will have a look. Thank You. I would like to use more Libraries and less reinventing the wheel :)

Robin2

Is this a real problem or a hypothetical problem?

I understand very well what switch bounce is. But I don't recall ever needing to write code specially to deal with it and in many of my test projects the switches are very poor - often no more than touching two pieces of wire.

There is a nice philosophy "if it works, don't fix it."

...R

UKHeliBob

Quote
The problem is if I want to add another switch I have to write the same Function again to reference a different switch pin etc.
Is there way to give the function the ability to take multiple inputs at the same time and give multiple outputs? Such as different debounced button states? The purpose is just to simplify the code for 8 - 10 buttons which need debouncing.

Put the switch pins, last and current  in arrays then you only need to pass the index to the arrays.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

JimboZA


in many of my test projects the switches are very poor - often no more than touching two pieces of wire.


It wouldn't surprise me if touching two wires together is actually less bouncy than a crap switch. If one pushed them together firmly enough there will likely be only one contact counted. Otoh when I tested one of these, I was consistently getting  2 for 1 and decided to use the bounce2 library.
Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

Robin2


It wouldn't surprise me if touching two wires together is actually less bouncy than a crap switch. If one pushed them together firmly enough there will likely be only one contact counted.


I haven't tested with an oscilloscope but I suspect my wires bounce a lot. It is rare that I bother to push them together firmly. But since my code only reads the value once before going off to do something it doesn't matter how many other contacts there may be.

I am not claiming that bouncing never causes a problem. I'm just saying don't create a fix if it is not needed.

...R

JimboZA

Quote
my code only reads the value once before going off to do something it doesn't matter how many other contacts there may be.


Good point: the example I had in mind- and for which I tested that switch- does need to know there's only one click. It simulates a coin drop at a turnstyle, so a second click would register as a second payment.
Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

Go Up