forked from tenbaht/sduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattachInterrupt.ino
More file actions
66 lines (56 loc) · 1.49 KB
/
attachInterrupt.ino
File metadata and controls
66 lines (56 loc) · 1.49 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* attachInterrupt.ino
*
* Simple test and demonstration of the pin change interrupt using
* the attachInterupt() function.
*
* The builtin LED is briefly lit on every button press.
*
* The button press is detected by an interrupt routine and the main loop()
* turns the LED on and off.
* Debouncing is done by simply waiting for a few hundred milliseconds after
* each button press.
*
* required hardware:
* - Connect a button from PA2 to GND.
*
* When using other pins for the button, please keep in mind that there is
* no pin change interrupt support on PA0, PA1, PA7 and PD7. (for stm8blue:
* Arduino pin D0 (PA1) is the only one without interrupt support)
*
* written for sduino, MIT licence
*
* (c) 2020 by Michael Mayer
*/
// the pin where the input button is attached. Change, if needed
#define BUTTON PA2
// volatile is important, because this variable is modified in IRQ routine
volatile uint8_t flag = 0;
/*
* IRQ routine
*
* Triggered on every falling edge. It sets a flag that can be detected by
* the main loop.
*
* no debouncing is done here. Expect multiple trigger for every button press.
*/
void on_button_pressed(void)
{
flag = 1;
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 1); // turn off the LED
pinMode(BUTTON, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON), on_button_pressed, FALLING);
}
void loop()
{
if (flag) {
digitalWrite(LED_BUILTIN, 0);
delay(300);
digitalWrite(LED_BUILTIN, 1);
flag = 0;
}
}