I have seen several questions about a limitation for the number of switch cases.
There is defenitely one.
With the simliest possible code I run out of cases at 0...63.
With more complicated content this happens earlier.
Would be worth mentioning in the IDE documentation (ie Arduino Reference).
There is no limit of number of switch cases in Arduino. If you encounter this limitation, it is either a lack of memory or a bug in your program.
I increased the number of cases in your code and it works fine from 0 to 70.
I am absolutely sure that if you add the number of cases, for example, to 256, the code will also work. You can check it youself, I am too lazy to type so many identical lines on the keyboard.
But in general, if you need a switch with more than 10-15 cases, this is a reason to think about whether you are designing the program correctly.
I've found that if you sort your cases in one order or the other, that after about 7 or 8 cases, a switch saves program spaces over ifs. It certainly operates faster.
Which board are you compiling for, and what exact error are you getting?
I increased the number of cases to 2211 on an UNO with no problems - that's when maxed out the available program memory.
Something that is a known problem with switch/case is initializing a variable when declaring it inside a case. Unless you limit the scope of the variable by enclosing the case in curly brackets, any subsequent case can never be reached.
Ask @gah000.
He asked a question and did not appear on the forum again. Last time before this question he came in 2018, next time he should be expected no earlier than 2026
I experienced a problem with switch / case whith a code to implement a 44key remote control IR unit. It went on to say case 29 and cases 30 ff where just simply ignored.
I would at least expect an error message. That I can live with - but ignoring command lines????
Anyhow, I made up the simplest code I could think of (the one I had attached) and experienced the same behavior.
This code as is does run! But: Once you include the cases which I commented out you should see the effect (I used an Arduino Uno R3). If the code – on your h/w - does run just expand it further. You soon or later should see the same effect.
I consider this manner unacceptable. If this is not a bug but a “feature” I would appreciate if it was mentioned in the commands documentation.
Esp. newbies could have a real hard time finding out why their code doesn’t run as expected.
Also, when searching for a limitation to switch / case I found several entries mentioning or asking for limitations but no satisfying answer to the problem. Just the usual “smart” comments.
Not on the simulator, not in real life on an UNO I happen to see hooked up to my 'puter.
163
default reached at: 164
A little text edit wizardy and I have 165 cases in my switch. At no point did the code do anything suspicious.
I imagine there is a limt. It would be my hope that the compiler would tell you that you had reached it, even if your close friends wouldn't have told you that ship sailed a long time ago and adviced you to seek a different approoach.
Thank you very much for the time and efford.
That is interesting. So I expect a problem with my h/w (will be interesting to find out. Having worked on very large systems h/w 40..50y ago I will never say "can't be h/w").
Also, I will try the latest IDE version, of course.
Thx again!
Funny.
I am using a different Uno and I switched IDE from 1.8.13 to 2.0.1 (which I don't see superior at all (yet) except that it is a little more Eclipse like). But I still get the same behavour. It silently creeps out after case#64. Same W10_64bit underneath.
Strange!
I tested your code on Arduino Nano, IDE v1.8.3 Windows7 x64
It works fine with 70 switch cases.
Many others tested the code up 2000+ switch-cases. The result was the same.
I am sure that Arduino IDE hasn;t limit of switch cases except of memory limitation. I also think that your case is not a "hardware problem".
I think that the problem is with your code.