Pages: [1]   Go Down
Author Topic: Possible use of function.  (Read 150 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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
Logged

Offline Offline
Edison Member
*
Karma: 41
Posts: 1283
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: August 04, 2014, 10:41:23 pm by Peter_n » Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Tesla Member
***
Karma: 134
Posts: 7594
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 116
Posts: 4306
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 104
Posts: 4631
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

UK
Offline Offline
Tesla Member
***
Karma: 134
Posts: 7594
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 104
Posts: 4631
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Pages: [1]   Go Up
Jump to: