Push button problem again

Ok guys....I feel like giving up on this problem.I have modified this code a million times and still dont get the required result.Please help me :frowning:

#include <Bounce.h>


const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

 int buttonPushCounter = 0;   // counter for the number of button presses
 int buttonState = LOW;         // current state of the button
 int lastButtonState = LOW;     // Previous state of the button

unsigned long pressed_time = 0;

Bounce bouncer = Bounce (buttonPin,40);

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(buttonPin, HIGH);
  Serial.begin(115200);
}

void loop() {
  
  bouncer.update();
  buttonState = digitalRead(buttonPin); 
  int buttonState = bouncer.read();
 if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      ++buttonPushCounter;
      digitalWrite(ledPin,HIGH);
      pressed_time=millis();  
    }
    else{
      digitalWrite(ledPin, LOW);
    }
    lastButtonState = buttonState;
  }

  if (buttonPushCounter % 2==0 ) {  
      if (millis() - pressed_time <= 400) {
        digitalWrite(ledPin, HIGH);
        Serial.println("Event Occured");
    } 
    else {
      lastButtonState = buttonState;
      buttonPushCounter=0;
    }
     buttonPushCounter=0;
     pressed_time=0; 
  }
}

I am trying to print "Event Occurred" after two successive button presses.(Which this code does)
But the time elapsed between two button pushes should not be greater than 400milliseconds. ( which i have been unable to achieve even after several troubleshooting sessions :frowning: )

Hi.

Just my 2 cents worth.

You have a line :
digitalWrite(buttonPin, HIGH);
Why would you write to this pin, you just defined it as a INPUT?

Also, there are 2 lines :
buttonState = digitalRead(buttonPin);
int buttonState = bouncer.read();
This will make things very confusing. I am sure the first line should be deleted, as you use the Bounce class.

Also, are you sure you are pressing/releasing/pressing the button inside 400 ms? As your code will only trigger if it sees HIGH - LOW - HIGH, within 400 ms?

For debugging, i would insert a line to print the time difference, probably after the line : if (buttonPushCounter % 2==0 ) {
, something like :
unsigned long delta = millis() - pressed_time;
Serial.println(delta,DEC);

Hope this helps,
Guido

Hi Guido,

Thanks so much for your advice.I think I somehow uncommented the lines you are talking about while pasting the code in here.

buttonState = digitalRead(buttonPin);
digitalWrite(buttonPin, HIGH);

During troubleshooting I was adding and deleting stuff from this code and these lines are some of the residual test code that are now commented.

Also I did try printing the millis() - pressed_time and was mostly getting a 0 and sometimes a 2 or 3.

My code triggers even if the second button press was after a few seconds of the first one.So I think its recognizing the HIGH-LOW-HIGH transition irrespective of the time....which is basically my problem and am trying to figure out how to solve it.

This is really a simple task. The last code you posted has multiple methods of reading the same data (the state of the button). Then, you say that you've commented some of that out.

Why don't you post what you are actually running?

#include <Bounce.h>


const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

 int buttonPushCounter = 0;   // counter for the number of button presses
 int buttonState = LOW;         // current state of the button
 int lastButtonState = LOW;     // Previous state of the button

unsigned long pressed_time = 0;

Bounce bouncer = Bounce (buttonPin,40);

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(buttonPin, HIGH);
  Serial.begin(115200);
}

void loop() {
  
  bouncer.update();
  buttonState = digitalRead(buttonPin); 
  int buttonState = bouncer.read();
 if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      ++buttonPushCounter;
      digitalWrite(ledPin,HIGH);
      pressed_time=millis();  
    }
    else{
      digitalWrite(ledPin, LOW);
    }
    lastButtonState = buttonState;
  }

  if (buttonPushCounter % 2==0 ) {  
      if (millis() - pressed_time <= 400) {
        digitalWrite(ledPin, HIGH);
        Serial.println("Event Occured");
    } 
    else {
      lastButtonState = buttonState;
      buttonPushCounter=0;
    }
     buttonPushCounter=0;
     pressed_time=0; 
  }
}

Here's the code.

@ DeepZ,
having digitalWrite(buttonpin,HIGH); like that in setup enables the built in pull up resistor on current arduino boards. Thus eliminating the need for a pull down restistor on the hardware side.

If you are using the Bounce library, you should not also be directly reading the button state.

  buttonState = digitalRead(buttonPin);
  int buttonState = bouncer.read();

Local and global variables with the same name are also not a good idea.