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
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
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;
}
what 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