It's simple, I have an integer with a variableName R1M, and a value of 10.

In the function I have to read R1P, and therefore have to convert the variableName to a string, and change the last letter.

So I have a String, containing "R1P", and i need to digitalRead(myString), which will not work.

How do i convert myString containing "R1P" to a variableName?

(Yes, I have been googling like crazy :o )

TL;DR:
digitalRead(myString) will not work. How do I convert a String to variableName?

Why use variables with different names ?

If they must be different then set R1P equal to R1M

If you need more help then please post a full example of what your code is doing

I am doing a function to test a set of motors with corresponding sensors.

Motor R1M -> Sensor R1P
Motor R2M -> Sensor R2P
Motor R3M -> Sensor R3P

I need to call the function by the motor name, and then it needs to check the corresponding sensor.

``````void testMotor(String motor) {
int startMillis;
int currentMillis;
String pos = motor;
pos.replace("P", "S");
delay(1000);
if (sensPos == LOW) {
Serial.println("Roller in position, rotating to next position...");
delay(4000);
digitalWrite(motor, LOW);
startMillis = millis();
while (sensPos == LOW && currentMillis <= startMillis + 4000) {
currentMillis = millis();
}
if (sensPos == HIGH) {
Serial.println("Motor and sensor OK")
} else {
Serial.println("ERROR! Motor timed out!")
}

} else {
Serial.println("Roller NOT in position, positioning rotor...");
delay(4000);
digitalWrite(motor, LOW);
startMillis = millis();
while (sensPos == LOW && currentMillis <= startMillis + 4000) {
currentMillis = millis();
}
if (sensPos == HIGH) {
Serial.println("Rotor in position, rotating to next position...")
} else {
Serial.println("ERROR! Motor timed out, could not sense position!")
}

delay(4000);
digitalWrite(motor, LOW);
startMillis = millis();
while (sensPos == LOW && currentMillis <= startMillis + 4000) {
currentMillis = millis();
}
if (sensPos == HIGH) {
Serial.println("Motor and sensor OK")
} else {
Serial.println("ERROR! Motor timed out!")
}

}
}
``````

digitalRead takes an integer value as an argument, NOT a String. At runtime, variable names no longer exist, so passing a variable name as a function argument CANNOT ever work.

Regards,
Ray L.

I need to call the function by the motor name, and then it needs to check the corresponding sensor.

If that is what you really want to do then pass the motor name as a String and test it in the function to determine which sensor to test

``````void testMotor(String motor)
{
if (motor == "this")
{
sensor = X;
}
else
if (motor == "that")
{
sensor = Y;
}