Pages: [1] 2   Go Down
 Author Topic: Trying to solve debounce problem  (Read 652 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 32
 « on: February 27, 2013, 07:40:15 am » Bigger Smaller 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:
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
Newbie
Karma: 0
Posts: 32
 « Reply #1 on: February 27, 2013, 07:43:24 am » Bigger Smaller 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
Brattain Member
Karma: 517
Posts: 44552
Seattle, WA USA
 « Reply #2 on: February 27, 2013, 07:50:16 am » Bigger Smaller 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
Karma: 59
Posts: 2736
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
 « Reply #3 on: February 27, 2013, 07:53:39 am » Bigger Smaller 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

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Poole, Dorset, UK
Offline
Edison Member
Karma: 23
Posts: 1795
 « Reply #4 on: February 27, 2013, 07:57:45 am » Bigger Smaller 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
Newbie
Karma: 0
Posts: 15
If not today, maybe tomorrow
 « Reply #5 on: February 27, 2013, 07:58:35 am » Bigger Smaller Reset

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
}
 debounce.JPG (103.46 KB, 1024x768 - viewed 4 times.) Logged

Queens, New York
Offline
Karma: 59
Posts: 2736
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
 « Reply #6 on: February 27, 2013, 08:02:58 am » Bigger Smaller 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

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Poole, Dorset, UK
Offline
Edison Member
Karma: 23
Posts: 1795
 « Reply #7 on: February 27, 2013, 08:15:55 am » Bigger Smaller Reset

@joshSG,

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

Mark
 Logged

Offline
Newbie
Karma: 0
Posts: 15
If not today, maybe tomorrow
 « Reply #8 on: February 27, 2013, 08:44:57 am » Bigger Smaller 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.

Thanks,

Josh
 Logged

Queens, New York
Offline
Karma: 59
Posts: 2736
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
 « Reply #9 on: February 27, 2013, 08:51:36 am » Bigger Smaller 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.

Thanks,

Josh

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

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Offline
Newbie
Karma: 0
Posts: 32
 « Reply #10 on: February 27, 2013, 08:54:18 am » Bigger Smaller Reset

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:
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
Brattain Member
Karma: 517
Posts: 44552
Seattle, WA USA
 « Reply #11 on: February 27, 2013, 08:57:33 am » Bigger Smaller 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
Newbie
Karma: 0
Posts: 15
If not today, maybe tomorrow
 « Reply #12 on: February 27, 2013, 09:02:42 am » Bigger Smaller Reset

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

Thanks,

Josh
 Logged

Queens, New York
Offline
Karma: 59
Posts: 2736
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
 « Reply #13 on: February 27, 2013, 09:16:35 am » Bigger Smaller Reset

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

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Offline
Sr. Member
Karma: 15
Posts: 330
When the going gets weird, the weird turn pro. - Hunter S. Thompson
 « Reply #14 on: February 27, 2013, 09:37:48 am » Bigger Smaller 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