How to check whether the input is the same as my variable?

I am trying to check whether there is a variable with the same name as the input and use it later in the code.
Ex:

/*

*/
int array1[] = {2,3};
int array2[] = {5,6};
void setup() {
    Serial.begin(9600);
    Seial.println("Which array do you want? array1[] or array2.[]");
    while (Serial.available==0){
      //waiting for input
    String z= Serial.readString(); 
    analogWrite(13,z[1]);//now i would like to use the array which is the same as the input.
}

void loop() {
    
}

I don't think you can do that. After the compiler has done its work, there are no more variable names. Only operations and memory addresses. If you want to do something like this, you'll have to for example store names/identifiers as char arrays and store a relationship between such a char array and a variable in your program

How to do that?

Tell us what you want to achieve, not how you think you can achieve it. We can suggest some simple ways.

Is it not possible to use only integer numbers for serial input for use two dimensional array?
Well... If you really want string input, create a look-up table that corresponds to the word.

Yes, that sounds a good way, use something like

int arrays[][] = {{2,3},  {5,6}} ;

Then you can ask for array 0 or 1, and use something like

 int z= Serial.readInt(); 
 if (0 <= z && z <= 1)
   // do something with arrays[z][...]
if (0 >= z && z <= 1)

Isn't it always True when the (int)z is negative? :thinking:

The only way to do that is store the name of the variable. Here is a way to do it:

int array1[] = {2,3};
int array2[] = {5,6};

const String array1name = "array1";
const String array2name = "array2";

void setup() 
{
  Serial.begin(9600);
  
  while (1)
  {
    Serial.println("Which array do you want? array1 or array2");
  	while (!Serial.available()); //waiting for input
    String z= Serial.readString(); 
	if (z == array1name)
    {
      Serial.println("Write from array1");
      analogWrite(13,array1[1]);
      break;
    }
    else if (z == array2name)
    {
      Serial.println("Write from array2");
      analogWrite(13,array2[1]);
      break;
    }
    else
    {
      Serial.println("Invalid input.  Try again...");
    }
  }
}

void loop() 
{
}

Once an array is chosen the analogWrite is performed and the program exits setup().

No, I definitely had it right, I validate that the value is valid, in the range 0..1, so that I don't index outside the array's limits.

In Python you can say:

  if min_value <= z <= max_value:

(which is very like the maths notation)

but in C or C++ you are limited to the more clumsy

  if (min_value <= z && z <= max_value)

Agreed, sorry!

int array1[] = {2,3};
int array2[] = {5,6};

struct Array_s {
    const char *name;
    int        *array;
    int         size;
};

Array_s arrays [] = {
    { "array1", array1, sizeof(array1)/sizeof(int) },
    { "array2", array2, sizeof(array2)/sizeof(int) },
};
#define N_ARRAYS    (sizeof(arrays)/sizeof(Array_s))

const char *prompt = "Which array do you want? array1[] or array2.[]";


char line [80];
int  idx = 0;

// -----------------------------------------------------------------------------
void
process (
    char *line )
{
    for (unsigned n = 0; n < N_ARRAYS; n++)  {
        if (! strcmp (arrays [n].name, line))  {
            for (int i = 0; i < arrays [n].size; i++)  {
                Serial.print   (" ");
                Serial.print   (arrays [n].array [i]);
            }
            Serial.println ();
            return;
        }
    }
    Serial.println ("  unknown array name");
}

// -----------------------------------------------------------------------------
void loop() {
    if (Serial.available ()) {
        char c = Serial.read ();
        if ('\n' == c) {
            line [idx] = '\0';
            process (line);
            idx = 0;
            Serial.print (prompt);
        }
        else
            line [idx++] = c;
    }
}

void setup() {
    Serial.begin(9600);
    Serial.print (prompt);
}

[quote="nousernameavailable, post:1, topic:877151"]
I am trying to check whether there is a variable with the same name as the input
[/quote]
you can associate a string name with variables

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.