Motor Shield: define motor array?

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
vienna_guy
 
Posts: 2
Joined: Tue Jun 25, 2013 6:10 am

Motor Shield: define motor array?

Post by vienna_guy »

Hi,
I'm quite a new to programming and I appreciate your help on this issue:

I'm trying to put my motor's definition in an array so I can pick a specific motor depending on a returned variable.

The standard code is something like:

Code: Select all

 
AF_DCMotor motor1(1, MOTOR12_64KHZ);           
AF_DCMotor motor2(2);                            
What I was trying is something like this:

Code: Select all

// Pointer to DC_Motors
AF_DCMotors *my_motors;
 
//Define Array members
my_motors[0] = AF_DCMotor(1,motor12_64kHz);
my_motors[1] = AF_DCMotor(2);
I get the following error code, which doesn't mean anything to me:
error: expected constructor, destructor, or type conversion before '*' token

Was searching the forum, but didn't find anything useful.

Thank you very much!

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

Re: Motor Shield: define motor array?

Post by adafruit_support_bill »

The first error message was due to a typo: 'AF_DCMotors' != 'AF_DCMotor'.
But you also need to define the size of your array and initialize it in setup.

Something like this should work - keep in mind that this is an array of pointers to motors, so you will need to dereference them at point of use.

Code: Select all

// Pointer to DC_Motors
AF_DCMotor* my_motors[2];

//Define Array members
AF_DCMotor motor0(1,MOTOR12_64KHZ);
AF_DCMotor motor1(2);

void setup() 
{
  my_motors[0] = &motor0;
  my_motors[1] = &motor1;
}

vienna_guy
 
Posts: 2
Joined: Tue Jun 25, 2013 6:10 am

Re: Motor Shield: define motor array?

Post by vienna_guy »

Thank you very much!

User avatar
andrewa
 
Posts: 145
Joined: Mon Oct 19, 2009 9:53 pm

Re: Motor Shield: define motor array?

Post by andrewa »

The syntax seems off from the examples we have; I've got two motor shields, and would like to be able to address each motor set by changing a single variable; currently, I've got:

Code: Select all

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x60); 

// Second motor shield
Adafruit_MotorShield AFMS_2 = Adafruit_MotorShield(0x61); 

// Set motor names
Adafruit_DCMotor *myMotor_1 = AFMS.getMotor(1);
Adafruit_DCMotor *myMotor_2 = AFMS.getMotor(2);
Adafruit_DCMotor *myMotor_3 = AFMS.getMotor(3);
Adafruit_DCMotor *myMotor_4 = AFMS.getMotor(4);

Adafruit_DCMotor *myMotor_5 = AFMS_2.getMotor(1);
So, the alternate would seem to be something along the lines of (dimension to 6, since we'll not use the 0th element - trying to write
this for someone who might get confused... :-) ):

(However, the MotorArray[1] = ... statements are rejected)...

Code: Select all

// alternate
Adafruit_DCMotor*  MotorArray[6];

Adafruit_DCMotor *Motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *Motor2 = AFMS.getMotor(2);
Adafruit_DCMotor *Motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *Motor4 = AFMS.getMotor(4);

Adafruit_DCMotor *Motor5 = AFMS_2.getMotor(1);

MotorArray[1] = &Motor1;
MotorArray[2] = &Motor2;
MotorArray[3] = &Motor3;
MotorArray[4] = &Motor4;
MotorArray[5] = &Motor5;
So, two questions:

1) How to fix the definitions above
2) How do we change statements (like the following) to reflect this array-based system?

myMotor_1->setSpeed(MotorSpeed);
myMotor_1->run(FORWARD);
Last edited by adafruit_support_mike on Tue Apr 01, 2014 5:19 pm, edited 1 time in total.
Reason: added CODE tags to preserve the formatting

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Motor Shield: define motor array?

Post by adafruit_support_mike »

You have an extra layer of indirection in your code.

Your array holds "Adafruit_DCMotor*" types and your "MotorN" varibles are also "Adafruit_DCMotor*" types. The expression "&MotorN" is of type "Adafruit_DCMotor**", which is one step removed from what you want.

Remove the '&' signs from the array assignment and the code should work.

Using the array items would look like this:

Code: Select all

MotorArray[1]->setSpeed( MotorSpeed );
MotorArray[1]->run( FORWARD );

User avatar
andrewa
 
Posts: 145
Joined: Mon Oct 19, 2009 9:53 pm

Re: Motor Shield: define motor array?

Post by andrewa »

Aah, no dice:

// alternate
Adafruit_DCMotor* MotorArray[6];

Adafruit_DCMotor *Motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *Motor2 = AFMS.getMotor(2);
Adafruit_DCMotor *Motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *Motor4 = AFMS.getMotor(4);

Adafruit_DCMotor *Motor5 = AFMS_2.getMotor(1);

MotorArray[1] = Motor1;
MotorArray[2] = Motor2;
MotorArray[3] = Motor3;
MotorArray[4] = Motor4;
MotorArray[5] = Motor5;


Still gives 'expected constructor, destructor, or type conversion before '=' token' error on MotorArray[1]=Motor1; line...

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

Re: Motor Shield: define motor array?

Post by adafruit_support_bill »

MotorArray[1] = Motor1;
MotorArray[2] = Motor2;
MotorArray[3] = Motor3;
MotorArray[4] = Motor4;
MotorArray[5] = Motor5;

Still gives 'expected constructor, destructor, or type conversion before '=' token' error on MotorArray[1]=Motor1; line...
You can't initialize individual array elements at file level. You need to move the array initialization inside your setup()

User avatar
andrewa
 
Posts: 145
Joined: Mon Oct 19, 2009 9:53 pm

Re: Motor Shield: define motor array?

Post by andrewa »

Works like a charm; thanks!

Additionally (because someone looking at this might be looking at limits/home switches), you can do the following for input pins

#define MOTORMAX 5

// limit pins are in an array, so they can be addressed easily
// element 0 not used, so we can index from 1 to 5
// Pin 30 is the far limit for Motor 1, Pin 31 is the near limit for Motor 1
// Pin 32 is the far limit for Motor 2, Pin 32 is the near limit for Motor 2
// etc.

int Far_Limit_Pin[]={0, 30, 32, 34, 36, 38};
int Near_Limit_Pin[]={0, 31, 33, 35, 37, 39};


and later, in setup():

// Set the speed to start, and limit switch pins
for(i=1;i<=MOTORMAX;i++)
{
MotorArray->setSpeed(0);
pinMode(Far_Limit_Pin,INPUT_PULLUP);
pinMode(Near_Limit_Pin,INPUT_PULLUP);
}

So you can do things like:


int MotorUsed = 1;

MotorArray[MotorUsed]->setSpeed(MotorSpeed);
MotorArray[MotorUsed]->run(FORWARD);
if(digitalRead(Far_Limit_Pin[MotorUsed])==false)
{
// etc. etc. etc.
}

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

Return to “Arduino Shields from Adafruit”