Working with Case & Switches

I have been working with this code for sometime and would like to turn it into a multi stage program. Once a person finds the place the box is suspose to be, I want to make it change to a different set of cords and have them to their and then change to another set of cords and so on.

Now I have started with the first switch and case #. It seems to complile just fine. But I am unsure as to where to place the “break” command. I did place it at where I wanted to break and it seems to compile. But when I start to place the “case 2:”, I don’t seem to be placing it in the correct location.

With it in the current location I get, “SlimJim_Inverse_Stages_and_switches:340: error: expected unqualified-id before ‘case’”.

I have tried searching for info, but I get more searchers about C++ than anything else and I can’t really make out the differences.

Does anyone have any suggestions?

Thanks
Chip

inverse with stages.txt (11.1 KB)

switch(varName){
case 1:
//
break;
case 2:
//
break;
case 'A': 
//
break;
} // end switch:case

Don't forget default: !!

CrossRoads:

switch(varName){

case 1:
//
break;
case 2:
//
break;
case 'A':
//
break;
} // end switch:case

This I think I understand. I just the placement of it inside my code.

KeithRB:
Don't forget default: !!

I don't know what you mean.

'switch' works with integer values only

switch ( random('a', 'z') )
{
    case 'a':
        ... some useful code here ...
        break;

    case 'b':
        ... some useful code here ...
        break;

    case 'c':
        ... some useful code here ...
        break;

    case 'd':
        ... some useful code here ...
        break;

    case 'd':
    case 'e':
    case 'f':
    case 'q':
        ... some useful code here ...
        break;

    default:
        // handle all other values ...
        break;
}

@lloyddean, what are you saying?

"In particular, a switch statement compares the value of a variable to the values specified in case statements. When a case statement is found whose value matches that of the variable, the code in that case statement is run. "

The examples show case 1, case 2, and I know case 'a', case 'b' work also.

Is a "default" case required? Is that why I am getting the error?

CrossRoads:
@lloyddean, what are you saying?

"In particular, a switch statement compares the value of a variable to the values specified in case statements. When a case statement is found whose value matches that of the variable, the code in that case statement is run. "

The examples show case 1, case 2, and I know case 'a', case 'b' work also.

No, a default case is not required. If none of the cases are met, the code just continues on after the end }.

CrossRoads:
No, a default case is not required. If none of the cases are met, the code just continues on after the end }.

Can you tell if I am placing it correct in my code?

I only see a Case 1, it looks good to start.

The compiler error message you are quoting, should state which line of the program the error is in. Well, which line is it ??

Even if the compiler accepts it, coding a switch statement with 1,2,'A', ... is simply unwise programming.

It is difficult to figure out what it will do. Unless you are trying to be deliberately obscure.

Either show the whole sketch or more code above and below the 'switch' statement.

I was showing the use of 'default' as well as multiple 'case' statements dropping into the same code.

'case' statements need not be in any particular order either.

No I'm not trying to be deliberately obscure. I'm sorry if it came across like that. It would be just my lack of know how. Another suggestion was to put things into different functions because of the massive amount of code that i already have. Then use the switch cases to call those functions.

What do you suggest using with the switch statements other than 1,2 and so on. I am working with a multi stage Geocache and was going to use stage 1 then stage 2 and so on. I so far have been able to have the "stages" change like I want them to. Or so I think I do.

michinyon:
Even if the compiler accepts it, coding a switch statement with 1,2,'A', ... is simply unwise programming.

It is difficult to figure out what it will do. Unless you are trying to be deliberately obscure.

Thank you for looking at the code. I removed the Case 2 because of the error and started with that. I try building the functions and try calling them from there. I just have to figure out how in incorporate some of the code from the void setup into the void loop/function call.

CrossRoads:
I only see a Case 1, it looks good to start.

Well I changed the orginal loop to a function and then created a new loop and added the switches and case numbers to it. Everything complies just find with no errors. But when I upload load, the arduino gets to the end of the void setup and then stops. It doesn’t seem to get into the void loop.

I even tried adding a “default” thinking it might would use that, but it doesn’t.

Any suggestions?

Chipper

I have attached the new code.

inverse with stages.txt (11.3 KB)

Your program looks ridiculously complicated.

How do you actually "know" that it never makes it into the loop() function ?

michinyon:
Your program looks ridiculously complicated.

How do you actually "know" that it never makes it into the loop() function ?

Well, it would get to a certin point and then stop. I knew exactly where it was stopping at.

I later discovered that I had a "void" in the case section and that was what was making it stop. After removing the void, it worked great!!

Now on to do the rest of the code.