-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interrupt.ino
44 lines (40 loc) · 880 Bytes
/
Interrupt.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// C++ code
#define BUTTONPIN 2
#define RED 8
volatile bool buttonPress = false;
volatile int count = 0;
volatile unsigned long timeStart;
unsigned long debounceDelay = 50;
int LEDSTATE = LOW;
void buttonPressLed() {
if (LEDSTATE == LOW) {
LEDSTATE = HIGH;
} else {
LEDSTATE = LOW;
}
digitalWrite(RED, LEDSTATE);
}
void buttonRiseInterrupt() {
unsigned long timeNow = millis();
if (timeNow - timeStart > debounceDelay) {
timeStart = timeNow;
buttonPress = true;
}
}
void setup() {
timeStart = millis();
Serial.begin(115200);
pinMode(RED, OUTPUT);
pinMode(BUTTONPIN, INPUT);
digitalWrite(RED, LEDSTATE);
attachInterrupt(digitalPinToInterrupt(BUTTONPIN),
buttonRiseInterrupt, RISING);
}
void loop() {
if (buttonPress) {
buttonPress = false;
buttonPressLed();
count += 1;
Serial.println(count);
}
}