Can't get hardware interrupt working on Seeeduino Xiao

So I am new to the XIAO and am trying to get a hardware interrupt working. I have some code that works perfectly on a Pro Micro, but does not work on the XIAO and I am not sure why. Hoping someone can point me in the right direction. Purpose is to take the output of an RC receiver and find the servo pulse width. Based on pulse width switch different lights on and off. Only Nav implemented so far. Code follows.
Thanks

/*
*/

// RC input settings
#define IRQ_NUMBER 10 // Interrupt number to use
#define PIN_SERVO 2 // RC channel input pin number - this needs to match whatever interrupt is used
#define SERVO_LOW 1000 // RC channel low threshold
#define SERVO_MID 1400 // RC channel midpoint
#define SERVO_HIGH 1800 // RC channel high threshold
#define SERVO_DEAD_BAND 25 // Servo signal dead-band size, eliminates flicker
#define SERVO_REVERSED false // Whether or not the servo channel is reversed

// Nav lights settings
#define NAV_PIN_LIGHT 3 // Nav light output pin number

// Var declarations
volatile unsigned long servoPulseStartTime;
volatile int servoPulseWidth = 0;

// nav, strobe and landing light via servo, acb is on whenever we have power!
boolean curNavLight = false;
boolean switchNavLight = false;

// the setup function runs once when you press reset or power the board
void setup() {

// Set up interrupt handler.
attachInterrupt(digitalPinToInterrupt(PIN_SERVO), measureServoSignal, CHANGE);
//attachInterrupt(IRQ_NUMBER, measureServoSignal, CHANGE);

// initialize light pins as an output.

pinMode(PIN_SERVO, INPUT_PULLUP);
pinMode(NAV_PIN_LIGHT, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
unsigned long currentTime = micros();

checkServo();

setNavLight(switchNavLight);
}

// Check servo signal, and decide whether to turn things on or off
void checkServo() {
// Nav Lights
// Modify threshold to prevent flicker
int navThreshold = SERVO_LOW;
if (!curNavLight) {
// Nav are not on, adjust threshold up
navThreshold += SERVO_DEAD_BAND;
} else {
// Nav are on, adjust threshold down
navThreshold -= SERVO_DEAD_BAND;
}

// Set output condition
//Serial.print(servoPulseWidth);  // uncomment to monitor input pulse width
//Serial.println(" ");
if (servoPulseWidth >= navThreshold) {  // nav on
  switchNavLight = true;
} else {
  switchNavLight = false;
}
//Serial.print(navThreshold);
//Serial.print(" ");

}

// Turn on or off nav lights
void setNavLight(boolean state) {
if (state && !curNavLight) {
digitalWrite(NAV_PIN_LIGHT, LOW);
} else if (!state && curNavLight) {
digitalWrite(NAV_PIN_LIGHT, HIGH);
}
curNavLight = state;
}

// Measure servo PWM signal. Range should be about 800uS to 2200uS active high
void measureServoSignal() {
Serial.print("Triggered | ");
int pinState = digitalRead(PIN_SERVO);
if (pinState == HIGH) {
// Beginning of PWM pulse, mark time
Serial.println(pinState);
servoPulseStartTime = micros();
} else {
// End of PWM pulse, calculate pulse duration in uS
servoPulseWidth = (int)(micros() - servoPulseStartTime);
Serial.println(pinState);
// If servo channel is reversed, use the inverse
if (SERVO_REVERSED) {
servoPulseWidth = (1000 - (servoPulseWidth - 1000)) + 1000;
}
}
Serial.println(servoPulseWidth);
}

Hi there,
and welcome here, so the code tags , can be used here to make it look correct easier to read and others can compile it too.
</> paste it in there.
so I see this

#define SERVO_REVERSED false // Whether or not the servo channel is reversed

should this be a Boolean? maybe?
also typically the PIN is defined before the int is Attached.
you have it the other way around.?
my .02
HTH
GL :slight_smile: PJ
:v:

Thanks for the tips. I think I do have the PIN defined first before the the interrupt is attached; right near the top.

#define PIN_SERVO 2 // RC channel input pin number - this needs to match whatever interrupt is used

Regardless I think I found the problem to be bad hardware. I moved the input to pin 4 and it started to work just fine. Not sure what happened with pin 2.

Hi there,
It’s a strapping pin.
HTH
GL :slight_smile: PJ
:v:
this part is what I was talking about.

// Set up interrupt handler.
attachInterrupt(digitalPinToInterrupt(PIN_SERVO), measureServoSignal, CHANGE);
//attachInterrupt(IRQ_NUMBER, measureServoSignal, CHANGE);

// initialize light pins as an output.

pinMode(PIN_SERVO, INPUT_PULLUP);
pinMode(NAV_PIN_LIGHT, OUTPUT);
}

can you post the working code?

 myIMU.begin();
  
  pinMode(int2Pin, INPUT);
  attachInterrupt(digitalPinToInterrupt(int2Pin), int1ISR, RISING);

The only change I made was to change the input pin from 2 to 4.

#define PIN_SERVO 4 // RC channel input pin number - this needs to match whatever interrupt is used

Otherwise it is identical to my original post.
Also I found this document https://wiki.seeedstudio.com/XIAO_ESP32C3_Getting_Started/ which helps.