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);
}