Interrupt not working in IDE 1.8.1, was in IDE1.6.5

I have a program that uses an interrupt to improve program timing. Board is an Uno. Here are the relevant lines:

(Global declarations)

// 60 cycle power sync signal
int intPin = 2;

boolean syncFlag = false;

(In Setup)

// interrupt for 60 cycle power signal
attachInterrupt(digitalPinToInterrupt(intPin), syncSig, FALLING);

(in Loop)

// wait for sync
syncFlag = false;
while(syncFlag == false)
{
delay(1);
}

(the ISR)

void syncSig()
{
syncFlag = true;
}

Idea is to wait for a falling edge on the signal at pin2, the ISR sets syncFlag = true; and the while loop falls through.

This works perfectly using IDE 1.6.5. When I updated to IDE 1.8.1 it no longer works. The program appears to never fall through the while loop. If I comment out the while statement the program runs as expected, but of course is no longer synchronized to the signal on pin 2.

Any suggestions?

Thanks!

 volatile boolean syncFlag = false;

Pete

El_Supremo - thanks!

Works perfectly now.

Best regards.

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

You must declare any variable that can be changed in an ISR as volatile. Otherwise, the compiler doesn't know that it has to check the value of that variable each time you use it, rather than fetching it once and reusing the value (which is typically faster and smaller, and hence an appealing target for optimization).

The optimization in recent IDE versions is a little better (actually, it's a LOT better - take a look at the size of a sketch under 1.6.5 vs 1.8.1), and it's not missing the chance to optimize that while loop anymore.