/*
Forum: https://forum.arduino.cc/t/midi-out-am-nano-programmieren/1394108
Wokwi: https://wokwi.com/projects/435923199268581377
Interrupt-Aufrufe bei seriellem Dateneingang
Durch Ändern des Wertes von "mode" lassen sich die unterschiedlichen Aufrufhäufigkeiten
testen.
RISING -> 1 bis 4 Aufrufe pro Byte
LOW -> ca. 11 bis 40 Aufrufe pro Byte
0 Aufrufe jeweils erst ab dem sechsten Character im Buffer
2025/07/09
ec2021
*/
constexpr byte isrPin {3};
constexpr byte mode {LOW}; // Can be set to LOW, CHANGE, RISING, FALLING
volatile int count = 0;
void setup() {
Serial.begin(31250);
Serial.println();
pinMode(isrPin, INPUT_PULLUP);
attach();
}
void loop() {
if (Serial.available()) {
detach();
while (Serial.available()) {
char c = Serial.read();
switch (c) {
case '\n':
Serial.print("[NL]"); // New Line = "Zeilenvorschub"
break;
case '\r':
Serial.print("[CR]"); // Carriage Return = "Wagenrücklauf"
break;
default:
Serial.print(c);
}
}
printCount();
attach();
}
}
void ISRoutine() {
count++;
}
void printCount() {
noInterrupts();
int actCount = count;
count = 0;
interrupts();
if (actCount > 0) {
Serial.print("\t Count =\t");
Serial.println(actCount);
}
}
void detach() {
detachInterrupt((digitalPinToInterrupt(isrPin)));
}
void attach() {
attachInterrupt((digitalPinToInterrupt(isrPin)), ISRoutine, mode);
}