Using Enumerations

I would love to know if this is possible:

I have 3 settings: speed, direction, color

I'm trying to have 1 variable array, setting[2] to control these BUT have them identified by the user by name (speed, direction, color).

Something like this:

String input;
enum setting { speed, direction, color};

Serial.print("Which setting");
while (!Serial.available()); // sits and waits until a user input
input = Serial.readString();

switch ((int) input)
{
case 0:
// do the speed thing
break;

case 1:
// do the direction thing
break;

case 2:
// do the color thing
break;
default:
// not accepted input
break
}

I know this kind of thing is possible in C# but I don't know about C++ or Arduino.

Any help would be appreciated!

Dave

How would you do it in C#?

On C/C++ you can't refer to variables by name from user input.

You also can't cast a String to an int.

You can use a lookup table containing the parameter names as text. Find the name of the parameter that you want to set in the table and use its index in the switch statement.

Here is an example similar to what you want to do (as sterretje described), maybe it will help

arduino_new:
How would you do it in C#?

I think reflection is the word.

here's a function i typically put in all my programs to exercise code using commands from the Serial input. pcRead() is called from loop().

// process single character commands from the PC
void
pcRead (void)
{
    static long  val  = 0;
    static int   sign = 1;

    if (Serial.available()) {
        int c = Serial.read ();

        switch (c)  {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            val = c - '0' + (10 * val);
            Serial.println (val);
            break;

        case ' ':
            val  = 0;
            sign = 1;
            break;

        case '-':
            sign = -1;
            break;

        case 'B':
            dumpBuf();
            break;

        case 'D':
            debug = val;
            val   = 0;
            break;

        case 'd':
            status();
            break;

        case 'i':
            pins();
            break;

        case 'l':
            lead = val * sign;
            break;

        case 'm':
            manual ^= 1;
            break;

        case 'P':
            pos = target = 0;
            SetSpeed(0);
            break;

        case 'p':
            pins();
            break;

        case 'R':
            SetSpeed(M_MAX);
            break;

        case 'r':
            setDir(D_CW == h_dir ? D_CCW : D_CW);
            break;

        case 'S':
            SetSpeed(M_MAX < val * sign ? M_MAX : val * sign);
            val  = 0;
            sign = 1;
            break;

        case 's':
            SetSpeed(0);
            manual = 1;
            status();
            pos    = 0;
            maxPer = minPer = 0;
            break;

        case 't':
            manual = 0;
            target = val * sign;
            val    = 0;
            sign   = 1;

#if 0
            t_dir  = (target < pos ? D_CCW : D_CW);
            setDir   (t_dir);
#else
            setDir   (target < pos ? D_CCW : D_CW);
#endif
            SetSpeed (M_MAX);

            Serial.print   ("pcRead: ");
            Serial.print   (" target ");
            Serial.println (target);

            SetSpeed(M_MAX);

            isrCnt = maxPer = minPer = 0;
            break;

        case 'T':
            Serial.print   ("pcRead: ");
            Serial.print   (" usec ");
            Serial.println (micros());
            break;

        case 'v':
            Serial.println (VERSION);
            break;

            break;

        case '?':
            Serial.println ("   [0-9] 10*val + digit");
            Serial.println ("   sp    val = 0");
            Serial.println ("   -     sign = -1");
            Serial.println ("   B     dumpBuf()");
            Serial.println ("   D     toggle debug");
            Serial.println ("   d     status()");
            Serial.println ("   i     report state");
            Serial.println ("   l     set lead = val");
            Serial.println ("   m     toggle manual");
            Serial.println ("   P     set pos = 0");
            Serial.println ("   p     display pin values");
            Serial.println ("   R     run max speed");
            Serial.println ("   r     reverse");
            Serial.println ("   S     set speed (0-255)");
            Serial.println ("   s     stop");
            Serial.println ("   t     set target position = val");
            Serial.println ("   T     time (usec)");
            Serial.println ("   v     version");
            break;

        default:
            break;
        }
    }
}