Search code examples
arduinoswitch-statementarduino-c++

Weird things with Switch Case Arduino


Well I know it sounds pretty weird, but I made code in Arduino which works find when i just use an if statement for every different option. But that's what switch cases are for, so I tried it and for some weird reason it goes into and executes the code for standbyStatus==0 and for standbyStatus==1, but for standbyStatus==2 or in default, nothing is executed whatsoever. Note that the variable standbyStatus is set to 2 by the code for status 1. I can check that over the serial monitor, but the test Serial.print("case 2.1"); in the 2 -code doesn't appear. Only END of standbymanager. Again, when I use if, everything works as it should. Here is first the code with if and then the same code just as a switch case:

Also, that method is called over and over again. This is my loop.

void loop() 
{
  t= millis();
  newAudio();
  calcAudioScale();
  if(standbyStatus==0){changeLEDs();}
  standbymanager();
}

That method is basicly checking if there is some sound for like 40ms or sth, and if there is not, it'll change to standbyStatus One, which is a smooth dim up of the LEDs, which will be interrupted if there is sound, but if there isn't, it'll change to standbyStatus Two, which is just a solid light and checking just every second if there is sound. If so, it'll change back to stabdbyStatus zero.

Code #1 'IF':

void standbymanager()
{          
  if(standbyStatus==0)
  {
            Serial.print(" ");
            Serial.print("case0");
            if(audio[standbyTrigger]<0)
            {
              idleCounter++;
            }
            if(idleCounter>40)
            {
              standbyStatus = 1;
            }
  }

    if(standbyStatus==1)
  {
            Serial.print(" ");
            Serial.print("case1");
            idleCounter = 0;
            float power = 0;
            for(int i=0; i<100; i++)
            {
              if(audio[standbyTrigger]>0)
                {
                  standbyStatus = 0;
                  return;
                }
              power = i/100;
              analogWrite(red, 255 - r*power);
              analogWrite(green, 255 - g*power);
              analogWrite(blue, 255 - b*power);
              delay(20); 
            }
            standbyStatus = 2;
            return;
  }

    if(standbyStatus==2)
  {
            Serial.print(" ");
            Serial.print("case 2.1");
            if(audio[standbyTrigger]>10)
                {
                  standbyStatus = 0;
                  return;
                }
            analogWrite(red, 255 - r);
            analogWrite(green, 255 - g);
            analogWrite(blue, 255 - b);
            Serial.print(" ");
            Serial.print("case2.2");
            delay(1000);
           return;
  }
  Serial.print(" ");
            Serial.print("END of StandbyManager");
}

Code #2 'Switch Case'

void standbymanager()
{          
  switch(standbyStatus)
  {
    case 0:
            Serial.print(" ");
            Serial.print("case0");
            if(audio[standbyTrigger]<0)
            {
              idleCounter++;
            }
            if(idleCounter>40)
            {
              standbyStatus = 1;
            }
            break;

    case 1:
            Serial.print(" ");
            Serial.print("case1");
            idleCounter = 0;
            float power = 0;
            for(int i=0; i<100; i++)
            {
              if(audio[standbyTrigger]>0)
                {
                  standbyStatus = 0;
                  break;
                }
              power = i/100;
              analogWrite(red, 255 - r*power);
              analogWrite(green, 255 - g*power);
              analogWrite(blue, 255 - b*power);
              delay(20); 
            }
            standbyStatus = 2;
            break;

    case 2:
            Serial.print(" ");
            Serial.print("case 2.1");
            if(audio[standbyTrigger]>0)
                {
                  standbyStatus = 0;
                  break;
                }
            analogWrite(red, 255 - r);
            analogWrite(green, 255 - g);
            analogWrite(blue, 255 - b);
            Serial.print(" ");
            Serial.print("case2.2");
            delay(1000);
            break;

    default:
            standbyStatus=0;
            Serial.print(" ");
            Serial.print("default");
            delay(1000); 
            break;
  }
  Serial.print(" ");
            Serial.print("END of StandbyManager");
}

Solution

  • I guess the errors come from case 1:

    • variables can't be declared inside a case statement
    • the break inside the for doesn't break from the switch statement

    You can refactor it to:

    case 1: 
    {
            Serial.print(" ");
            Serial.print("case1");
            idleCounter = 0;
            float power = 0;
            bool flag = false;
            for(int i=0; i<100; i++)
            {
              if(audio[standbyTrigger]>0)
                {
                  standbyStatus = 0;
                  flag = true;
                  break;
                }
              power = i/100;
              analogWrite(red, 255 - r*power);
              analogWrite(green, 255 - g*power);
              analogWrite(blue, 255 - b*power);
              delay(20); 
            }
            if (flag == false) {
               standbyStatus = 2;
            }
            break;
    }