Breaking a switch case from an ISR I'm struggling with something. My CPU sits in a switch case state machine like this:

switch(expression) {

   case constant-expression_1  :
      statement(s);
      break; /* optional */

   case constant-expression_2  :
      statement(s);
      break; /* optional */

   /* you can have any number of case statements */
   default : /* Optional */
   statement(s);
}


I have an interrupt that occurs on a button press. In the interrupt, I change the switch expression to the case I want to go next, like this:

void ISR(void){
   expression = constant-expression_2;
}


After the interrupt has been handled, I want the CPU to return directly to the case being switched to, and to not continue where it left off.

Does anyone have any good ideas on how to implement this, if it's possible? I'd prefer to not have to wait for the code to finish what was happening in the previous case before going to the one being switched to.

Thanks in advance!
Most of the time this isn't a problem, because code normally runs pretty fast. The problem, most of the time, is the use of delays. What I did was use my own delay function that checks if the expression has changed, and if so, immediately returns.

Below is my code. I had to modify it a bit, so it might not work out of the box, but will give you the idea as to how.

volatile byte expression;
volatile boolean expressionChanged = false;

switch(expression) {

   case constant-expression_1  :
      statement(s);
      myDelay(1000);
      statement(s);
      break; /* optional */

   case constant-expression_2  :
      statement(s);
      myDelay(500);
      statement(s);
      break; /* optional */

   /* you can have any number of case statements */
   default : /* Optional */
      statement(s);
}

void myDelay(int ms)
{
    uint32_t start = micros();
    while (ms &gt; 0 &amp;&amp; !expressionChanged)
    {
        //yield();not implemented in arduino
        if ( (micros() - start) &gt;= 1000)
        {
            ms--;
            start += 1000;
        }
    }   
}

void ISR(void){
   expression = constant-expression_2;
   expressionChanged = true;
}