change mode with button press and case statements - error

Hi guys,

Having some trouble getting my head around case statements. Was wondering if someone could give me some pointers on where im going wrong with the code.

I've got it to compile with the first case statement, but if I add in a 2nd case I get the error "jump to case label [ -fpermissive]".

Can anyone point out why these case statements are not working?

sorry for no code tags. New to programming, but i believe most of you will probably follow this quite easily anyway. (I have an RGB that is going to fade in one mode, stay a solid colour in the other 4 modes (red, green, blue and white).

int counter = 0;
int buttonPin = 13;
int GREEN = 9;
int BLUE = 10;
int RED = 11; 
int delayTime = 5;


void setup()
{
pinMode(buttonPin, INPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(buttonPin, INPUT);
}

int redVal;
int blueVal;
int greenVal;


void loop() {
 //Handle input
   if(buttonPin == HIGH)
 {
   counter ++;
   //Reset count if over max mode number - Looking to have 5 "modes"
   if(counter == 5)
   {
     counter = 0;
   }
 }

 
 //Change mode
 switch (counter) {  // Led cross fade
 case 1:
  analogWrite (RED, 50);
  analogWrite (GREEN, 0);
  analogWrite (BLUE, 0);
  delay (1500);
 
  int redVal = 0;
  int blueVal = 0;
  int greenVal = 0;
  for( int i = 0 ; i < 255 ; i += 1 ){
       redVal += 1;
       analogWrite( RED, 0 + redVal );

    delay( delayTime );
  }
 
  redVal = 255;
  blueVal = 0;
  greenVal = 0;
  for( int i = 0 ; i < 255 ; i += 1 ){
    
    greenVal += 1;
    analogWrite( RED, 255);
    analogWrite( GREEN, 0 + greenVal );

    delay( delayTime );
  }
 
  redVal = 255;
  blueVal = 0;
  greenVal = 255;
  for( int i = 0 ; i < 255 ; i += 1 ){
    blueVal += 1;
    analogWrite( RED, 255);
    analogWrite( GREEN, 255);
    analogWrite( BLUE, 0 + blueVal );

    delay( delayTime );
  }

  delay(3000);
   
  break;

  case 2:
  digitalWrite (RED, HIGH);

  bteak;

  case 3:
  digitalWrite (GREEN, HIGH);

  break;

  case 4:
  digitalWrite (BLUE, HIGH);

  break;

  case 5:
  digitalWrite (RED, HIGH);
  digitalWrite (GREEN, HIGH);
  digitalWrite (BLUE, HIGH);
  
 
 }
}

case 2:
digitalWrite (RED, HIGH);

bteak;

Case of not seeing wood for trees? :wink:

int counter = 0;
int buttonPin = 13;
int GREEN = 9;
int BLUE = 10;
int RED = 11;
int delayTime = 5;


void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(RED, OUTPUT);
  pinMode(buttonPin, INPUT);
}

int redVal;
int blueVal;
int greenVal;


void loop() {
  //Handle input
  if (buttonPin == HIGH)
  {
    counter ++;
    //Reset count if over max mode number - Looking to have 5 "modes"
    if (counter == 5)
    {
      counter = 0;
    }
  }


  //Change mode
  switch (counter) {  // Led cross fade
    case 1:
      {
        analogWrite (RED, 50);
        analogWrite (GREEN, 0);
        analogWrite (BLUE, 0);
        delay (1500);

        int redVal = 0;
        int blueVal = 0;
        int greenVal = 0;
        for ( int i = 0 ; i < 255 ; i += 1 ) {
          redVal += 1;
          analogWrite( RED, 0 + redVal );

          delay( delayTime );
        }

        redVal = 255;
        blueVal = 0;
        greenVal = 0;
        for ( int i = 0 ; i < 255 ; i += 1 ) {

          greenVal += 1;
          analogWrite( RED, 255);
          analogWrite( GREEN, 0 + greenVal );

          delay( delayTime );
        }

        redVal = 255;
        blueVal = 0;
        greenVal = 255;
        for ( int i = 0 ; i < 255 ; i += 1 ) {
          blueVal += 1;
          analogWrite( RED, 255);
          analogWrite( GREEN, 255);
          analogWrite( BLUE, 0 + blueVal );

          delay( delayTime );
        }

        delay(3000);
      }
      break;

    case 2:
      digitalWrite (RED, HIGH);

      break;

    case 3:
      digitalWrite (GREEN, HIGH);

      break;

    case 4:
      digitalWrite (BLUE, HIGH);

      break;

    case 5:
      digitalWrite (RED, HIGH);
      digitalWrite (GREEN, HIGH);
      digitalWrite (BLUE, HIGH);
      break;
  }
}

You cannot declare/initialise variables in a case statement.
You already have global variables named redVal, greenVal and blueVal so why the need for new ones ?

once you encapsulate case 1 in braces as shown above, you will still have issues responding to the button press.

  1. using the delay in your cross-fade will block reading the pin on a press, look at Blink With our Delay example in your IDE.

  2. the test if(buttonPin == HIGH) will always return true while the pin is held high, so you are likely to get unpredictable results. Look at the State Change example in the IDE.

You cannot declare/initialise variables in a case statement.

Yes, you can:

int uselessVariable = 4;

void loop()
{
   switch(uselessVariable)
   {
      case 4:
      {
         int perfectlyValidNewVariable = 27;
         Serial.print(perfectlyValidNewVariable);
      }
      break;
   }
}

Whether you should, or not, is a different issue.

BulldogLowell:
2) the test if(buttonPin == HIGH) will always return true while the pin is held high

No it won't. It will always be false because 13 != 1. Perhaps there should be a digitalRead in there.