weird motor problem with modified robot car kit

I've been playing with one of these Arduino car kits:

and am having a weird problem...

This one didn't come with wheel encoders, but I got some separately and put them on.
I got the motors working with the encoders. I can use feedback from the encoder to control the motors better. Great.
I got the ultrasonic sensor working with the servo motor that turns it.
But when I tried to put it all together in the same program, it doesn't work.

What it looks like is one of the wheel motors is sticking. It will turn, but only with high PWM, and after a short delay. I thought it was a mechanical problem, but it turns out that is not the case.
I went back to an old program that just controls the wheel motors, and it works fine.
I eventually tracked it down to one line of code: my servo.attach(A2); (this is the servo that turns the ultrasonic sensor. If comment this out, the wheel motors work fine. But of course the servo doesn’t.)
I tried attaching the servo to other pins. Same problem.
I thought maybe it was a problem with not enough power, but the problem persists even when the servo is not connected! Just including that line of code makes the wheel motor not work right.

Any Ideas?

(I know, I'm not posting the code or my system specs... The code is 300+ lines and all the parts seem to work when I run them in separate programs. I only see this problem when it's all run at the same time.)

I know, I'm not posting the code or my system specs.

Do you expect people to just "know" what the problem is?

Please spend a few minutes looking through the "How to use the forum" post, which give hints on getting the most out of your posting adventure.

There's just one advice I guess: Check your wiring and make sure you have written your code correctly LINE BY LINE.

I guess I’m pretty inept. I cant even find a “How to use the forum” post.

I was hoping my issue might sound familiar to long time Arduino robot users.

I’ll try to pare down the code to something short with the same issue.

General Guidance and How to use the Forum

First post, at the top of every forum topic.

Here's a simple program with the problem.
I'm sending pwm to the two wheels of my car.
Both wheels turn when I send them 255.
But ONLY ONE wheel turns when I send 80.
Everything works fine when I comment out the line: myservo.attach(A5);
Any ideas why would this be?


#include <Servo.h>

Servo myservo;
int pinLF=9;
int pinLB=8;
int pinRF=7;
int pinRB=6;
int enA=10;
int enB=5;

void setup()
myservo.attach(A5); // attach a servo on pin A5 ***THIS LINE IS AFFECTING MY LEFT WHEEL MOTOR
for (int i = 2; i <11; i ++) pinMode(i, OUTPUT);
digitalWrite(pinLF,HIGH); // L motor forward
digitalWrite(pinRF,HIGH); // R motor forward

void loop()
analogWrite(enA,80); // send pwm speed 80 to motors
delay (1000);
analogWrite(enA,0); // stop motors
delay (1000);
analogWrite(enA,255); // send pwm speed 255 to motors
delay (1000);
analogWrite(enA,0); // stop motors
delay (1000);

Better, but please use code tags ("</>") instead of quote tags for code.

This warning, which explains the problem, can be found on the Arduino Servo Reference Page:

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

OMG! That sounds like it! Thank you!