I'm working on a project that requires 3 steppers, 1 DC motor, and 2 servos so I ended up stacking two Adafruit V2 motorshields on an Arduino UNO to control all these components. While I was able to address the steppers using their I2C address, I've found no discussions or documentation related to communicating with a servo motor on the top-most motor shield. I know that servo pins are hard coded to pins 9 and 10 so I haven't been addressing the servos with an I2C address.
My issues is as follows:
The code below works when I try using the servo on the stacked motor shield.
#include <Servo.h>
int i;
Servo myservo;
void setup() {
Serial.begin(9600);
myservo.attach(9);
int i=5;
Serial.println("1:Rotate, 2:Reset");
}
void loop() {
delay(1);
char choice = Serial.read();
if (choice == '1') {
for (i=5; i<=180; i++) {
myservo.write(i); //waste
delay(30);
}
delay(3000);
for (i=180; i>5; i--) {
myservo.write(i); //waste
delay(30);
}
}
else if (choice == '2'){
i=5;
myservo.write(i);
}
}
However when I copy and paste the previous code onto my stepper motor code, the code stops at the comment "Servo here" and then doesn't continue and the servo doesn't move at all. The steppers work though.
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
int i = 1;
int originalAngle = 5;
int j;
Adafruit_MotorShield AFMS12 = Adafruit_MotorShield(0x60);
Adafruit_MotorShield AFMS34 = Adafruit_MotorShield(0x61);
Adafruit_StepperMotor *stepperMotor1 = AFMS12.getStepper(200, 1);
Adafruit_StepperMotor *stepperMotor2 = AFMS12.getStepper(200, 2);
Adafruit_StepperMotor *stepperMotor3 = AFMS34.getStepper(200, 1);
Adafruit_DCMotor *myDCMotor = AFMS34.getMotor(4);
Servo myservo;
void setup() {
Serial.begin(9600);
AFMS12.begin(); // create with the default frequency 1.6KHz
AFMS34.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
// stepperMotor1->setSpeed(200); // 200 rpm
stepperMotor2->setSpeed(200); // 200 rpm
stepperMotor3->setSpeed(200); // 200 rpm
// stepperMotor4->setSpeed(200); // 200 rpm
myservo.write(originalAngle);
myservo.attach(9);
}
void loop() {
Serial.println("==");
Serial.println(i);
Serial.println("==");
Serial.println("Double coil steps Motor 1");
stepperMotor1->step(100, FORWARD, DOUBLE);
stepperMotor1->step(100, BACKWARD, DOUBLE);
Serial.println("Double coil steps Motor 2");
stepperMotor2->step(100, FORWARD, DOUBLE);
stepperMotor2->step(100, BACKWARD, DOUBLE);
Serial.println("Double coil steps Motor 3");
stepperMotor3->step(100, FORWARD, DOUBLE);
stepperMotor3->step(100, BACKWARD, DOUBLE);
Serial.println("DC Motor");
myDCMotor->setSpeed(150);
myDCMotor -> run(FORWARD); //rotate propeller
delay(1000); //spin for 5 seconds
myDCMotor->setSpeed(0);
i++;
Serial.println("Servo here");
for (j=originalAngle; j<=150; i++) {
myservo.write(j); //waste
delay(30);
}
delay(3000);
for (j=150; j>originalAngle; j--) {
myservo.write(j); //waste
delay(30);
}
}
I think I've narrowed it down to a coding issue as the servo motor technically works with the stacked motor shields but I'm unsure how to get the same code to work when the steppers are involved.
3 posts - 3 participants