-
-
Notifications
You must be signed in to change notification settings - Fork 731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add note re: contact bounce to attachInterrupt
reference
#575
Comments
I suspect that you are triggering the interrupt with something that has contact bounce, resulting in multiple interrupt triggers in very rapid succession and thus a random LED state for each press of the button. This could be be resolved by adding some debouncing code to the example, but I'm not sure whether this would be a good thing. The example is intended to be a simple demonstration of how to use I suppose an alternative that doesn't add any significant irrelevant code to the example would be to set the LED pin to mirror the state of the interrupt pin: const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = HIGH;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = digitalRead(interruptPin);
} That will not be visibly affected by contact bounce, but it's not a very interesting demonstration of the use of interrupts. |
Perhaps the attachInterrupt page could mention the Bounce library and specifically recommend not using interrupts for pushbuttons, switches and other mechanisms that suffer from mechanical chatter? This seems to be a very common misunderstanding among novices. The documentation could really do much better to guide users towards the proper approach to meet their needs. |
I submitted a pull request. Hopefully it will help others avoid this common misunderstanding. |
@per1234 Along with this Issue, I am trying out all the examples mentioned on the Reference Pages with help of my Arduino Uno Board and other specifications necessary and will be happy to report any issue with any present example that will need an update or correction. |
attachInterrupt
reference
Moved from arduino/Arduino#8711 by @Omar-alSuntawi
This code does not work correctly !
which is on the Arduino Reference https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
It seems to accidentally read random values.
The text was updated successfully, but these errors were encountered: