Arduino Long Switch Statement Bug?

I created a long switch statement that one of the case conditions (the last one) is not being executed. I checked my code and the variable used in the switch statement goes to the correct type and value, but still this case statemente is begin missed.

I created another switch statemente below and it works fine now. Also, if I comment out the huge code before the last case statement, it is executed properly. What is going on?

Please post a full sketch that illustrates the problem, using code tags when you do to make it easier to read and copy the code for examination

Have you tried more Serial.print statements to see what the variables and program flow are doing ?

Hi, @fernandoi

To add code please click this link;

Thanks... Tom.... :smiley: :+1: :coffee: :australia:

If you declare a var inside the switch it creates a subtle bug. Clean any and all var creation within the switch and things should straighten out. That FLOAT needs to be defined OUTSIDE the switch.

There is also the distinct possibility that there is another variable of the same name with a different scope that is declared somewhere else in the code that has not been posted

The problem caused by declaring a variable inside a case statement can be avoided by either not doing it or by enclosing the code block for the case in curly braces

However, neither of those solutions will fix the potential problem of different variables with the same name and different scopes

Is this "huge code" contains a variable declarations? it may be the problem...

Anyway, please show the WHOLE code, inserted it to the forum using code tags.

Help us help you.

Hi, thanks everyone for the suggestions. The solution was to remove all local variable declaration from within the case statements. The funny thing is that I had to remove them from other case statements that were indirectly affecting the execution of the last case statement. Also, there were no name conflicts anywhere in the code...

I am glad that you got it working but I would still like to see the original code in full

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.