Skip to content
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

Open
per1234 opened this issue Mar 27, 2019 · 4 comments · May be fixed by #576
Open

Add note re: contact bounce to attachInterrupt reference #575

per1234 opened this issue Mar 27, 2019 · 4 comments · May be fixed by #576

Comments

@per1234
Copy link
Collaborator

per1234 commented Mar 27, 2019

Moved from arduino/Arduino#8711 by @Omar-alSuntawi

This code does not work correctly !

const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}

void loop() {
  digitalWrite(ledPin, state);
}

void blink() {
  state = !state;
}

which is on the Arduino Reference https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

It seems to accidentally read random values.

@per1234
Copy link
Collaborator Author

per1234 commented Mar 27, 2019

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 attachInterrupt(). Debounce code has nothing to do with attachInterrupt().

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.

@PaulStoffregen
Copy link

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.

@PaulStoffregen
Copy link

I submitted a pull request. Hopefully it will help others avoid this common misunderstanding.

#576

@animeshsrivastava24
Copy link
Contributor

@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.

@per1234 per1234 changed the title attachInterrupt example code problem Add note re: contact bounce to attachInterrupt reference Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants