Pages: [1] 2   Go Down
Author Topic: Trying to solve debounce problem  (Read 868 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
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:
/*
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:
#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);
     }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50883
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Queens, New York
Offline Offline
Faraday Member
**
Karma: 108
Posts: 3785
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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'){
« Last Edit: February 27, 2013, 07:55:30 am by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2395
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Mark
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
If not today, maybe tomorrow
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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   
    }


* debounce.JPG (103.46 KB, 1024x768 - viewed 8 times.)
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 108
Posts: 3785
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@JoshSG
Thats has nothing to do with debounce, that is a latch. And it still wont work correctly with out a proper debounce code.
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2395
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@joshSG,

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

Mark
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
If not today, maybe tomorrow
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley-cool

Thanks,

Josh
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 108
Posts: 3785
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley-cool

Thanks,

Josh


Actually name does matter, because a latch and a debounced button/switch are two different things.
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
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:
/*
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:
#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);
     }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50883
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 15
If not today, maybe tomorrow
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 108
Posts: 3785
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 364
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Pages: [1] 2   Go Up
Jump to: