Skip to content

Commit 9935b34

Browse files
changing example to use an actual interrupt
1 parent c5d9d12 commit 9935b34

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

examples/Example8_ExtInterrupt/Example8_ExtInterrupt.ino

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@
1818
#include <SparkFun_Qwiic_Button.h>
1919
QwiicButton button;
2020
uint8_t brightness = 100; //The brightness to set the LED to when interrupt pin is high
21-
//Can be any value between 0 (off) and 255 (max)
21+
//Can be any value between 0 (off) and 255 (max)
2222
int interruptPin = 2; //pin that will change states when interrupt is triggered
23+
int ledPin = 13;
24+
25+
bool buttonIsPressed = false;
26+
bool buttonHasBeenClicked = false;
27+
bool interruptEntered = false; //Interrupt flag so we can clear things outside of the interrupt
2328

2429
void setup() {
2530
Serial.begin(115200);
@@ -28,7 +33,8 @@ void setup() {
2833
Wire.begin(); //Join I2C bus
2934

3035
//intialize interrupt pin
31-
pinMode(interruptPin, INPUT_PULLUP);
36+
pinMode(interruptPin, INPUT);
37+
attachInterrupt(digitalPinToInterrupt(interruptPin), buttonHandler, FALLING);
3238

3339
//check if button will acknowledge over I2C
3440
if (button.begin() == false) {
@@ -43,9 +49,8 @@ void setup() {
4349
}
4450

4551
void loop() {
46-
//check state of interrupt pin
47-
//interrupt pin goes low when user presses the button
48-
if (digitalRead(interruptPin) == LOW) {
52+
if (interruptEntered)
53+
{
4954
Serial.println("Button event!");
5055

5156
//check if button is pressed, and tell us if it is!
@@ -58,6 +63,12 @@ void loop() {
5863
Serial.println("The button has been clicked!");
5964
}
6065
button.clearEventBits(); //once event bits are cleared, interrupt pin goes high
66+
interruptEntered = false;
67+
delay(15);
6168
}
62-
delay(20); //Don't hammer too hard on the I2C bus
69+
}
70+
71+
void buttonHandler()
72+
{
73+
interruptEntered = true;
6374
}

0 commit comments

Comments
 (0)