Enum in Switch Statement

Hi, I'm trying to use a switch/case statement that will output various functions based on the number I type into Terminal or Putty using my Arduino Uno. I originally did this with if/else statements but thought switch/case might be easier to use moving forward. It seems as if my switch/case statement is just falling through and no case statements are being caught. I am also receiving no error messages and want to avoid using libraries. I have an enum available, but I am not totally sure how to use it in this case. Any advice would be appreciated, thanks!

boolean stringComplete = false;
boolean analogString = false;
int outputFunction = "";
char inChar;
int inputString = "";
enum Key_Options {
  G,
  Q,
  S,
  E,
};

void setup(){
  Serial.begin(9600);
  Serial.write("Your Arduino is connected to a Serial Port.  Try typing and press enter \n");

}

void serialEvent() {
  while (Serial.available()) {
    char inCar = (char)Serial.read();
    if (inChar =='\n' | inChar == '\r'){
      stringComplete = true;
    }
    else{
      inputString +=inCar;
    }
  }
}

void loop(){
  switch(inputString){
    case G:
      Serial.println("Got G");
      break;
    case Q:
      Serial.println("42.42");
      break;
    case S:
      analogString = true;
      break;
    case E:
      analogString = false;
      break;
  }
  if (analogString == true){
    Serial.println(analogRead(3));
    delay(1000);
  }

  
}

Hi

Try this:

boolean stringComplete = false;
char inChar;
String inputString = "";

void setup(){
  Serial.begin(9600);
  Serial.write("Your Arduino is connected to a Serial Port.  Try typing and press enter \n");

}

void serialEvent() {
  while (Serial.available()) {
    char inCar = Serial.read();
    if (inChar =='\n' | inChar == '\r'){
      stringComplete = true;
    }
    else{
      inputString +=inCar;
    }
  }
}

void loop(){
  switch(inputString){
    case "G":
      Serial.println("Got G");
      break;
    case "Q":
      Serial.println("42.42");
      break;
    case "S":
      analogString = true;
      break;
    case "E":
      analogString = false;
      break;
  }
  if (stringComplete == true){
    Serial.println(analogRead(3));
    delay(1000);
  }
}

I don't know why you use a string for a one character command...

Jacques

enum Key_Options {

G,
  Q,
  S,
  E,
};

An enum in C is just a different way of writing integer constants. Under this system the constant G will have the value 0. It won't be related to the ASCII value of 'G' in any way.

Now you can force it to use values you select in the enum. You could make G='G' but really, what's the point of that when Jacque's solution is perfect? You can even make it case-insensitive so the poor user doesn't have to hold down the SHIFT key...

  switch(inputString){
    case "G":
    case "g":
      Serial.println("Got G");
      break;
    case "Q":
    case "q":
      Serial.println("42.42");
      break;

MorganS:
An enum in C is just a different way of writing integer constants. Under this system the constant G will have the value 0. It won't be related to the ASCII value of 'G' in any way.

Now you can force it to use values you select in the enum. You could make G='G' but really, what's the point of that when Jacque's solution is perfect? You can even make it case-insensitive so the poor user doesn't have to hold down the SHIFT key...

  switch(inputString){

case "G":
    case "g":
      Serial.println("Got G");
      break;
    case "Q":
    case "q":
      Serial.println("42.42");
      break;

You can also make it case insensitive by using:

  • inCar = toupper((char)Serial.read());*

which gets rid of the lower case constants in the switch.

You must remember that when you declare a function, it NEEDS to be applied, for example, in your loop, otherwise it will never be executed (in your code, for example):

void serialEvent() {
while (Serial.available()) {
char inCar = (char)Serial.read();
if (inChar =='\n' | inChar == '\r'){
stringComplete = true;
}
else{
inputString +=inCar;
}
}
}

This function is not called at any time. Soon, you will not get anything to have what you compare.

https://www.arduino.cc/en/Reference/SerialEvent

Sorry, but it does get called by the Arduino framework. It's not very useful, so it is rare to find any programs actually using it.

serialEvent() - Arduino Reference

Sorry, but it does get called by the Arduino framework. It's not very useful, so it is rare to find any programs actually using it.

Correct timely!

The snippet of code provided has MANY blatant errors, and will not even come close to compiling. The enums are useless, the switch statement is not valid syntax, and "inChar" is NOT the same as "inCar". So, how about posting ACTUAL code that you have compiled and run.

Regards,
Ray L.