Cannot move two motors at once

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
thespiesman
 
Posts: 7
Joined: Wed Jun 15, 2011 5:30 pm

Cannot move two motors at once

Post by thespiesman »

Alright, so usually I can get the motor shield to move two motors at once, but for some reason when I'm trying to use serial input as well to govern the motion of the motors, one motor moves just fine but the other will not. I hooked up a voltmeter across the coils (the wires where they are connected to the shield via the screws) and the voltage on the one that doesn't work doesn't change at all! I don't know if I just have a shoddy power supply that needs a better current limiter (higher inner resistance load, an additional resistor put in series with the power supply, etc etc), or if there's something in the coding that makes it not work. In the code I've got it set to move motors in certain ways based on a certain character getting sent down the serial port.

Code: Select all

 #include <AFMotor.h>
 
 AF_Stepper motor2(48, 2);
 AF_Stepper motor1(48, 1);

 void setup()  {
   Serial.begin(9600);
   motor1.setSpeed(100);
   motor1.setSpeed(100);
 }

 void doublestep (int steps1, int steps2, int dir1, int dir2, int style1, int style2) {
   while (steps1--) {
     motor1.step(1, dir1, style1); 
   }
   while (steps2--) {
     motor2.step(1, dir2, style2); 
   }
 }

 void loop()  {
   if (Serial.available())  {
     char readin = Serial.read();
     if (readin == 49)  {
       motor1.step(48, FORWARD, DOUBLE);
       Serial.println("First motor forward");
     }
     else if (readin == 50)  {
       motor1.step(48, BACKWARD, DOUBLE);
       Serial.println("First motor backward");
     }
     else if (readin == 51)  {
       motor2.step(48, FORWARD, DOUBLE);
       Serial.println("Second motor forward");
     }
     else if (readin == 52)  {
       motor2.step(48, BACKWARD, DOUBLE);
       Serial.println("Second motor backward");
     }
     else if (readin == 53)  {
       doublestep(48, 48, BACKWARD, FORWARD, DOUBLE, DOUBLE);
       Serial.println("First backward, Second forward");
     }
     else if (readin == 54)  {
       doublestep(48, 48, FORWARD, BACKWARD, DOUBLE, DOUBLE);
       Serial.println("First forward, Second backward");
     }
     else if (readin == 55)  {
       doublestep(48, 48, FORWARD, FORWARD, DOUBLE, DOUBLE);
       Serial.println("Both forward");
     }
     else if (readin == 56)  {
       doublestep(48, 48, BACKWARD, BACKWARD, DOUBLE, DOUBLE);
       Serial.println("Both backward");
     }
     else  {
       Serial.println("Not a valid Input");
     }
   }
 }

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

Re: Cannot move two motors at once

Post by adafruit_support_bill »

The AFMotor library does not support concurrent stepper movement by itself. For that you need the AccelStepper library:
http://www.ladyada.net/make/mshield/download.html

thespiesman
 
Posts: 7
Joined: Wed Jun 15, 2011 5:30 pm

Re: Cannot move two motors at once

Post by thespiesman »

What in particular in the AccelStepper library would I utilize then? I am not as familiar with that library, and all the documentation I've seen on it hasn't been very helpful in how I can go about using it.

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

Re: Cannot move two motors at once

Post by adafruit_support_bill »

Check the AF_Motor_MultiStepper example in the AccelStepper library. You can set up moves on both motors, then tell them both to run.

thespiesman
 
Posts: 7
Joined: Wed Jun 15, 2011 5:30 pm

Re: Cannot move two motors at once

Post by thespiesman »

I used that example just now and the second motor won't backstep, and then it stops stepping overall.

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

Re: Cannot move two motors at once

Post by adafruit_support_bill »

You must have a different version. In the example on GitHub, stepper 2 does not reverse, it rotates continuously for a million steps. Stepper 1 reverses on every 24. I have it running now on my bench as I type.

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

Return to “Arduino Shields from Adafruit”