18
18
#include < SparkFun_Qwiic_Button.h>
19
19
QwiicButton button;
20
20
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)
22
22
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
23
28
24
29
void setup () {
25
30
Serial.begin (115200 );
@@ -28,7 +33,8 @@ void setup() {
28
33
Wire.begin (); // Join I2C bus
29
34
30
35
// intialize interrupt pin
31
- pinMode (interruptPin, INPUT_PULLUP);
36
+ pinMode (interruptPin, INPUT);
37
+ attachInterrupt (digitalPinToInterrupt (interruptPin), buttonHandler, FALLING);
32
38
33
39
// check if button will acknowledge over I2C
34
40
if (button.begin () == false ) {
@@ -43,9 +49,8 @@ void setup() {
43
49
}
44
50
45
51
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
+ {
49
54
Serial.println (" Button event!" );
50
55
51
56
// check if button is pressed, and tell us if it is!
@@ -58,6 +63,12 @@ void loop() {
58
63
Serial.println (" The button has been clicked!" );
59
64
}
60
65
button.clearEventBits (); // once event bits are cleared, interrupt pin goes high
66
+ interruptEntered = false ;
67
+ delay (15 );
61
68
}
62
- delay (20 ); // Don't hammer too hard on the I2C bus
69
+ }
70
+
71
+ void buttonHandler ()
72
+ {
73
+ interruptEntered = true ;
63
74
}
0 commit comments