Servo Problem - Mega 2560 - <Servo.h> - Not Turning

Ok, so I'm having a bit of a rough time with this final part of my project, I'm building an R2D2. I have EVERYTHING running except for the servo that turns R2's head. I have written test code 3 different ways, and all three ways have worked -rewrote Adafruits Motor Party(stripped it down to just the for loops and servo bit), and 2 simple sketches that use .write() or .writeMicroseconds(). I have even gone so far as to copy my test code verbatim into my current R2 sketch... and still no dice.

If anyone can help, I would greatly appreciate it.

Here is the current R2 code:

/*
   Author:
           Matt Tucker
   Date:
          27APR2018
   Description:
          The combination of several individual test projects. The following code will test:
            4 LEDs, 2 motors, 1 servo, and 1 speaker
          The code is to be executed on a Mega 2560 with a motor shield and sensor shield.
   Notes:
          Future iterations will include a Raspberry Pi running a Python script that communicates
          with the Mega 2560. The Python script will use OpenCV to evaluate images from a live
          video for faces, dogs, ect and then send commands to the Mega 2560 accordingly.
*/

//***Speaker***
#include <SD.h>
#define SDPIN 53
#include <TMRpcm.h>
TMRpcm tmrpcm;
int i = 0;
String file;
String type = ".wav";
//********END*****//

//***UltraSonic***
const int pingPin = A4;
//********END*****//

//***LEDs***
const int ledPin1 = 34;
const int ledPin2 = 36;
const int ledPin3 = 38;
const int ledPin4 = 40;
//********END*****//

//***Motors***
#include <AFMotor.h>
AF_DCMotor motor1(2);
AF_DCMotor motor2(3);
//********END*****//

//***Servo***
#include <Servo.h>
Servo servo1;
const int servoPin = 9;
//********END*****//

//***GeneralGlobalVariables***
#include <math.h>
int rndNum;
int count = 0;
//********END*****//

void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));

  //***Speaker***
  tmrpcm.speakerPin = 46;
  tmrpcm.setVolume(5);
  if (!SD.begin(SDPIN))
  {
    Serial.println("Initialization Failed!");
    return;
  }
  //********END*****//

  //***Servo***
  servo1.attach(servoPin);
  //********END*****//

  //***Motors***
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  //********END*****//

  //***LEDs***
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  //********END*****//

  //***StartUP***
  //tmrpcm.play("60.wav");
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin4, HIGH);
  //delay(10000);
  delay(100);
  //********END*****//
}

//***Motors***
void Forward()
{
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  delay(2000);
}

void VearLeft()
{
  motor1.setSpeed(200);
  motor2.setSpeed(100);
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  delay(2000);
}

void VearRight()
{
  motor1.setSpeed(100);
  motor2.setSpeed(200);
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  delay(2000);
}

void TurnLeft()
{
  motor1.run(FORWARD);
  motor2.run(BACKWARD);
  delay(2000);
}

void TurnRight()
{
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
  delay(2000);
}

void Backward(long inches)
{
  while (inches <= 6)
  {
    motor1.setSpeed(200);
    motor2.setSpeed(200);
    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
  }
}

void Backward()
{
  motor1.setSpeed(200);
  motor1.run(RELEASE);
  motor2.setSpeed(200);
  motor2.run(RELEASE);
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  delay(2000);
}

void RevLeft()
{
  motor1.setSpeed(100);
  motor2.setSpeed(200);
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  delay(2000);
}

void RevRight()
{
  motor1.setSpeed(200);
  motor2.setSpeed(100);
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  delay(2000);
}

void Coast()
{
  motor1.run(RELEASE);
  motor2.run(RELEASE);
}

void Stop()
{
  motor1.run(BRAKE);
  motor2.run(BRAKE);
}
//********END*****//

//***Servo***
void Servo_Motor_Turn()
{
  servo1.writeMicroseconds(2000);
  delay(1000);
  servo1.writeMicroseconds(1500);
  delay(1000);
  servo1.writeMicroseconds(1000);
  delay(1000);
  servo1.writeMicroseconds(1500);
  delay(1000);
}

void Servo_Motor_UnTurn()
{
  servo1.writeMicroseconds(1500);
}
//********END*****//

//***UltraSonic***
long Sensor_loop()
{
  long microseconds, inches;

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  microseconds = pulseIn(pingPin, HIGH);

  //code to convert time to distance
  inches = long (microseconds / 74 / 2);
  //code to print distance values
  Serial.print(inches);
  Serial.print("in, ");
  return inches;

  delay(100);
}
//********END*****//

//***LEDs***
void Blink1(int num)
{
  num = (num % 20) * 100;
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  delay(num);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin4, LOW);
  delay(num);
  digitalWrite(ledPin3, HIGH);
  delay(num);
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin4, HIGH);
}

void Blink2(int num)
{
  num = (num % 20) * 100;
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin4, LOW);
  delay(num);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin1, LOW);
  delay(num);
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin3, HIGH);
}

void Blink3(int num)
{
  num = (num % 20) * 100;
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin4, LOW);
  delay(num);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin4, HIGH);
  delay(num);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin2, HIGH);
}
//********END*****//

//***SPEAKER***
void Speaker()
{
  file = String((i % 62) + 2) + ".wav";
  Serial.println("playing: " + file);
  tmrpcm.play(file.c_str());
  i++;
}

void loop()
{
  rndNum = random(0, 100);
  //ALL***UltraSonic_Motors_Servo_Speaker_LEDs***ALL
  Serial.println("Count: " + String(count));
  long inches = Sensor_loop();
  {
    if (inches > 6)
    {
      Forward();
      Blink3(rndNum);
    }
    else
    {
      Serial.println("Speaker");
      Speaker();
      Serial.println("ServoTurn");
      Servo_Motor_Turn();
      Serial.println("Stop");
      Stop();
      Serial.println("blink1");
      Blink1(rndNum);
      Serial.println("backward");
      Backward();
      Serial.println("stop");
      Stop();
      Serial.println("blink2");
      Blink2(rndNum);
    }
    count++;
  }
  //********END*****//
}

Here is the last test code I wrote/tested succesfully:

#include <Servo.h>
Servo servo1;
const int servoPin = 9;

void setup()
{
  Serial.begin(9600);
  servo1.attach(servoPin);
}

void Servo_Motor_Turn()
{
  servo1.writeMicroseconds(2000);
  delay(1000);
  servo1.writeMicroseconds(1500);
  delay(1000);
  servo1.writeMicroseconds(1000);
  delay(1000);
  servo1.writeMicroseconds(1500);
  delay(1000);
}

void loop()
{
  Servo_Motor_Turn();
}

except for the servo that turns R2's head

So what does happen ?
What should happen ?
When should the servo move ?

To make the code easier to understand I suggest that you give the servo a sensible name.

Nothing happens... Everything but the head(servo) works
It's supposed to turn when the ultrasonic comes within 6" of an object.
And it's called, "servo1."

const int pingPin = A4;
...
#include <Servo.h>
Servo servo1;
const int servoPin = 9;
...
void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));
  ...
  servo1.attach(servoPin);
  ...
}
...
void Servo_Motor_Turn()
{
  servo1.writeMicroseconds(2000);
  delay(1000);
  servo1.writeMicroseconds(1500);
  delay(1000);
  servo1.writeMicroseconds(1000);
  delay(1000);
  servo1.writeMicroseconds(1500);
  delay(1000);
}

void Servo_Motor_UnTurn()
{
  servo1.writeMicroseconds(1500);
}

long Sensor_loop() //UltraSonic Sensor
{
  long microseconds, inches;

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  microseconds = pulseIn(pingPin, HIGH);

  //code to convert time to distance
  inches = long (microseconds / 74 / 2);
  //code to print distance values
  Serial.print(inches);
  Serial.print("in, ");
  return inches;

  delay(100);
}
...
void loop()
{
  rndNum = random(0, 100);
  Serial.println("Count: " + String(count));
  long inches = Sensor_loop();
  {
    if (inches > 6)
    {
      ...
    }
    else
    {
      ...
      Serial.println("ServoTurn");
      Servo_Motor_Turn();
      ...
    }
  }
}

So are you saying your test code moves the servo normally? But in the real code it prints "ServoTurn" and then the servo doesn't turn?

Sounds like a possible power problem. How is the servo connected and powered? And what else is taking power in the full system that isn't when you're just testing the servo on its own?

Steve

That is correct, in the test code I get movement from the servo, but when everything is put together it prints "ServoTurn" and the servo doesn't turn.

Altogether there is the Mega 2560, Adafruit Motor Shield v1, there are two 200:1 Plastic Gearmotor 90° Output motors from Pololu, a 3pin ultrasonic sensor, Adafruit MicroSD card breakout board, an LM386 Audio Amplifier, a speaker, and the servo(3pin).

To power it, I'm using the USB and a 7.2v 3300mAh Ni-MH battery pack plugged into the motor shield.

So, in hopes that it was a power issue, I just hooked up a 9v wall source to the arduino and the same 7.2v battery to the motor shield. No change. The servo still doesn't turn.

Hi,
How are you powering your servo?
Is it a continuous servo or 0 to 180deg servo?

Try putting your serial print that says servo print, immediately after the servo1.write command.
To see if the code actually gets to the command.

You may have a library/timer/interrupt conflicts.

I cannot vouch for this library but it says it can work around interrupt problems.

Tom... :slight_smile:

So, the servo is connected to pins in the Adafruit Motor shield; and, the docs claimed the pins are powered directly from the 2560, not from the power source I plugged into the shield.

It's a 0-180 servo.

Will try in the AM. But I can see it moves on to other parts of the code. Wouldn't it just, "freeze" there?

Also, I think I might try another pin. I'm wondering if the pin(pin 9) is being used by something else, in one of the libraries, and I'm just unaware of it.

Hi,
Check this schematic, I think its yours, pin9 is used for PWM motor control.


Tom... :slight_smile:

Ok, so I've narrowed the problem down. When I comment "Speaker()" out, the servo turns. SOOOOOO... The problem is somehow with that.

Here's all the code, just for the speaker:

//***Speaker***
#include <SD.h>
#define SDPIN 53
#include <TMRpcm.h>
TMRpcm tmrpcm;
int i = 0;
String file;
String type = ".wav";
//********END*****//

//***UltraSonic***
const int pingPin = A4;
//********END*****//

...

//***Servo***
#include <Servo.h>
Servo servo1;
const int servoPin = 9;
//********END*****//

//***GeneralGlobalVariables***
#include <math.h>
int rndNum;
int count = 0;
//********END*****//

void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));

  //***Speaker***
  tmrpcm.speakerPin = 46;
  tmrpcm.setVolume(5);
  if (!SD.begin(SDPIN))
  {
    Serial.println("Initialization Failed!");
    return;
  }
  //********END*****//

  //***Servo***
  servo1.attach(servoPin);
  //********END*****//

  ...

  //***StartUP***
  //tmrpcm.play("60.wav");
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin4, HIGH);
  //delay(10000);
  delay(100);
  //********END*****//
}

...

//***Servo***
void Servo_Motor_Turn()
{
  servo1.writeMicroseconds(2000);
  delay(1000);
  servo1.writeMicroseconds(1500);
  delay(1000);
  servo1.writeMicroseconds(1000);
  delay(1000);
  servo1.writeMicroseconds(1500);
  delay(1000);
}

void Servo_Motor_UnTurn()
{
  servo1.writeMicroseconds(1500);
}
//********END*****//

//***UltraSonic***
long Sensor_loop()
{
  long microseconds, inches;

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  microseconds = pulseIn(pingPin, HIGH);

  //code to convert time to distance
  inches = long (microseconds / 74 / 2);
  //code to print distance values
  Serial.print(inches);
  Serial.print("in, ");
  return inches;

  delay(100);
}

...

//***SPEAKER***
void Speaker()
{
  file = String((i % 62) + 2) + ".wav";//work around for the file naming convention: 2.wav - 63.wav
  Serial.println("playing: " + file);
  tmrpcm.play(file.c_str());
  i++;
}

void loop()
{
  rndNum = random(0, 100);
  //ALL***UltraSonic_Motors_Servo_Speaker_LEDs***ALL
  Serial.println("Count: " + String(count));
  long inches = Sensor_loop();
  {
    if (inches > 6)
    {
      Forward();
      Blink3(rndNum);
    }
    else
    {
      Serial.println("Speaker");
      //Speaker(); //<-------The problem is here: Comment Speaker() out servo works. Comment Speaker() in Servo doesn't work
      Serial.println("ServoTurn");
      Servo_Motor_Turn();

      ...

    }
    count++;
  }
  //********END*****//
}

So my problem is either with the speaker or with the SD card reader.

The problem has been solved(at least partially). It was a conflict with the use of Timer1. I was able to download and use the ServoTimer2 library to work around this problem.

I'll have to put the entire bot(errr... droid) back together before I know if the problem has been totally corrected.