Using Adafruit Motorshield & linked to rc receiver, servo will not function

For a robotics project, I have an Arduino Uno and an Adafruit Motorshield running 4 DC motors and a servo. I have successfully been able to control the motors this way, and I have been able to get the servo running this way, but not together.

The complete project code:

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <Servo.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

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

Servo myservo;
const int chA=A0;
const int chB=A1;
const int chC=A2;
const int chD=A3;
const int chE=18;
int ch1;
int ch2;
int ch3;
int ch4;
int ch5;

int dur1;
int dur2;
int dur3;
int dur4;
int dur5;


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
AFMS.begin();

// set pinMode for servo controls

pinMode(chA,INPUT);
pinMode(chB,INPUT);
pinMode(chC,INPUT);
pinMode(chD,INPUT);
pinMode(chE,INPUT);

myservo.attach(9);

}

void loop() {
  // use MotorN->setSpeed(num 0-255) to set the SPEED.
  // use MotorN->run(FORWARD, BACKWARD, or RELEASE) to set direction
  // ie left toggle, from all the way back to all the way forward
  // is mapped to the range of values -255 to 255, set as int ch1
  // if ch1 < 0, run(BACKWARD)
  // if ch1 > 0, run(FORWARD)
  // setSpeed = abs(ch1)
  // and etc.

  dur1 = pulseIn(chA,HIGH);
  ch1 = map(dur1, 1000, 1900, -255, 255);
  if (ch1 < 0)
  {
    Motor1->run(BACKWARD);
    Motor1->setSpeed(abs(ch1));
  }
  else
  {
    Motor1->run(FORWARD);
    Motor1->setSpeed(ch1);
  }

  dur2 = pulseIn(chB,HIGH);
  ch1 = map(dur2, 1000, 1900, -255, 255);
  if (ch2 < 0)
  {
    Motor2->run(BACKWARD);
    Motor2->setSpeed(abs(ch2));
  }
  else
  {
    Motor2->run(FORWARD);
    Motor2->setSpeed(ch2);
  }

  dur3 = pulseIn(chC,HIGH);
  ch3 = map(dur3, 1000, 1900, -255, 255);
  if (ch3 < 0)
  {
    Motor3->run(BACKWARD);
    Motor3->setSpeed(abs(ch3));
  }
  else
  {
    Motor3->run(FORWARD);
    Motor3->setSpeed(ch3);
  }

  dur4 = pulseIn(chD,HIGH);
  ch4 = map(dur3, 1000, 1900, -255, 255);
  if (ch4 < 0)
  {
    Motor4->run(BACKWARD);
    Motor4->setSpeed(abs(ch4));
  }
  else
  {
    Motor4->run(FORWARD);
    Motor4->setSpeed(ch4);
  }
//if the GEAR switch is flipped, pwm duration ~1050, write angle to servo and hold
//that position; else, write angle 0 to servo and hold that position


  dur5 = pulseIn(chE,HIGH);
  if (dur5 > 1700) {
    myservo.write(0);
  } else {
    myservo.write(90);
  }
  
  }

All of the motors work, as I said, with their speed relative to the position of the joystick. I have the servo linked to a toggle on the controller. However, the servo will not respond even when the motors do.

I've been able to get the servo to work as intended in a bare-bones sketch containing only the servo, as such:

#include <Servo.h>

Servo myservo;
int ch1;
int dur1;

void setup() {
  pinMode(18, INPUT);
  myservo.attach(9);

}

void loop() {
  if (pulseIn(18,HIGH) < 1700) {
    myservo.write(90);
  } else {
    myservo.write(0);
  }

}

Flip the toggle on and it goes to 90 degrees, turn the toggle off and it returns to neutral position. Additionally, I was able to replace the if-else statement on the original code with the 'sweep' example and it worked. I'm pretty sure at this point that it's something simple that I'm just not seeing because I've been staring at it too long. Any help would be greatly appreciated.

I don't see any sign of you having put in Serial.print() lines so you can view any of the stuff relating to your servo code. Why not?

Paul

Paul_KD7HB:
I don't see any sign of you having put in Serial.print() lines so you can view any of the stuff relating to your servo code. Why not?

Paul

Because I'm not very experienced with Arduino and didn't know that was standard procedure. Nevertheless, I added that in, found that the issue was with the pin. Switched from analog pin A4 to A5 and now it's working as intended. I won't pretend to know how that worked, but thanks. I'll keep that in mind for the future.

jdavidsaver:
Because I'm not very experienced with Arduino and didn't know that was standard procedure. Nevertheless, I added that in, found that the issue was with the pin. Switched from analog pin A4 to A5 and now it's working as intended. I won't pretend to know how that worked, but thanks. I'll keep that in mind for the future.

Well now you are experienced! Way to go!

The serial print is the only way to get Arduino programs debugged. When you write a program from scratch, use the Serial.print() to debug as you code, then you will have fewer bugs to attack and can find them easier.

Paul