0

Right now, I have the following switch-case, built using an enum representing some text.

    String inputMessage = Serial.readString();    //Input is either TEXT_1 or TEXT_2.
    enum Messages {
        TEXT_1, TEXT_2
    };
    enum Messages currentMessage = TEXT_2;
    switch (currentMessage) {
      case TEXT_1: 
        Serial.println("RESPONSE_1");
        break;
      case TEXT_2: 
        Serial.println("RESPONSE_2");
        break;
      default:
        break;

I am trying to get to a point where the contents of a String (this is Arduino C code), inputMessage, which will be TEXT_1 or TEXT_2, to control currentMessage, which will control the switch-case. I would like to avoid an if-else stack if possible (thus, the switch-case attempt) since I do intend to scale up the number of TEXT to a much higher number in the future.

Please let me know if more information is required.

HFOrangefish
  • 267
  • 1
  • 10
  • You can just add more `case` clauses or use an array for the messages... what is your question? – chqrlie Aug 26 '22 at 09:00
  • 1
    If the values of `TEXT_1` and `TEXT_2` are `0`, `1` etc you can index an array of strings or string pointers and avoid `if` and `switch`. With `char *msg[] = { "RESPONSE_1", "RESPONSE_2" };` for example and `Serial.println(msg[currentMessage]);`. – Weather Vane Aug 26 '22 at 09:02
  • @WeatherVane Could you elaborate? I'm afraid I don't quite follow. – HFOrangefish Aug 26 '22 at 09:07
  • What is the type of `TEXT_1` and `TEXT_2`? – David Ranieri Aug 26 '22 at 09:09
  • 1
    What's more to say? You can have an array of strings, indexed by their reference ID. `TEXT_1` etc has a value of `0`, etc. – Weather Vane Aug 26 '22 at 09:12
  • 1
    I think @WeatherVane means something like https://ideone.com/WMnmJn – David Ranieri Aug 26 '22 at 09:14
  • It is unclear from the question whether `TEXT_1` is an integer or a string. First it is commented as a message from serial, then it is defined as `enum`. It would be clearer if you showed an actual message, and what you want to do with it, but the serial input in the given code is unrelated to what follows. – Weather Vane Aug 26 '22 at 09:18
  • @WeatherVane `TEXT_1` cannot be a string as it is used in enum declaration. – 0___________ Aug 26 '22 at 09:23
  • 1
    @0___________ that is exactly why I asked for clarification. it is also described as a string input from serial. See the first line of of the code, which has a comment. Is this a descriptive string or a numeric string (which can be conveted to an integer)? – Weather Vane Aug 26 '22 at 09:25
  • Re “much higher number in the future”: Store the strings in a sorted array and use `bsearch` to look up the message. – Eric Postpischil Aug 26 '22 at 12:19

1 Answers1

0

enum is just an integer. It is not a string.

you need to have a if/else ladder with string comparison functions as you cant have a string as a control expression of the switch/case. Only integers are allowed

0___________
  • 60,014
  • 4
  • 34
  • 74
  • Missed opportunity with this 'closed'... OP mentioned "a much higher number in the future"... An "if/else tree" might, at least, be somewhat better than the inevitable presumed linear search of the space. – Fe2O3 Aug 26 '22 at 10:01