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
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.
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