Bounce.h library acting weird

Okay, so i am using the debounce.h library to cancel out some noise on a button. I just want and LED to stay turned on when pressing the button and then shut of when pressed again.

Heres the code:

#include <Bounce.h>

Bounce button1Deb = Bounce(); 

int button1 = 2;  
int ventilA = 8;

int button1State;
int lastButton1State = LOW;

int bounceTime = 100; 
int pushTime = 500;

void setup()
{
  Serial.begin(9600);
  button1Deb.attach(button1);
  button1Deb.interval(bounceTime);   

  pinMode(button1, INPUT);
  pinMode(ventilA, OUTPUT);
}


void loop()
{
  button1Deb.update(); 
  button1 = button1Deb.read();  

  if(button1 == LOW){
    button1State = !lastButton1State;  
    lastButton1State = button1State;  
    delay(pushTime);  
  }
  
  // Some LED get turned on and off
  if(button1State== HIGH){
    digitalWrite(ventilA, HIGH);
  }  
  else{
    digitalWrite(ventilA, LOW);
  }

}

The problem is that it runs the

 if(button1 == LOW)

twice every time i press the button, thus turning on the LED for the pushTime ammount of time and then turning it of again. I have verified this with Serial.print commands. How come this happens? Are the bounce library to slow or i am missing something (by experience it happens to be the last :stuck_out_tongue: )

Okay i think that it was a quistion of updating the timer. I fixed it by using the millis() function:

#include <Bounce.h>

Bounce button1Deb = Bounce(); 

int button1 = 2;  
int ventilA = 8;

int button1State;
int lastButton1State = LOW;

int bounceTime = 100; 
int pushTime = 500;

int timer=0;


void setup()
{
  Serial.begin(9600);
  button1Deb.attach(button1);
  button1Deb.interval(bounceTime);   

  pinMode(button1, INPUT);
  pinMode(ventilA, OUTPUT);
}


void loop()
{
  button1Deb.update(); 
  button1 = button1Deb.read();  


  if(millis()-timer > pushTime){
    if(button1 == LOW){
      button1State = !lastButton1State;  
      lastButton1State = button1State;  
      timer = millis();
    }
  }
  // Some LED get turned on and off
  if(button1State== HIGH){
    digitalWrite(ventilA, HIGH);
  }  
  else{
    digitalWrite(ventilA, LOW);
  }

}

Please correct me if this was not a updating issue, i would like to know my mistakes :slight_smile:

i would like to know my mistakes

Here's one:

Bounce button1Deb = Bounce();

You should NEVER call the constructor directly.

Bounce button1Deb;

Implicitly (and correctly) calls the constructor.

Here's another:

#include <Bounce.h>

You failed to post a link to the library.

And some more:

int bounceTime = 100; 
int pushTime = 500;

Time variables are ALWAYS unsigned long.