Arduino Acts weird when using servo libary?

Hello, I am trying to write a program to control a simple robot with 2 DC motors, an H-Bridge, and a Ping sensor on a servo.
This is on an Arduino UNO
I am using a standard servo to control the direction that the Ping sensor faces.
The H-Bridge controls the two wheels of the robot.
My problem is that when ever I have:

myservo.attach(PING_SERVO);

included in the setup and I tell my robot to go forward using the following command:

analogWrite(A1output,88);
analogWrite(A2output,0);
analogWrite(A3output,88);
analogWrite(A4output,0);

The second motor on the H-Bridge does not respond. As soon as I comment or delete the following line:

myservo.attach(PING_SERVO);

both motors work fine. :~ Anybody have any ideas why this is?

#include <Servo.h> 
Servo myservo; 

     //NAME    //PIN     //DISCRIPTION
int ENableM1   = 4;      //ENable pin of H-Bridge for Motor 1
int ENableM2   = 2;      //ENable pin of H-Bridge for Motor 2

int A1output   = 6;      //H-Bridge input 1 for Motor 1
int A2output   = 9;      //H-Bridge input 2 for Motor 1
int A3output   = 10;     //H-Bridge input 3 for Motor 2
int A4output   = 11;     //H-Bridge input 4 for Motor 2

int PING_SERVO = 3;

///////////////////////////////////////////////////
/////////////////                 /////////////////
/////////////////      SETUP      /////////////////
/////////////////                 /////////////////
///////////////////////////////////////////////////

void setup()
      {
        myservo.attach(PING_SERVO);  // attaches the servo 
        pinMode(ENableM1,OUTPUT);
        pinMode(ENableM2,OUTPUT);
        pinMode(A1output,OUTPUT);
        pinMode(A2output,OUTPUT);
        pinMode(A3output,OUTPUT);
        pinMode(A4output,OUTPUT);
      }

///////////////////////////////////////////////////
/////////////////                 /////////////////
/////////////////  MAIN PROGRAM   /////////////////
/////////////////                 /////////////////
///////////////////////////////////////////////////
      
void loop() 
{
    
      digitalWrite(ENableM1,HIGH);
      digitalWrite(ENableM2,HIGH);  

      analogWrite(A1output,88);
      analogWrite(A2output,0);
      analogWrite(A3output,88);
      analogWrite(A4output,0);  
}

Are you powering all this from the Arduino 5V or an external power supply?

The H-Bridge chip is powered by the 5v from arduino, but the motors are power from an external battery pack.

I thought that was the problem at first, but I got a dedicated battery pack for the motors. The arduino is powered by a 9v battery.

What about the Servos? Servos should generally be powered externally as well.

the only Servo is the one that rotates the ping sensor. I have that tied into the 5v from arduino.
The external 7.4v LiPo battery powers the 2 DC motors.

Arduino is powering the following(From 9v battery):

  1. 5v H-Bridge power supply
  2. Ping Sensor
  3. Servo that rotates Ping

Is this still too much on the Arduino?

The servo library prevents use of PWM on pins 11 and 12 so you need to use an alternative pin for A4output.

What sort of 9V battery ? If it is a PP3 then it will last for a very short time before going flat particularly as it is powering a servo. Why not use the LiPo to power everything ?

The servo library prevents use of PWM on pins 11 and 12

Are you sure about that? (Pin 12 not being a PWM pin, and all that...)

PaulS:

The servo library prevents use of PWM on pins 11 and 12

Are you sure about that? (Pin 12 not being a PWM pin, and all that...)

Well, I could pretend that I thought that we were talking about a Mega and using between 12 and 23 motors, in which cas PWM is disabled on pins 11 and 12. However, I will own up to misreading the Servo reference page, in which case

On boards other than the Mega, the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.

So, the pins for A2output and A3output will need to be reassigned.

Thanks for the help.

So so far I have the four H-Bridge inputs, the PING sensor, and the PING servo going to PWM pins.
Thats a total of 6 needed pins, but If I can not use 9 and 10, then there is not enough.
Does the PING sensor and the PING Servo need to go to PWM pins or is there a work around for this?

Sorry for all the questions, I am trying to work this out the best I can.
Would it be better to manually write to the servo rather than use the servo library?
I wanted to use the servo library so that I could easily assign a set angle to the servo (0, 45, 90, 180 degs).

Would it be better to manually write to the servo rather than use the servo library?

No.

but If I can not use 9 and 10, then there is not enough.

It does not stop you using those pins, it just stops you using PWM on those pins.

What I meant is that I will lose the PWM use of thoes two pins. With the four HBridge inputs, the servo, and the PING sensor, I will need 6 PWM pins, but excluding pins 9 and 10, that only leaves me 4 PWM pins.
Is there a work around for this at all?

Is there a work around for this at all?

More than one. SoftwareServo is one approach. Using a Mega with more timers and more PWM pins is another.

The servo doesn't need a PWM pin. Neither does the Ping sensor.

Does the HBridge actually need 4 PWM inputs ? In my admittedly limited experience of HBridges each motor has 2 digital inputs, a combination of HIGH and LOW on which controls motor direction, and a PWM input that controls the speed of rotation.

Can you provide a link to the device that you are using ?

Does the HBridge actually need 4 PWM inputs ?

No but he has 4 H-bridges, so you need one PWM pin per bridge to control the speed.
You only need two pins per H-Bridge as you can wire any enable permanently high.

This is the HBridge that I am using:

Grumpy_Mike:

Does the HBridge actually need 4 PWM inputs ?

No but he has 4 H-bridges, so you need one PWM pin per bridge to control the speed.
You only need two pins per H-Bridge as you can wire any enable permanently high.

I only have on H-Bridge chip that is powering two DC motors.
The Enabels are not an issue. I need 4 PWM pins for 1A, 2A, 3A, and 4A.
With these four, the PING sensor, and the PING servo, I am now short 2 PWM outputs. I was woundering if there was a work around for this?

If you use digital pins connected to 1A and 2A for directional control and put PWM on the enable pin you only need one PWM output per motor.

This is an example program for a motor control library that I wrote. As you will see it controls 2 motors with only 2 PWM outputs.

#include <bbMotor.h>

bbMotor leftMotor(6,8,7);  //enable (PWM), InA, InB
bbMotor rightMotor(3,2,4);  //enable (PWM), InA, InB

#define LOW_SPEED 10
#define HIGH_SPEED 50
#define FULL_SPEED 255

void setup()
{
  leftMotor.begin();
  rightMotor.begin();
  delay(5000);
}

void loop()
{
  accelerate(100);
  delay(3000);
  leftMotor.hardStop();
  rightMotor.hardStop();
  delay(3000);
}

void forwardBoth(int speed)
{
  leftMotor.forward(speed);
  rightMotor.forward(speed);
}

void spinLeft(int speed)
{
  leftMotor.reverse(speed);
  rightMotor.forward(speed);
}

void softStopBoth()
{
  leftMotor.softStop();
  rightMotor.softStop(); 
}

void accelerate(int targetSpeed)
{
  for (int speed = 1;speed <targetSpeed;speed++)
  {
    leftMotor.forward(speed);
    rightMotor.forward(speed);
    delay(100);
  } 
}

I only have on H-Bridge chip that is powering two DC motors.

No that chip has four half H-bridges, you need one full bridge per motor.