Switch case inactivity timeout

May I ask whether this is the proper code for the inactivity timeout?

To-do list: -Case 1: If both buttons are pressed, the green and red leds will turn on;
-Case 2: If the left button is touched, the green led will turn on;
-Case 3: If the right button is pressed, the red led will turn on; and so on. If no buttons are touched for 10 seconds, it returns to case 1.

My question is, if I am inactive for 10 seconds and then return to case 1, will the if statement inactivity still apply to all the rest of the cases or only case 3?

unsigned long lastActivityTime = 0;
unsigned long TimeOutInterval = 10000;
  switch (State)
  {
  case 1:
   if (BothButtonIsPressed)
    State = 2
    break;
  case 2:
   if (leftbuttonIsPressed)
    lastActivityTime = millis()
    State = 3;
    break;

  case 3: 
    if (rightbuttonIsPressed)
      State = 4;  
    if (millis() - lastActivityTime >=TimeOutInterval)
      State = 1;
    break;

  case 4: 
    //Other stuff
    beak;
  }

Only to case 3, because of the "break"s. Once the state is set to 1, the next loop iteration (it is meant to have the switch...case inside the "loop()")will enter "case 1:".
Please use a better indentation, because the "if()" statements are associated to the first statement only, a good indentation'd be like:

  switch (State) {
    case 1:
      if (BothButtonIsPressed)
        State = 2
      break;
    case 2:
      if (leftbuttonIsPressed)
        lastActivityTime = millis()
      State = 3;
      break;

    case 3: 
      if (rightbuttonIsPressed)
        State = 4;  
      if (millis() - lastActivityTime >=TimeOutInterval)
        State = 1;
      break;

    case 4: 
      //Other stuff
      break;
    }

Please note on case 3 the second if() overlaps the first one when the timeout occours. I don't know if this is what you need...
And the "case 4" has a "beak;" statement (missing an "r")...

PS: it's always a better idea to post the entire code, or at least the most relevant things.

Hi @mrhueyyt ,

I guess that some curly brackets are missing behind your if statements.

E.g. in case 2 independent of whether the left button is pressed or not the state will immediately change to 3. Is this what you want?

Depending on what's going on around the switch() function it might be a quite short time for case 2.

Is the intention of the switch to handle different functions depending on the buttons pressed or to control a state machine (e.g. no reaction on left or right button before both buttons have been pressed first)?

don't believe you need a switch statement

const byte PinLed [] = { 13, 12 };
const byte PinBut [] = { A3, A2 };

enum { Off = HIGH, On = LOW };
enum { Red, Green };
enum { Left, Right };

const unsigned long TimeOutInterval = 3000;
      unsigned long msec0 = 0;
      bool          tmr;

// -----------------------------------------------------------------------------
void
loop ()
{
    unsigned long msec = millis ();

    if (tmr && msec - msec0 > TimeOutInterval)  {
        digitalWrite (PinLed [0], On);
        digitalWrite (PinLed [1], On);
    }

    if (LOW == digitalRead (PinBut [Left]))  {
        msec0 = msec;
        tmr   = true;
        digitalWrite (PinLed [Green], On);
        digitalWrite (PinLed [Red],   Off);
    }

    if (LOW == digitalRead (PinBut [Right]))  {
        msec0 = msec;
        tmr   = true;
        digitalWrite (PinLed [Green], Off);
        digitalWrite (PinLed [Red],   On);
    }
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(PinLed); n++)  {
        pinMode      (PinBut [n], INPUT_PULLUP);
        pinMode      (PinLed [n], OUTPUT);
        digitalWrite (PinLed [n], Off);
    }
}