Go Down

Topic: Button Debounce help (Read 482 times) previous topic - next topic

ColeH

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: [Select]
#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

Arrch

Code: [Select]

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


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

ColeH


liudr

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.

UKHeliBob


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 !
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

MaJiG

I would write the code like this:
Code: [Select]

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.

PaulS

Code: [Select]
  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.

Go Up