Go Down

### Topic: Trying to solve debounce problem (Read 2327 times)previous topic - next topic

#### himfire

##### Feb 27, 2013, 01:40 pm
Hi ,

I'm trying to create a library , I'm trying to solve the debounce problem , when it get an input it add to the counter +1 until it reach the specific number +5 then it will say the button is pressed else , the button is not pressed and reset the counter 0 , the problem I think it's on the definition , I have tried to use with the serial monitor , to get an input to use it with a led on pin 13 ,

Code: [Select]
`/* debounce.cpp - Library for solving debounce problem Created by Hisham Alfadel , February 26 , 2013 Released for the public usage and I hope you enjoy with it*/int debounce::reset(int M){  //Return the counter to B = 0 and LED switch it off  Serial.println("B = 0");  digitalWrite(ledPin,LOW);  M = 0;return M ;}int debounce::button_state(int ledPin1 , int button , int H ){  //check the for HIGH   if(button == '1'){    //Serial.println("I'm here" + B );    //check the number is less than 6    if(H < 6){      H = H + 1;     Serial.println("High");     }  }  if (button == '0'){    H = reset(H);    Serial.println("Reset");    Serial.println(H);  }    return H;}`
Code: [Select]
`/*bounce.h - Library for solving debounce problemCreated by Hisham Alfadel , February 27 , 2013Released for the public usage and I hope you enjoy with itThis code is used to solve the problem of the debounce situationthe code will use the logarithm of :" If it's high add 1 to counteruntil it's rised to Five else fall down to Zero */#ifndef debounce_h#define debounce_h#include "Arduino.h"//Definitions are placed hereclass debounce{public:debounce( int ledPin1 , int button , int H);int B = 0; //This is used for the increamentunsigned int D = 0; //This is for the serial output }#endif`

Code: [Select]
`#include <debounce.h>const unsigned int ledPin= 13;unsigned int button_try = 0 ;int B = 0;void setup() {  // put your setup code here, to run once:  Serial.begin(9600);  pinMode(ledPin,OUTPUT);  }void loop() {  // put your main code here, to run repeatedly:   button_try=Serial.read();  B=debounce.button_state(ledPin,button_try,B);    //then should put here if statement to get it right  if(B == 5){      digitalWrite(ledPin1,HIGH);      }}`

#### himfire

#1
##### Feb 27, 2013, 01:43 pm
it say when I want to compile :

unsigned int button_try = 0 ;//for this line in the last code

new type may not be defined in a return type

#### PaulS

#2
##### Feb 27, 2013, 01:50 pm
Your class, debounce, does not have a reset() method OR a button_state() method.

You never create an instance of the class to call methods on.

You don't seem to understand that C (and C++) is a call by value language. That is, arguments to functions may be changed by the function, but the caller will have no idea that the function may have changed its copy of the value.

#### HazardsMind

#3
##### Feb 27, 2013, 01:53 pmLast Edit: Feb 27, 2013, 01:55 pm by HazardsMind Reason: 1
Quote
int debounce::button_state(int ledPin1 , int button , int H ){
this doesn't look right, button should be a char or a byte, not an int. My reason, this line
Quote
if(button == '1'){
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

#### holmes4

#4
##### Feb 27, 2013, 01:57 pm
You also need to look at the way button are debounced. See the example in the playground or in the examples code.

Mark

#### JoshSG

#5
##### Feb 27, 2013, 01:58 pm
Hi,
// read the state of the pushbutton value:

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
// print out the state of the button:
if (switch0 == 0){
Serial.print("ON ");
Serial.println(buttonState);
switch0=1; ///<<--------------THIS ONE WORKS FOR ME AND NEVER MISSED HOW LONG/SHORT   THE BUTTON HAS BEEN PRESSED
}
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
if (switch0==1){
Serial.print("OFF ");
Serial.println(buttonState);
switch0=0;    ///<<--------------THIS ONE WORKS FOR ME AND NEVER MISSED HOW LONG/SHORT   THE BUTTON HAS BEEN PRESSED
}

#### HazardsMind

#6
##### Feb 27, 2013, 02:02 pm
@JoshSG
Thats has nothing to do with debounce, that is a latch. And it still wont work correctly with out a proper debounce code.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

#### holmes4

#7
##### Feb 27, 2013, 02:15 pm
@joshSG,

Layout code using auto format before postimg and always post code in code tags!.

Mark

#### JoshSG

#8
##### Feb 27, 2013, 02:44 pm
Hi,
I tried the debounce in the example and it does not work for my application, any name will do, debounce, latch, lock as long as it has met the objective, just want to share, it is for all who those who wants to try.

Thanks,

Josh

#### HazardsMind

#9
##### Feb 27, 2013, 02:51 pm

Hi,
I tried the debounce in the example and it does not work for my application, any name will do, debounce, latch, lock as long as it has met the objective, just want to share, it is for all who those who wants to try.

Thanks,

Josh

Actually name does matter, because a latch and a debounced button/switch are two different things.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

#### himfire

#10
##### Feb 27, 2013, 02:54 pm

HazardsMind I have tried to change some mistakes were in my coding and I have done your suggestion , but still having the same problem . and I have posted the code again after fixing some problems .

Code: [Select]
`/* debounce.cpp - Library for solving debounce problem Created by Hisham Alfadel , February 26 , 2013 Released for the public usage and I hope you enjoy with it*/int debounce::reset(int M){  //Return the counter to B = 0 and LED switch it off  Serial.println("B = 0");  digitalWrite(ledPin,LOW);  M = 0;return M ;}int debounce::button_state( char button , int H ){  //check the for HIGH   if(button == '1'){    //Serial.println("I'm here" + B );    //check the number is less than 6    if(H < 6){      H = H + 1;     Serial.println("High");     }  }  if (button == '0'){    H = reset(H);    Serial.println("Reset");    Serial.println(H);  }    return H;}`

Code: [Select]
`/*debounce.h - Library for solving debounce problemCreated by Hisham Alfadel , February 27 , 2013Released for the public usage and I hope you enjoy with itThis code is used to solve the problem of the debounce situationthe code will use the logarithm of :" If it's high add 1 to counteruntil it's rised to Five else fall down to Zero */#ifndef debounce_h#define debounce_h#include "Arduino.h"//Definitions are placed hereclass debounce{public:debounce( char button , int H);int B = 0; //This is used for the increamentunsigned int D = 0; //This is for the serial output }#endif`

Code: [Select]
`#include <debounce.h>const unsigned int ledPin= 13;char button_try;int B = 0;void setup() {  // put your setup code here, to run once:  Serial.begin(9600);  pinMode(ledPin,OUTPUT);  }void loop() {  // put your main code here, to run repeatedly:   button_try=Serial.read();  B = debounce.button_state(button_try,B);    //then should put here if statement to get it right  if(B == 5){      digitalWrite(ledPin1,HIGH);      }}`

#### PaulS

#11
##### Feb 27, 2013, 02:57 pm
Your class STILL does not have members reset() or button_state() declared. You can't implement methods that have not been declared.

And you certainly can't call them without an instance of the class.

#### JoshSG

#12
##### Feb 27, 2013, 03:02 pm
Hi,
@Fire1 I would suggest to please use or buy a Latch not the Debounce Button/Switch to avoid problem.

Thanks,

Josh

#### HazardsMind

#13
##### Feb 27, 2013, 03:16 pm

Hi,
@Fire1 I would suggest to please use or buy a Latch not the Debounce Button/Switch to avoid problem.

Thanks,

Josh

Your giving the OP, wrong information. You clearly don't know the difference between debouncing a button and latching a button. I would love to fully explain the differences in detail, but this is something you need to research for yourself, THEN come back and see if the advice your giving is correct.

OP, if you want to debounce using hardware, google button debounce hardware. It is just a simple capacitor and resistor in the proper setup.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

#### Jimmy60

#14
##### Feb 27, 2013, 03:37 pm
I don't see you reading any buttons. I see you using serial data incorrectly. You don't need to debounce this, you need to learn more about reading and handling serial input.

Go Up