Help! I am stuck with my Switch Case commands

So I have been working in electronics for many years and have decided to have a go at learning a bit of software.

I am also an avid paintballer so what I am trying to do is make a prop.

I am using a UNO with a DFROBOT LCD Shield with 5 buttons.

So my issue is I am using Case statements as follows

State 0 //Welcome msg
1 //Set game timer
2 //Begin timer countdown
3 //Arm bomb
4 //Bomb armed continue countdown
5 //Disarm bomb
6 //Bomb disarmed continue countdown
7 //Armed bomb detonated
8 //Disarmed bomb times up

In my code I have got it to arm the bomb, then continue countdown then go to state 7. But i am trying to add code the return to state 3 if the arm case does not complete and i can not find anywhere that tells you how to exit case 4 and go back to case 3. it always seems to go to case 7.

 case 4: // Arming bomb
    {
      do {
        if (analogRead(0) < 790){
      lcd.clear();
      lcd.setCursor(0,0);
      
      lcd.print("  ARMING BOMB   ");
      lcd.setCursor(0,1);
      lcd.print((char)0);
      lcd.print("               ");
      delay(188);
        }
        if (analogRead(0) < 790){
      lcd.setCursor(0,1);
      lcd.print((char)1);
      delay(188);
        }
        else {
          laststate = state -1;
          break;
        }
        if (analogRead(0) < 790){
      lcd.setCursor(0,1);
      lcd.print((char)2);
      delay(188);
        }
        else {
          laststate = state -1;
          break;
        }
        if (analogRead(0) < 790){
      lcd.setCursor(0,1);
      lcd.print((char)3);
      delay(188);
        }
        else {
          laststate = state -1;
          break;
        }
        if (analogRead(0) < 790){
      lcd.setCursor(0,1);
      lcd.print((char)4);
      delay(188);
        }
        else {
          laststate = state -1;
          break;
        }
        if (analogRead(0) < 790){
      lcd.setCursor(1,1);
      lcd.print((char)0);
      delay(188);
        }
        else {
          laststate = state -1;
          break;
        }
        if (analogRead(0) < 790){
      lcd.setCursor(1,1);
      lcd.print((char)1);
      delay(188);
        }
        else {
          laststate = state -1;
          break;
        }
        if (analogRead(0) < 790){
      lcd.setCursor(1,1);
      lcd.print((char)2);
      delay(188);
        }
        else {
          laststate = state -1;
          break;
        }
        if (analogRead(0) < 790){
      lcd.setCursor(1,1);
      lcd.print((char)3);
      delay(188);
        }
        else {
          laststate = state -1;
          break;
        }
        if (analogRead(0) < 790){
      lcd.setCursor(1,1);
      lcd.print((char)4);
      delay(188);
        }
        else {
          laststate = state -1;
          break;
        }
        if (analogRead(0) < 790){
      lcd.setCursor(2,1);
      lcd.print((char)0);
      delay(188);
        }
        else {
          laststate = state -1;
          break;
        }
        if (analogRead(0) < 790){
      lcd.setCursor(2,1);
      lcd.print((char)1);
      delay(188);
        }
        else {
          laststate = state -1;
          break;
        }
        if (analogRead(0) < 790){
      lcd.setCursor(2,1);
      lcd.print((char)2);
      delay(188);
        }
        else {
          laststate = state -1;
          break;
        }
        if (analogRead(0) < 790){
      lcd.setCursor(2,1);
      lcd.print((char)3);
      delay(188);
        }
        else {
          laststate = state -1;
          break;
        }
        if (analogRead(0) < 790){
      lcd.setCursor(2,1);
      lcd.print((char)4);
      delay(188);
        }
        else {
          laststate = state -1;
          break;
        }
      }
      while (analogRead(0) < 790); //if ok button has been released it will go to state 7 once the do commands have compleated
      state = 7;
      
    }
    break;

Any advise you can give to educate my bad coding skills will be dandy thanks :slight_smile:

Only with this piece of code I can't say anything. Can you post the complete code?

i can not find anywhere that tells you how to exit case 4 and go back to case 3. it always seems to go to case 7.

At the end of case 4 you have

state = 7;

Can you see why it might go to case 7 ?

if whatever you're trying to do has failed: state = 3 else state = 7.

possibly since you have state = 7; at the end.

What i like to do when controlling flow in a program is to utilise separate functions which you then just call in the main loop. You need to make sure you have cases for things you don't want to happen.

So like maybe a do nothing case.

luisilva: Only with this piece of code I can't say anything. Can you post the complete code?

The code is more than the 9000 character limit so i am unable to post the full code.

Henry_Best: if whatever you're trying to do has failed: state = 3 else state = 7.

So you suggest I continue the if statement at the end of the case to control the flow?

The code is more than the 9000 character limit so i am unable to post the full code.

So, attach it.

You should investigate and implement using a State Machine library.

I have used this one with great success: SM

darkfirepheonix: So you suggest I continue the if statement at the end of the case to control the flow?

If that's what you want to it to do, yes. I would add a new if statement to test if the thing you're trying to do has failed or succeeded. Or do I have your problem wrong?

Hey Guys,

Thanks for the advise everyone. I have something that works now.
There is 1 last feature I would like to add.
I would like to randomize the delay when disarming the bomb between 188ms and 375ms (*80 = 15 to 30 seconds).
Any idea on how to do this as when using the random function it just threw up errors.

Code has been attached to this post

paintball_with_LCD_v6.ino (29.4 KB)

when using the random function it just threw up errors.

Post the code and the errors.

You need to make sure you have cases for things you don't want to happen.

So like maybe a do nothing case.

Which is pretty much exactly what default is for. For example:

switch (x) {
   case 1:
      // do appropriate stuff here
      break;
   case 2:
      // do appropriate stuff here
      break;
   case 3:
      // do appropriate stuff here
      break;
   default:
      Serial.print("Error! You should never see this. x = ");
      Serial.println(x);
      break;
}

econjack: Which is pretty much exactly what default is for. For example:

switch (x) {
   case 1:
      // do appropriate stuff here
      break;
   case 2:
      // do appropriate stuff here
      break;
   case 3:
      // do appropriate stuff here
      break;
   default:
      Serial.print("Error! You should never see this. x = ");
      Serial.println(x);
      break;
}

And to ensure the error has been acknowledged :

default:
      Serial.print("Error! You should never see this. x = ");
      Serial.println(x);
      Serial.println("To restart, press reset.");
      while(1);
      break;