Go Down

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

fire1

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 problem
Created by Hisham Alfadel , February 27 , 2013
Released for the public usage and I hope you enjoy with it

This code is used to solve the problem of the debounce situation
the code will use the logarithm of :" If it's high add 1 to counter
until it's rised to Five else fall down to Zero
*/

#ifndef debounce_h
#define debounce_h

#include "Arduino.h"
//Definitions are placed here
class debounce{
public:
debounce( int ledPin1 , int button , int H);
int B = 0; //This is used for the increament
unsigned 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);
     }
}

fire1

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

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 pm Last 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

You also need to look at the way button are debounced. See the example in the playground or in the examples code.

Mark

JoshSG

Hi,
      // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // 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

@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

@joshSG,

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

Mark

JoshSG

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. 8)

Thanks,

Josh

HazardsMind


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. 8)

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

fire1

Hi thanks for the reply

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 problem
Created by Hisham Alfadel , February 27 , 2013
Released for the public usage and I hope you enjoy with it

This code is used to solve the problem of the debounce situation
the code will use the logarithm of :" If it's high add 1 to counter
until it's rised to Five else fall down to Zero
*/

#ifndef debounce_h
#define debounce_h

#include "Arduino.h"
//Definitions are placed here
class debounce{
public:
debounce( char button , int H);
int B = 0; //This is used for the increament
unsigned 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

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

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

Thanks,

Josh

HazardsMind


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

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

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