Pages: [1]   Go Down
Author Topic: Button Debounce help  (Read 403 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 102
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
  I am working on a sketch in which when the correct keypad password is typed in, a servo moves, then when a button is pressed it waits 5 seconds then moves back.  I have added almost everything i need but I still need to debounce the button.  I have read example sketches but cant quite figure out how to put it into my sketch. Can anybody help me?
Here is the code:
Code:
#include <Servo.h> //include the servo library

#include <Keypad.h> //include the keypad library

Servo servoMain; //Define our servo

const int buttonPin = 2; //the pushbutton pin

int buttonState = 0; //variable for reading the pushbutton

char* secretCode = "235711";
int position = 0;

const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte rowPins[rows] = {4, 5, 6, 7};
byte colPins[cols] = {1, 2, 3};

Keypad keypad = Keypad(makeKeymap(keys),
                       rowPins, colPins,
                       rows, cols);

int redPin = 8;
int greenPin = 9;

void setup()
{
 //start button code
 pinMode(buttonPin, INPUT);
 //end button code
  //start servo code
  servoMain.attach(10); //servo on digital pin 10
  //end servo code
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  setLocked(true);
}

void loop()
{
  //start button code
  buttonState = digitalRead(buttonPin); //read the state of the button
 
  if (buttonState == HIGH) {
    delay(5000); //wait 5 seconds
    servoMain.write(90); //turn servo 90 deg. left (locked)
  }
  //end button code

 
  char key = keypad.getKey();
  if (key == '*' || key == '#') {
    position = 0;
    setLocked(true);
  }
 
  if (key == secretCode[position]) {
    position++;
  }
 
  if (position == 6) {
    setLocked(false);
  }
  delay(50);
}

void setLocked(int locked)
{
  if (locked) {
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);
  }
  else {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, HIGH);
    servoMain.write(0);  // Turn Servo to center
  }
}
P.S. I am new to programming and Arduino
Thanks
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3379
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (buttonState == HIGH) {
    delay(5000); //wait 5 seconds

With a 5 second delay, the button bounce shouldn't be an issue.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 102
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's great, thanks.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7189
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So this button is separate from the keypad, right? Just want some clarification.

You should use a different name for your keypad than the class name Keypad. I noticed the first letter is lower case but why not using something like myKeypad or LockKeypad?

The wait 5 second is not really debouncing but will likely work as Arrch said.
Logged


East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4247
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The wait 5 second is not really debouncing but will likely work.
If the button bounces for 5 seconds then there are problems beyond the scope of software debouncing !
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Lost Wages
Offline Offline
Full Member
***
Karma: 11
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would write the code like this:
Code:
void loop()
{
  //start button code
  buttonState = digitalRead(buttonPin); //read the state of the button
  
  if (buttonState == HIGH)
  {
    //here: we have detected a button push, but it might be a glitch
    delay(10);  //wait 10 milliseconds

    if (buttonState == HIGH)
    {
      delay(4990);             //wait the rest of the 5 seconds
      servoMain.write(90); //turn servo 90 deg. left (locked)
   }
   //end button code


I would also swap the circuitry so that the button push produces a LOW by enabling the internal pullup resistor of the input pin and attaching the button from it to ground.  It is more reliable and has fewer parts.
Logged

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

Code:
  if (buttonState == HIGH)
  {
    //here: we have detected a button push, but it might be a glitch
    delay(10);  //wait 10 milliseconds

    if (buttonState == HIGH)
    {
If the state was HIGH, and you don't read it again, how can it not still be HIGH? Splitting the delay up into two parts does nothing useful, unless you read the pin state again.
Logged

Pages: [1]   Go Up
Jump to: