I have a programming question - currently using an Adafruit motor shield, but the same question will apply to a lot of similar situations.
The setup:
I declare a char* array, and load some names into it.
Then I use a Class from the AFMotor library, to instantiate an instance of the class.
The instance names match the names in the char* array.
If I iterate through the array, I correctly see each name in sequence.
The problem:
If I iterate through the array, and assign each member to a char or string variable, the substitution fails when I try to use it to invoke a class method. The error is either "string does not contain a method of type.... (substitute class type being called)"
Of course this is true: The Method belongs to the Class with the instance being the same name as the string. (but the string was declared as a string....)
Is there a way to workaround this, so I can iterate through many things? The code below should clarify what I am doing.....
Code: Select all
#include <AFMotor.h>
char* Motors[] = {"WristFlex", "ElbowFlex", "ShoulderFlex", "ShoulderRotate"};
AF_DCMotor WristFlex(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor ElbowFlex(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor ShoulderFlex(3, MOTOR34_64KHZ); // create motor #3, 64KHz pwm
AF_DCMotor ShoulderRotate(4, MOTOR34_64KHZ); // create motor #4, 64KHz pwm
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Motor test!");
for(int i = 0; i < 4; i++){
String CurrentM = Motors[i];
Serial.println(CurrentM); // This confirms that I am retrieving the array names correctly.
CurrentM.setSpeed(200); // <---- This is what throws the error that CurrentM does not contain the method setSpeed.....
}
any ideas?