This library provides a consistent critical section interface over various Arduino platforms.
This library works for
- ArduinoCore-samd:
Arduino Zero
,MKR 1000
,MKR WiFi 1010
,Nano 33 IoT
,MKR GSM 1400
,MKR NB 1500
,MKR WAN 1300/1310
✔️ - ArduinoCore-mbed:
Portenta H7
,Nano 33 BLE
,Nano RP2040 Connect
,Edge Control
✔️ - arduino-esp32:
ESP32 Dev Module
,ESP32 Wrover Module
, ... ✔️ - arduino-pico:
Raspberry Pi Pico
,Adafruit Feather RP2040
, ... ✔️ - adafruit/ArduinoCore-samd:
Adafruit Feather M4 CAN Express
, ... ✔️ - ArduinoCore-renesas:
Portenta C33
,Uno R4 WiFi
,Uno R4 Minima
, ... ✔️
#include <107-Arduino-CriticalSection.h>
volatile int button_evt_counter = 0;
void setup() {
Serial.begin(9600);
while (!Serial) { }
attachInterrupt(0, onButton_1_Pressed, RISING);
attachInterrupt(1, onButton_2_Pressed, RISING);
}
void loop() {
int copy_button_evt_counter;
{
/* Prevent change of button_evt_counter during readout. */
CriticalSection crit_sec;
copy_button_evt_counter = button_evt_counter;
}
Serial.println(copy_button_evt_counter);
}
void onButton_1_Pressed() {
CriticalSection crit_sec;
button_evt_counter++;
}
void onButton_2_Pressed() {
CriticalSection crit_sec;
button_evt_counter++;
}