0

I need help with debounce of push button. Sometimes it send twice same string to serial link, and I don't know why. Could someone help me, where is a problem?

int reading;
int exbutton= LOW;
unsigned long ddelay= 200;
unsigned long last= 0;
void loop(){
    reading= digitalRead(prkgbrake);
   if (reading== HIGH && exbutton == LOW && millis() - last> ddelay){
    if (brake == 0){
      Serial.write("brake:1\n");
      while( digitalRead(prkgbrake) == HIGH){
      }
    }
    else{
      Serial.write("brake:0\n");
      while( digitalRead(prkgbrake) == HIGH){
      }
    }
    last = millis();
  }

Thank you in advance.

user3421673
  • 97
  • 1
  • 3
  • 10

1 Answers1

0

I hope you didn't copy this code from somewhere, some of the code doesn't make sense.

For instance, what is 'prkgbrake'? What is 'brake'? They are not declared. Why don't you have a 'setup()' function?

Nevertheless, debouncing can be achieved in many ways. I will just fix your code. That way you will understand what you did wrong.

int exbutton        = LOW;
unsigned int _delay = 200;
int pushButton      = 2;

void setup() 
{
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(pushButton, INPUT_PULLUP);
}

void loop()
{
   while (digitalRead(pushButton) == LOW && exbutton == LOW)
   {
      if((millis() - last) > _delay)
      {
          Serial.println("Button Pressed");
          while(digitalRead(pushButton) == LOW);
      }
   }
   last = millis();
}

Explanantion:
Assuming your pushbutton is connected with digital pin 2. When you use a digital pin with a button it is better to use pullup/pulldown. You can use external resistor or the internal resistor for that. The internal resistor only supports Pull-up.

To know more about pull-up/-down checkout this Arduino page. The bottom line is when you use a pin as input it acts like an antenna and can capture signals from surroundings, known as floating state. So it is better to keep the pin in a known state. If you use internal pull-up, the pin will be always HIGH. So the button configuration has to be in a way so that when it is pressed the pin should go LOW.

Pull Up Configuration

The code pinMode(pushButton, INPUT_PULLUP); enables the digital pin 2 as input with pull-up enabled.

the loop() should work like this:

1) Check if the button is pressed (i.e. if it is LOW).

2) If not update the last variable.

3) If yes then DONT update last, and enter the while loop.

4) Now keep checking if millis()-last is greater than _delay. If not it will go back to while loop and check if the button is still pressed or not. If yes then it will come back and check if millis()-last is more than _delay or not. It will continue to do so until it passed the mentioned amount of debounce delay.

5) If button get depressed (i.e. goes to HIGH) before the '_delay' time then it will update the last and will check if button is pressed or not and will start counting the delay time.

N.B. Play with the _delay variable. It will define the responsiveness of your button.

Fahad
  • 159
  • 5