I am trying to write a program that checks for 2 button pushes and once confirmed prints "Event Occurred" on the terminal.Also with every push it needs to blink the LED.
I have tried to re do this code in several ways...even using interrupts but my code spits out an "Event Occurred" every time I push the button.Don't know whats going wrong.
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // Previous state of the button
long interval = 400;
long thismillis =0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// digitalWrite(buttonPin, HIGH);
Serial.begin(115200);
}
void loop() {
buttonState = digitalRead(buttonPin);
// if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
digitalWrite(ledPin, HIGH);
thismillis=millis();
}
else
{ digitalWrite(ledPin, LOW);
}
if (buttonPushCounter % 2 == 0 && (millis() - thismillis) <= interval)
{
digitalWrite(ledPin, HIGH);
Serial.print("Event Occured\n");
}
else {
digitalWrite(ledPin, LOW);
}
}
I added a 5ms delay for the debounce of the switch.That helped in changing the garbled data on the terminal to a stream of "Event Occurred" for every button push.
What I can't understand is why my code spits out multiple "Event Occurred" for one button push let alone check for 2 button pushes.
Thanks so much for your suggestions.They are always really helpful.
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // Previous state of the button
long interval = 400;
long thismillis =0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// digitalWrite(buttonPin, HIGH);
Serial.begin(115200);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
buttonPushCounter++;
digitalWrite(ledPin, HIGH);
thismillis=millis();
}
else
{
digitalWrite(ledPin, LOW);
}
if (buttonPushCounter % 2 == 0 && (millis() - thismillis) <= interval)
{
digitalWrite(ledPin, HIGH);
Serial.print("Event Occured\n");
}
else
{
digitalWrite(ledPin, LOW);
}
}
i tided you code a bit to make it easier to read.
what i see is : the "normal state is no button pushed so buttonState will be HIGH. Each pass through the loop the first if will succeed ---buttonPushCounter will increment, lEd will be relight and thismillis is updated.
then you push the button and the first else succeeds and the led goes off.
buttonPushCounter has some number in it that depends on how many times it went around while buttonState was high but probably a lot this loop will only take a few micro seconds unless you hit the print.
in the second if statment there is a 50/50 chance that buttonPushCounter is odd and since thismillis was just updated a few microseconds ago the second qualifier is sure to be true. so you will get the print.
when you go through the loop again the switch is still down so nothing has changed and you get the print again and ----- until you let the button up. It seems to me you should only get the repeated printing half the time the other half nothing but the LED changing.
I think your commented out code is the key. You want to do something about the button press, but only when the state changes from unpressed to pressed.
Debouncing takes care of the fact that that transition can occur more than once for each deliberate button press.
Perhaps you should try the button library. It handles the debouncing and state change functionality for you, leaving you free to concentrate on what you want to have happen when the button is pressed.
Don't forget that swtich bounce occurs at both ends of press and release.
In other words, what seems like... (u for "up", d for "down")
uuuuuuuudddddddddddddddddddddddddduuuuuuuuuu
to a human may look like...
uuuuuuuudududdddddddddddddddddddddududuuuuuu
to the computer.
If "blocking" isn't a problem, the simple answer is to keep the Aruduino stuck in a loop after the switch goes "down" until it has STAYED up for longer than can be explained by bouncing. Simple to program, and in many instances, the blocking (Arudino not looking at other things) won't be a problem.
Thanks cxevalo! I think there was some lose connections in my board hence I was getting a weird output.
#include <Bounce.h>
#define interval 400
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; // current state of the button
int lastButtonState = LOW; // Previous state of the button
unsigned long thismillis = 0;
Bounce bouncer = Bounce (buttonPin,20);
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
bouncer.update();
int buttonState = bouncer.read();
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
++buttonPushCounter;
digitalWrite(ledPin,HIGH);
thismillis=millis();
}
else{
digitalWrite(ledPin, LOW);
}
lastButtonState = buttonState;
}
if (buttonPushCounter % 2==0 ) {
int milliseconds = (millis() - thismillis);
if (milliseconds < interval){
digitalWrite(ledPin, HIGH);
Serial.println("Event Occured");
}
else {
lastButtonState = buttonState;
}
buttonPushCounter=0;
thismillis = 0;
}
}
So this is my code after a lot of troubleshooting and incorporating suggestions of all of you.It works but not completely.It gives me an "Event Occurred" after 2 button pushes but it does not take into account the maximum time interval of 400ms between the two button pushes.
In short I get an "Event Occurred" even if the time elapsed between the two button pushes is a couple of seconds.
Any suggestions?
Thanks so much all of you for all your help!
If you see problems that look like they might be coming from loose connections, you may well be having problems due to switch bounce. "Seeing" what is going on, at the speeds that many programs zip along, doing the "loop" function again and again is hard for a "mere human" brain to accomplish.