Using Switch Case Statements

Hello, I have the following code

/*

*/
#include <SoftwareSerial.h>
#include <Servo.h>
#define relay 8
#define ESC 9
Servo steering;
SoftwareSerial HC12(2,3);
void setup() {
    HC12.begin(9600);
    Serial.begin(9600);
    steering.attach(10);
}

void loop() {
   if(Serial.available()>1){
     int val=HC12.read();
     switch (val){
       case a:
       {
         analogWrite(ESC,250);
         digitalWrite(relay, HIGH);
       }
       case b:
       {
         analogWrite(ESC, 204);
         digitalWrite(relay, HIGH);
       }
       case c:
       {
         analogWrite(ESC,153);
         digitalWrite(relay,HIGH);
       }
       case d:
       {
         analogWrite(ESC,102);
         digitalWrite(relay,HIGH);
       }
       case e:
       {
         analogWrite(ESC,51);
         digitalWrite(relay,HIGH);
       }
       case f:
       {
         analogWrite(ESC,0);
         digitalWrite(relay,LOW);
       }
       case g:
       {
         analogWrite(ESC,51);
         digitalWrite(relay,LOW);
       }
       case h:
       {
         analogWrite(ESC,102);
         digitalWrite(relay,LOW);
       }
       case i:
       {
         analogWrite(ESC,153);
         digitalWrite(relay,LOW);
       }
       case j:
       {
         analogWrite(ESC,204);
         digitalWrite(relay,LOW);
       }
       case k:
       {
         analogWrite(ESC,255);
         digitalWrite(relay,LOW);
       }
     }
   }
}

and it doesn’t compile because it thinks the letters are integers which were not declared. Is there anyway I can still use the letters in my cases? Or should I just use numbers instead? Or maybe a series of if statements? Any other ideas?

case 'a':etc?

Thank you

Adding break statements to your switch could be useful.

Whandall:
Adding break statements to your switch could be useful.

Probably essential.

I'm curious what the variables a through k are and where are they defined. Also, case statement blocks do not need a set of braces:

  • case a:*
  • analogWrite(ESC,250);*
  • digitalWrite(relay, HIGH);*
  • break; // Probably needed as Whandall pointed out*

If you want to "use letters" for your cases, you need to define the variables a through k in your program. Their values must equal val for that case to be executed. I would also suggest a default statement block for those values that are not covered:

  • default:*
  • Serial.print("I shouldn't be here. val equals: ");*
  • Serial.println(val);*
  • break;*

econjack:
If you want to "use letters" for your cases, you need to define the variables a through k in your program.

No, that is not allowed - a through k would have to be consts

Hello,
I’m new in Arduino,
I made this Sketch and it is not working, can anyone help me what to use as three line encoder?

//
//
//
#define SWITCH(a,b,c) switch(a + b<<3 + c<<4)
#define CASE(a,b,c) case(a + b<<3 + c<<4)

int buttonPin2 = 2;
int buttonPin3 = 3;
int buttonPin4 = 4;

void setup()
{
Serial.begin(9600);
Serial.println(“start…”);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);

}

void loop()
{
int a = digitalRead(buttonPin2);
int b = digitalRead(buttonPin3), c = digitalRead(buttonPin4);
int x = 8;

SWITCH(a,b,c)
{
CASE(0,0,0):
{
x=0;
break;
}
CASE(1,0,0):
{
x=1;
break;
}
/*CASE(0,1,0):
x=2;
break;
CASE(0,0,1):
x=3;
break;
CASE(1,1,0):
x=4;
break;
CASE(1,0,1):
x=5;
break;
CASE(0,1,1):
x=6;
break;
*/
}
Serial.println(x);
delay(500);
}

Cases have to be compile-time constants
I'm surprised that code compiles

switch/case takes a single integer as its argument.

You are trying to use 3

Oh wait, a,b,c are the macro arguments as well.
Hmm

I hate the phrase "it is not working" - what do you mean?

Also, I'm not sure why you're spreading single bits quite so wide.

You do not need a switch for a simple binary display.

const byte buttonPin2 = 2;
const byte buttonPin3 = 3;
const byte buttonPin4 = 4;

void setup() {
  Serial.begin(9600);
  Serial.println("start...");
}

void loop() {
  Serial.println(digitalRead(buttonPin2) | (digitalRead(buttonPin3) << 1) | (digitalRead(buttonPin4) << 2));
  delay(500);
}