Arduino programming - Adafruit motor shield.

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
t3kboi
 
Posts: 7
Joined: Thu May 05, 2011 6:18 pm

Arduino programming - Adafruit motor shield.

Post by t3kboi »

Hi, I am a total noob - this is my first post.

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?

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino programming - Adafruit motor shield.

Post by adafruit_support_bill »

This will not work in C++. It can't directly convert a string to a named instance of a class.
You could do it if you created an an array of AF_DCMotor.

t3kboi
 
Posts: 7
Joined: Thu May 05, 2011 6:18 pm

Re: Arduino programming - Adafruit motor shield.

Post by t3kboi »

I think that is the workaround I was looking for:
Old Code:

Code: Select all

char* Motors[] = {"WristFlex", "ElbowFlex", "ShoulderFlex", "ShoulderRotate"};

New Code:

Code: Select all

AF_DCMotor Motors[] = {"WristFlex", "ElbowFlex", "ShoulderFlex", "ShoulderRotate"};
This generates an "invalid conversion from 'const char*' to 'uint_8'

AF_DC_Motor definition:

Code: Select all

AF_DCMotor::AF_DCMotor(uint8_t num, uint8_t freq)
Do you have any suggestions for how to setup the array for AF_DCMotor that would alleviate the error?

Thanks in Advance

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino programming - Adafruit motor shield.

Post by adafruit_support_bill »

Code: Select all

AF_DCMotor wristFlex(1);
AF_DCMotor elbowFlex(2);
AF_DCMotor shoulderFlex(3);
AF_DCMotor shoulderRotate(4);

AF_DCMotor Motors[] = {wristFlex, elbowFlex, shoulderFlex, shoulderRotate};

t3kboi
 
Posts: 7
Joined: Thu May 05, 2011 6:18 pm

Re: Arduino programming - Adafruit motor shield.

Post by t3kboi »

Perfect!!!

Thanks for the assist.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”