I am using the Button State Change Detection (Edge Detection) example to send a single output from a button press to my PC. When I view the serial monitor this seems to work ok. But when i send to software (called Testbench) on my PC it receives multiple signals - often 4 rather than one..I have tried using a delay () ....this seems to alter the number received, but I cant seem to just get one..... Any ideas what I can do with the code to ensure that just a single pulse is sent to output?
Any ideas what I can do with the code to ensure that just a single pulse is sent to output?
Show us your code
// this constant won't change:
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
int outPin = 8; // digital pin 8void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(outPin, OUTPUT); // sets the digital pin as output
// initialize serial communication:
Serial.begin(9600);
}void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);digitalWrite(outPin, HIGH); // sets the pin on
delayMicroseconds(1); // pauses for 50 microseconds;
digitalWrite(outPin, LOW); // sets the pin off// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
lastButtonState = buttonState;
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;}
You have no bounce suppression code.
What is pin 2 connected to?
Try this:
Serial.println("on");
Serial.println(buttonPushCounter); <<<<< add this line
What happens on the serial monitor?
Thanks for your reply....
Pin 2 is connected to one side of the button
It seems to work on the serial monitor I get the following:
.....on
1
But on my application I still get multiple outputs showing with different values. The screenshot shows one button press
What I am trying to do is get a single button press event sent to the software for each button press
delayMicroseconds(1); // pauses for 50 microseconds;
Useless comments need to be correct. Or deleted.
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
lastButtonState = buttonState;
}
Please explain why lastButtonState is only set when buttonState is HIGH. In reality, lastButtonState should be set to buttonState on EVERY pass through loop().
Pin 2 is connected to one side of the button
And the other side?
the other side is GND
You need a pull-up resistor on the input pin. Use a 10K from pin 2 to +5V or:
pinMode(buttonPin, INPUT_PULLUP); <<<< add _PULLUP
De-bounce the your input, see:
Debouncing http://www.gammon.com.au/forum/?id=11955