Skip to content

Commit 34becb6

Browse files
author
Michael Polia
committed
Modifications to the example for demonstrating interrupt-based binary semaphores.
1 parent ab8673f commit 34becb6

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

examples/Interrupts/Interrupts.ino

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*/
55

66

7-
// Include Arduino FreeRTOS library
8-
#include <Arduino_FreeRTOS.h>
7+
#include <Arduino.h>
8+
#include <Adafruit_TinyUSB.h> // for Serial
99

1010
// Include semaphore supoport
1111
#include <semphr.h>
@@ -18,8 +18,17 @@ SemaphoreHandle_t interruptSemaphore;
1818

1919
void setup() {
2020

21-
// Configure pin 2 as an input and enable the internal pull-up resistor
22-
pinMode(2, INPUT_PULLUP);
21+
Serial.begin(115200);
22+
23+
// Wait for a serial port connection to be established before continuing.
24+
// Don't want to miss any debug messages.
25+
while ( !Serial ) delay(10); // for nrf52840 with native usb
26+
27+
Serial.println("STARTING THE APPLICATION.");
28+
29+
30+
// Configure pin 4 as an input and enable the internal pull-up resistor
31+
pinMode(4, INPUT_PULLUP);
2332

2433
// Create task for Arduino led
2534
xTaskCreate(TaskLed, // Task function
@@ -36,13 +45,39 @@ void setup() {
3645
interruptSemaphore = xSemaphoreCreateBinary();
3746
if (interruptSemaphore != NULL) {
3847
// Attach interrupt for Arduino digital pin
39-
attachInterrupt(digitalPinToInterrupt(2), interruptHandler, LOW);
48+
attachInterrupt(digitalPinToInterrupt(4), interruptHandler, CHANGE);
4049
}
4150

4251

4352
}
4453

45-
void loop() {}
54+
void loop() {
55+
56+
static bool firstTime = true;
57+
static int previousDigitalReadValue = -1;
58+
59+
if ( firstTime ) {
60+
Serial.println("Starting loop....");
61+
delay(1000);
62+
63+
firstTime = false;
64+
}
65+
66+
int digitalReadValue = digitalRead (4);
67+
68+
if (digitalReadValue != previousDigitalReadValue) {
69+
if (digitalReadValue == HIGH) {
70+
Serial.println("HIGH");
71+
}
72+
else {
73+
Serial.println("LOW");
74+
}
75+
76+
previousDigitalReadValue = digitalReadValue;
77+
}
78+
79+
delay(1000);
80+
}
4681

4782

4883
void interruptHandler() {
@@ -64,13 +99,18 @@ void TaskLed(void *pvParameters)
6499

65100
pinMode(LED_BUILTIN, OUTPUT);
66101

102+
Serial.print("Starting task ");
103+
Serial.println(pcTaskGetName(NULL)); // Get task name
104+
delay(1000);
105+
67106
for (;;) {
68107

69108
/**
70109
* Take the semaphore.
71110
* https://www.freertos.org/a00122.html
72111
*/
73112
if (xSemaphoreTake(interruptSemaphore, portMAX_DELAY) == pdPASS) {
113+
Serial.println("Semaphore interrupt occurred.");
74114
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
75115
}
76116
vTaskDelay(10);

0 commit comments

Comments
 (0)