You can try to edit your code like this:
// RC input settings
#define PIN_SERVO 4 // 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
// Strobe settings
#define STB_PIN_LIGHT 5 // Strobe light output pin number
#define STB_BLINK_INTERVAL 1500000 // Blink interval for strobe light in microseconds
// Anti-collision beacon settings
#define ACB_PIN_LIGHT 7 // Anti-collision beacon output pin number
#define ACB_FADE_MIN 0 // Minimum fade level for beacon (0-255)
#define ACB_FADE_MAX 75 // Maximum fade level for beacon (0-255)
#define ACB_FADE_INTERVAL 8000 // Fade step interval, in microseconds (lower numbers = faster fade)
// Var declarations
volatile unsigned long servoPulseStartTime;
volatile int servoPulseWidth = 0;
volatile bool newServoData = false;
// Strobe via servo, acb is on whenever we have power!
boolean curStrobeLight = false;
boolean switchStrobeLight = false;
unsigned long lastFadeTime = 0;
unsigned long lastStrobeTime = 0;
int currentFade = ACB_FADE_MIN;
int fadeDirection = 1;
// 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);
// Initialize input pin.
pinMode(PIN_SERVO, INPUT_PULLUP);
// Initialize light pins as an output.
pinMode(STB_PIN_LIGHT, OUTPUT);
pinMode(ACB_PIN_LIGHT, OUTPUT);
// Start serial communication for debugging
Serial.begin(9600);
}
// the loop function runs over and over again forever
void loop() {
unsigned long currentTime = micros();
if (newServoData) {
noInterrupts(); // disable interrupts while accessing shared variable
int pulseWidth = servoPulseWidth;
newServoData = false;
interrupts(); // re-enable interrupts
checkServo(pulseWidth);
Serial.print(pulseWidth);
Serial.println(" ");
}
// Check if it's time to fade the anti-collision lights.
if ((currentTime - lastFadeTime) > ACB_FADE_INTERVAL) {
doFade();
lastFadeTime = currentTime;
}
// Check if it's time to flash the strobes
setStrobeLight(switchStrobeLight);
if (switchStrobeLight) {
if ((currentTime - lastStrobeTime) > STB_BLINK_INTERVAL) {
doStrobe();
lastStrobeTime = currentTime;
}
}
}
// Check servo signal, and decide whether to turn things on or off
void checkServo(int pulseWidth) {
// Strobe Lights
// Modify threshold to prevent flicker
int strobeThreshold = SERVO_MID;
if (!curStrobeLight) {
// Strobe are not on; adjust threshold up
strobeThreshold += SERVO_DEAD_BAND;
} else {
// Strobe are on, adjust threshold down
strobeThreshold -= SERVO_DEAD_BAND;
}
// Set output condition
if (pulseWidth >= strobeThreshold) { // strobe on
switchStrobeLight = true;
} else {
switchStrobeLight = false;
}
}
// Turn on or off strobe lights
void setStrobeLight(boolean state) {
curStrobeLight = state;
}
// Fade anti-collision LEDs
void doFade() {
currentFade += fadeDirection;
if (currentFade == ACB_FADE_MAX || currentFade == ACB_FADE_MIN) {
// If we hit the fade limit, flash the beacon, and flip the fade direction
if (fadeDirection == 1) analogWrite(ACB_PIN_LIGHT, 255); // Rotating ACB
Pause(millis(), 100);
fadeDirection *= -1;
}
analogWrite(ACB_PIN_LIGHT, currentFade);
}
// Strobe double-blink
void doStrobe() {
digitalWrite(STB_PIN_LIGHT, HIGH);
Pause(millis(), 75);
digitalWrite(STB_PIN_LIGHT, LOW);
Pause(millis(), 50);
digitalWrite(STB_PIN_LIGHT, HIGH);
Pause(millis(), 50);
digitalWrite(STB_PIN_LIGHT, LOW);
}
// Measure servo PWM signal
void measureServoSignal() {
int pinState = digitalRead(PIN_SERVO);
if (pinState == HIGH) {
// Beginning of PWM pulse, mark time
servoPulseStartTime = micros();
} else {
// End of PWM pulse, calculate pulse duration in uS
unsigned long pulseEndTime = micros();
servoPulseWidth = (int)(pulseEndTime - servoPulseStartTime);
newServoData = true;
// If servo channel is reversed, use the inverse
if (SERVO_REVERSED) {
servoPulseWidth = (1000 - (servoPulseWidth - 1000)) + 1000;
}
}
}
void Pause(unsigned long startMillis, int Duration) {
while (millis() - startMillis < Duration);
}