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 ,
/*
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;
}
/*
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
#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);
}
}
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.
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
}
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)
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
Actually name does matter, because a latch and a debounced button/switch are two different things.
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 .
/*
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;
}
/*
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
#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);
}
}
JoshSG:
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.
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.
Hi Fire1,
I totally agree with @Jimmy60, try not to use additional hardware as they add to your BOM, try to use the example from Button example that comes with Arduino, it has no extra hardware. This is my almost a week of playing with Arduino, I bought UNO and Leonardo for a start together with a CHEAP LATCH, breadboard and telephone wires, so far I never use any additional debounce hardware and still happy with the result.
@HazardsMind, thanks for the tutorial but my UNO and Leonardo so far didn't report any debouncing issue with my Cheap Latch so I have no issue, and as long as I have no issue with the debounce, I will not use extra hardware, let us just help Fire1 8)
I fail to see in this code where he is reading buttons. I see an incorrect attempt at a serial read. My gut is telling me that he is trying to fix the improper serial read with debouncing. The button_try = Serial.read() and the fact that he is looking at his buttons as characters is what brings me to this conclusion.
It would probably be a good idea for the OP to describe a little more precisely what he is expecting his Arduino sketch to actually do.
Yes , I'm trying to see what is the arduino doing when it's get any input by using serial monitor . then when it is working as I want , then I have to make some changes in the code for real life projects . My specific problem is in calling the variables , I think I had made some mistake some where in the definition of some variables .
Yesterday , I was searching for some software solution , and I have saw this page that agree with me . and he also doing the same as I'm doing . So please check this out : http://www.labbookpages.co.uk/electronics/debounce.html
I want to use all the pins of arduino to do multiple work . So for that do I have to buy stuffs for each pin , I think maybe some software solution will be better .