Re: enum and switch cases

Hi there,

I got a an issue trying to use enum - first time to use it. That's why. I am not actually a developer so that may add up too.

I'll let you have a look at the code and will tell you my issue at the end.

void loop(){

 if(esp8266.available()){ 

//read what's being sent from the ESP Module  something like "SwitchThirteen,LOW"
     String Recieved_Msg = esp8266.readString(); 
//split the messages
     String SwitchNumber = getValue(Recieved_Msg, ',', 0); // SwitchNumber=SwitchThirteen
     String State = getValue(Recieved_Msg, ',', 1); // State=LOW
     
     Serial.println("Switch Number: " + SwitchNumber);
     Serial.print("State: " + State);

     Recieved_Msg = "";
     
     TurnOnOffSwitch(SwitchNumber, State);
  }
}

Here's where the problem shows up
I am trying to print the values of switch numbers respectively i.e SwitchOne should print 1 and SwitchTwo should print 2 so that eventually I will turn the actual pins HIGH/LOW based on what is coming from loop function.

The problem is that I am always getting a zero "0"

void TurnOnOffSwitch(String SwitchNumber, String State){

enum SwitchList
{
  SwitchOne = 1,
  SwitchTwo,
  SwitchThree,
  SwitchFour,
  SwitchThirteen = 13
};
 
SwitchList Switch;
 Serial.println(Switch);
 switch (Switch)
 {
   case SwitchOne: Serial.println(SwitchOne); break;
   case SwitchTwo: Serial.println(SwitchTwo); break;
   case SwitchThree: Serial.println(SwitchThree); break;
   case SwitchThirteen: Serial.println(SwitchThirteen); break;
   default: break;
 }


// digitalWrite(SwitchNumber, State);
 
}

Here's a sample output

Switch Number: SwitchThree
State: LOW
0

When I am already sending "SwitchThree,LOW" with MQTT.

Appreciate your help fellows. Thanks

Please post your code

Hi, I already did. Do you see something missing ? :slight_smile:

Nizar:
Hi, I already did. Do you see something missing ? :slight_smile:

Yes, I missed your code.

void loop(){

  if(esp8266.available()){ // check if the esp is sending a message 

      String Recieved_Msg = esp8266.readString();
      String SwitchNumber = getValue(Recieved_Msg, ',', 0);
      String State = getValue(Recieved_Msg, ',', 1);
      
      Serial.println("Switch Number: " + SwitchNumber);
      Serial.print("State: " + State);
      Recieved_Msg = "";
      
      TurnOnOffSwitch(SwitchNumber, State);
   }
}


void TurnOnOffSwitch(String SwitchNumber, String State){

 enum SwitchList
{
   SwitchOne = 1,
   SwitchTwo,
   SwitchThree,
   SwitchFour,
   SwitchThirteen = 13
};
  
 SwitchList Switch;
  Serial.println(Switch);
  switch (Switch)
  {
    case SwitchOne: Serial.println(SwitchOne); break;
    case SwitchTwo: Serial.println(SwitchTwo); break;
    case SwitchThree: Serial.println(SwitchThree); break;
    case SwitchThirteen: Serial.println(SwitchThirteen); break;
    default: break;
  }


// digitalWrite(SwitchNumber, State);
  
}

@Nizar, do no hijack. Thread split.

Still only seeing a snippet.

 SwitchList Switch;
  Serial.println(Switch);
  switch (Switch)

What do you imagine the value of Switch is?
Why?

You pass SwitchNumber, a String, into your TurnOffSwitch function and then do nothing with it.

You create an enum variable named Switch but never assign it a value. Since it is a local variable the value will be whatever was on the stack.

To translate names into numbers you need a list of strings containing the names. An enum is NOT a list of strings. It is a list of variable names, each assigned a small constant integer value.

1 Like

You pass SwitchNumber, a String, into your TurnOffSwitch function and then do nothing with it.

That's definitely correct. Can't believe I actually did this. Sorry ! :slight_smile:

To translate names into numbers you need a list of strings containing the names

Would you please illustrate this by an example. Thanks

const char * Names[] = {"SwitchOne", "SwitchTwo", "SwitchThree", "SwitchFour", "SwitchThirteen"};
const int Numbers[] = {1,2,3,4,13};

void GetNumber(char *name) {
   for (int i=0; i < sizeof Names / sizeof Names[0]; i++) {
       if (strcmp(name, Names[i]) == 0)
            return Numbers[i];
   }
   return -1;  // No match found
}
1 Like

@johnwasser Thanks a lot for response. It really helped. Apologies for the late response though.

@johnwasser the shared code cannot compile with arduino 1.8.7

error return-statement with a value, in function returning 'void' [-fpermissive] thanks

gcharles:
@johnwasser the shared code cannot compile with arduino 1.8.7

error return-statement with a value, in function returning ‘void’ [-fpermissive] thanks

Easily fixed but after 2 years the OP might not care

void GetNumber(char *name)
int GetNumber(char *name)