Hi,
Doing some work and need a bit better response time for button switch detection, so I have turned to using Interrupts.
Now I have got a few examples from here but all these examples detect multiples times for a single keypress.
I read that I could use the ISR to set a variable and monitor that variable in the main loop to cut out the multiple detections. But this still detects multiple times for me.
Here is the code for ISR setting a variable. On the button I have a 10k resistor grounding it
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables that will change:
volatile int buttonState = 0; // variable for reading the pushbutton status
volatile boolean button_pressed = false; // variable that the ISR will change that loop() can monitor
void setup() {
Serial.begin(57600);
Serial.println("Starting...");
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
attachInterrupt(0, pin_ISR, CHANGE);
}
void loop() {
if (button_pressed) {
button_pressed = false;
Serial.println("Key Press");
}
}
void pin_ISR() {
buttonState = digitalRead(buttonPin);
if (buttonState !=0) {
button_pressed = true;
}
digitalWrite(ledPin, buttonState);
}
When it is ruinning, pressing the button will produce either 1 detection (displayed from the serial print in the main loop) or with will produce several 2 to x detections depending on the interrupt type (RISING, FALLING, CHANGE)
What is the best way to just detect the keypress once.
I am wanting to detect a single keypress and then run some time critical code to measure but with the multiple detections it is running the time critical code many times.
Thanks