Using a button to "get into" a switch->case, but jumping out of it

Hey guys,
I’m stumped. I need to use a button to get into cases, which works fine, but I can’t get out of the case(s).
Here’s my code:

void loop() {


  button1.update();
  switch(button1.value) { 
  case 1:
      // one press to get into this case
      photo();
      break;
  case -1:
      // long press to get into this case
      // something happens here for say 5 minutes
      for (int a=0; a <= 20; a++){
        doThis(); 
      }
      // I want a long press (-1) to jump out of this case back to the beginning of the loop
      // inside or out of the for() statement
      break;
  case 3:
      break;
  case 4:
      break;
 }
  
/*    while (HWSERIAL.available()) {
        Serial.write(HWSERIAL.read());
    }

    while (Serial.available()) {
        HWSERIAL.write(Serial.read());
    }
*/
}

Any help is appreciated!

You're jumping into the 'switch case' regardless of whether or not a button is pressed. (I haven't looked any deeper - that immediately caught my eye.)

I don't know how the button library works, but assuming a value larger than 0 is returned by 'update()' when a button is pressed, maybe something like:-

if(button1.update())
{
  // Do the switch case stuff here
}

And you should really post the full compileable code, not just a snippet.

Two ways I can think of. The first one is to simply use a return; the code will leave the loop and next enter it again.

void loop()
{
  switch(variable)
  {
    case 1:
    // do something
      break;
    case -1:
    // do something
      return;
  }
}

The second option is to use a flag; in the below I called it fAbort;

void loop()
{
  bool fAbort = false;
  switch(variable)
  {
    case 1:
    // do something
      break;
    case -1:
    // do something
      fAbort = true;
  }

  if(fAbort == false)
  {
    // do other stuff in the loop
  }
}

The first one does not waste space on a variable and additional testing but having return statements all over the show might not be your style of programming. I personally (would) use the first one.

Note that neither solution jumps to the beginning of the loop; they all leave the loop and next the loop is entered again.

sterretje: Two ways I can think of. The first one is to simply use a return; the code will leave the loop and next enter it again.

void loop()
{
  switch(variable)
  {
    case 1:
    // do something
      break;
    case -1:
    // do something
      return;
  }
}

The second option is to use a flag; in the below I called it fAbort;

void loop()
{
  bool fAbort = false;
  switch(variable)
  {
    case 1:
    // do something
      break;
    case -1:
    // do something
      fAbort = true;
  }

  if(fAbort == false)   {     // do other stuff in the loop   } }



The first one does not waste space on a variable and additional testing but having return statements all over the show might not be your style of programming. I personally (would) use the first one.

Note that neither solution jumps to the beginning of the loop; they all leave the loop and next the loop is entered again.

When the code within a particular 'case' has executed, the program leaves the 'switch...case' anyway. (Provided the 'case' ends with "break;".) I assumed that the problem is that it's immediately being re-entered whether or not a button was pressed.

OldSteve, you might be right; I assumed (it’s the mother of all evil, I know) that OP just wanted to jump out of the switch.

This comment in OP’s code is a bit confusing as the code gets into the specific case with a long press

// I want a long press (-1) to jump out of this case back to the beginning of the loop
// inside or out of the for() statement

Maybe OP only wants a single execution of doThis() to keep the loop responsive in which case replacing the for-loop by a global variable and an if statement might do the trick.

void loop() {
  static int counter = -1;

  int buttonval;

  buttonval = button1.update();
  switch (buttonval) {
    case 1:
      photo();
      break;
    case -1:
      // if new 'instance' of -1, initialize counter
      if (counter == -1)
        counter = 0;
      // do doThis() a maximum of 20 times
      if (counter < 20)
      {
        // execute once
        doThis();
        // update counter
        counter++;
      }
      break;
    case 3:
      break;
    case 4:
      break;
  }

  // set the counter to an 'invalid' value when buttonval changed from -1
  if (buttonval != -1)
  {
    counter = -1;
  }

  /*    while (HWSERIAL.available()) {
          Serial.write(HWSERIAL.read());
      }

      while (Serial.available()) {
          HWSERIAL.write(Serial.read());
      }
  */
}

sterretje: .....I assumed (it's the mother of all evil, I know) that OP just wanted to jump out of the switch.....

We all have to make asumptions at times, and read posts 'between the lines'.

That's what I did. I just saw it differently to you, and assumed that what was really going on was that after execution left the 'switch...case', it was immediately jumping back in with the value of the last button press, at the next iteration of the 'loop()'.

If we have his full code, and a link to the library, things will be easier. :)

The library is here https://github.com/tmittet/buttontracker, sorry I forgot that. I'll test out your suggestions.