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
)