/
04-070_Interrupt_with_button_and_led.ino
105 lines (91 loc) · 2.94 KB
/
04-070_Interrupt_with_button_and_led.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* 04.070 - GPIO interrupt with button and LED
*
* This sketch shows you how to use an interrupt routine to respond to a
* button press.
*
* When the button is pressed, the LED state toggles.
*
* This example includes software debouncing code.
*
* This sketch was written by Peter Dalmaris using information from the
* ESP32 datasheet and examples.
*
*
* Components
* ----------
* - ESP32 Dev Kit v4
* - LED
* - 320 Ohm resistor
* - Button (using an internal pull-up resistor)
*
* IDE
* ---
* Arduino IDE with ESP32 Arduino Code
* (https://github.com/espressif/arduino-esp32)
*
*
* Libraries
* ---------
* - None
* -
*
* Connections
* -----------
*
* ESP32 Dev Kit | LED
* ------------------------------
* GND | Cathode
* GPIO32 | Anode via resistor
*
* Connect pin 1 of the button to ESP32 GPIO 25.
* Connect pin 2 of the button to ESP32 GND.
*
* It is possible to use any other GPIO that can be configured as an output.
* This excludes GPIO 34, 35, 36, 37, 38, 39.
*
* Other information
* -----------------
*
* 1. ESP32 Datasheet: https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf
* 2. Interrupt allocations (Doc): https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/intr_alloc.html
* 3. Technical reference (2. Interrupt Matrix): https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf
* 4. ESP32-IDF portmacro.h: http://esp32.info/docs/esp_idf/html/dc/d35/portmacro_8h_source.html
* Created on March 26 2019 by Peter Dalmaris
*
*/
const byte LED_GPIO = 32; // Marked volatile so it can be read inside the ISR
bool led_state = false; // Keep track of the state of the LED
const byte interruptPin = 25;
volatile int interruptCounter = 0;
int numberOfInterrupts = 0;
// Debouncing parameters
long debouncing_time = 1000; //Debouncing Time in Milliseconds
volatile unsigned long last_micros;
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR handleInterrupt() {
portENTER_CRITICAL_ISR(&mux);
if((long)(micros() - last_micros) >= debouncing_time * 1000) {
interruptCounter++;
last_micros = micros();
}
portEXIT_CRITICAL_ISR(&mux);
}
void setup() {
Serial.begin(115200);
pinMode(LED_GPIO, OUTPUT);
Serial.println("Monitoring interrupts: ");
pinMode(interruptPin, INPUT_PULLUP); // Using an extarnal pull up instead of internal
attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);
}
void loop() {
if(interruptCounter>0){
portENTER_CRITICAL(&mux);
interruptCounter--;
portEXIT_CRITICAL(&mux);
led_state = !led_state;
digitalWrite(LED_GPIO, led_state); // turn the LED on (HIGH is the voltage level)
numberOfInterrupts++;
Serial.print("An interrupt has occurred. Total:");
Serial.println(numberOfInterrupts);
}
}