Switch structure and variables?

hello again, ive been looking into switch control structures and i have some un answered questions.

could i direct my code to a certain case e.g skip case 1 to go to 2. im using a rotary encoder as a way to navigate a menu to then set the encoder and press button to do a certain color. then end goal is rather to press the button 5 times to go through each color just do it with the encoder and a button press

switch (state) 
{ 
 case 1: //Red color
 digitalWrite(redLED,HIGH);// when variable = 1 do this
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,LOW);
 old = state;  // when button is presed satae will always + 1 this makes the old the previous nymber
 break;
 
 case 2:  //when variable = 2 do this
 //Green color
 digitalWrite(redLED,LOW);
 digitalWrite(greenLED,HIGH);
 digitalWrite(blueLED,LOW);
 old = state;  // state = 2 + old = 2

 break;
 case 3: //Blue color
 digitalWrite(redLED,LOW);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,HIGH);
 old = state; 

 break;
 case 4: //Purple color
 digitalWrite(redLED,HIGH);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,HIGH);
 old = state;

 break;
 case 5: //White color
 digitalWrite(redLED,HIGH);
 digitalWrite(greenLED,HIGH);
 digitalWrite(blueLED,HIGH);
 old = state; 

 break;
 default:
 digitalWrite(redLED,LOW);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,LOW);
 old = 0; // when buttons pressed 5 times it goes here as theres no more cases and resets old to 0/ start.  since old = state it will also makw state 0
 break;
 }
}

this is my 1st time using switch so i may have it all thought out wrong

i havent added the encoder code or lcd yet

Your code will be directed based on the value of "state". Set state based on the encoder.

1 Like

This question makes no sense, because this it what switch does

bad example, skip case 1 to 5
sorry

same, why don’t you start with reading about switch

i did nothing said about using variables to skip to a certian case

I don’t believe you, there are like 1000000000000000000000 pages about switch on the internet. state = 4 will skip all cases but 4

there java tho im c++

oh, this changes everything

Read this

(Or one of the 99999999999 others)

this is great, dont answer my question tho, it shows me an example of what ive done. inneed an example of what i need to do / a good hint . ive googles it and its jsut more examples of what I've done and how to do it not what i want to do

if endoer = 10 && button press go to case 5 skip rest this kinda thing

did say im new i get how to write and do it im just looking for a simple not even code example of how ,ive searched it and i cant find something that is similar to what im trying to do

Yep. Clearly.
Look, you've not even showed us how you come to the value of "state", which controls which of many cases in your switch gets executed. Nor have you clearly stated your intent. Step back and describe in words (clearly, with punctuation and sentences), what your switch is intended to do. I'm getting the feeling that you're using it very inappropriately, but it's darn hard to tell from what you've said so far.
C

if ((endoer == 10) && (digitalRead(buttonPin) == LOW)) {
  state = 5; 
} else {
  state = 1; // whatever makes sense
}

switch (state) { 
 case 1: //Red color
 digitalWrite(redLED,HIGH);// when variable = 1 do this
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,LOW);
 old = state;  // when button is presed satae will always + 1 this makes the old the previous nymber
 break;
 
 case 2:  //Green color
 digitalWrite(redLED,LOW);
 digitalWrite(greenLED,HIGH);
 digitalWrite(blueLED,LOW);
 old = state;  // state = 2 + old = 2

 break;
 case 3: //Blue color
 digitalWrite(redLED,LOW);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,HIGH);
 old = state; 

 break;
 case 4: //Purple color
 digitalWrite(redLED,HIGH);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,HIGH);
 old = state;

 break;
 case 5: //White color
 digitalWrite(redLED,HIGH);
 digitalWrite(greenLED,HIGH);
 digitalWrite(blueLED,HIGH);
 old = state; 

 break;
 default:
 digitalWrite(redLED,LOW);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,LOW);
 old = 0; // when buttons pressed 5 times it goes here as theres no more cases and resets old to 0/ start.  since old = state it will also makw state 0
 break;
}

1 Like

what :rofl: im trying to make a menu that when pressed with button will go to a different menu. then if the encoder is a set number ie 10 and u press a button it will select the case/color to be

thanks so much <3

Okey dokey, then. Ciao.

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