Quicker Pin Change interrupts

anbolge

Member
Hello,

I would appreciate if anyone could help.

I would like to achieve fast read speed for teensy 4.1 (either on change or better on rising edge). I found a solution with attachInterrupt that works, but I wanted to check with if there is a quicker way to read. My attempts to use ISR failed, because the board is an ARM board as I understood. I tried looking up for some other examples but was not able to found so far, if I miss it, please kindly direct me..


Code:
IntervalTimer myTimer;
const byte interruptPin = 2; //button
volatile unsigned long blinkCount = 0; // use volatile for shared variables
unsigned long blinkCopy;  // holds a copy of the blinkCount

void setup() {
  Serial.begin(9600);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), button, CHANGE);
}
void button() {
  blinkCount = blinkCount + 1;
}
void loop() {
  noInterrupts();
  blinkCopy = blinkCount;
  blinkCount = 0;
  interrupts();
  Serial.print("blinkCount = ");
  Serial.println(blinkCopy);
  delay(1000);
}
 
I'm not sure I understand your issue but the delay(1000) will probably not work too well.
Try this example non-blocking loop().
Code:
// IntervalTimer myTimer; // NOT IN USE HERE

const byte interruptPin = 2; //button
volatile unsigned long blinkCount = 0; // use volatile for shared variables
unsigned long blinkCopy;  // holds a copy of the blinkCount

elapsedMillis sincePrint; // See: https://www.pjrc.com/teensy/td_timing_elaspedMillis.html

void setup() {
  Serial.begin(9600);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), button, CHANGE);
}
void button() {
  blinkCount = blinkCount + 1;
}
void loop() {
  noInterrupts();
  blinkCopy = blinkCount;
  blinkCount = 0;
  interrupts();

  // Print every 0.5 second
  if (sincePrint > 500) {      // "sincePrint" auto-increases
    sincePrint = 0;
    Serial.print("blinkCount = ");
    Serial.println(blinkCopy);
  }

}
 
Back
Top