Please see @PeteKnight post in
and example to fix ISR issues in
Read more about ISR in
and
https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/volatile/