Engine control and Servo conflicting - what is wrong?

Hi All

Hope someone can help me out here as I'm truely confused :fearful: .

I acquired an Arduino controlled toy car with an L298n engine controller and a servo (and a lot more).

With big success I have been playing with L298n controlled engines, servo, ultrasonic, IR-tracker, IR-controller, Bluetooth-controller, LEDs, stepper engine, ... . All works and I am very happy :slight_smile:
I even managed to setup a bluetooth controlled car able to control direction and speed by tilting the phone 8)

BUT...! :cry:

When combining the servo and the engine control, suddenly the engine control goes haywire :o

To zoom in on the error I simplified and finally basically implemented the enging control code from comment #4 in 'Problem with Arduino Uno, L298N and DC Motors - Motors, Mechanics, Power and CNC - Arduino Forum'. See my version below.

Without the line 'myservo.attach(2);' all works as planned. Left and right side wheels goes reverse slowly stepping up the speed, next goes forward stepping up the speed. All according to plan.

Adding the 'myservo.attach(2);' line, suddenly the engine behaves differently. Reverse is ok, but when shifting to forward the servo flickers for a few seconds and now only the right-side engine goes forward.

This change is purely caused by the line 'myservo.attach(2);', and I have no idea why or how to solve this. After reducing the program I also swapped the pins but I cannot identify the problem.

Can anyone help, please?

PROGRAM:

#include <Servo.h>
Servo myservo;//create servo object to control a servo

//Engine pins
const int pinI1 = 9 ; // define interfaces I1
const int pinI2 = 6; // define I2 interfaces
const int EnA = 11 ; // define EA (PWM control) Interface
const int pinI3 = 3; // define I3 Interface
const int pinI4 = 5; // define I4 Interface
const int EnB = 10 ; // define EB (PWM control) Interface

void setup()
{
pinMode(EnA,OUTPUT);
pinMode(EnB,OUTPUT);
digitalWrite(EnA,HIGH); //Activate motor A
digitalWrite(EnB,HIGH); //Activate motor B

//Attach servo
//myservo.attach(2); //THIS IS THE LINE THAT MAKES THE ENGINE BEHAVE DIFFERENTLY!
}

void loop()
{
int i;
for (i = 0; i < 255; i++)
{
analogWrite(pinI1,LOW); //spin in one direction A
analogWrite(pinI2,i);
analogWrite(pinI3,LOW); //spin in one direction B
analogWrite(pinI4,i);
delay(100);
}
delay(3000);

for (i = 0; i < 255; i++)
{
analogWrite(pinI1,i); //spin in the other direction A
analogWrite(pinI2,LOW);
analogWrite(pinI3,i); //spin in the other direction B
analogWrite(pinI4,LOW);
delay(100);
}
delay(3000);

analogWrite(pinI1,LOW); //stops A
analogWrite(pinI2,LOW);
analogWrite(pinI3,LOW); //stops B
analogWrite(pinI4,LOW);
delay(5000);
}

Both servos and analogWrite() use the hardware timers. You cannot use some PWM pins when you are using servos. Read the documentation for the servo library to find out which ones.

The servo library interferes with PWM on pins 9 and 10. RTFM. Use other pins for the L298

I would also advise using more meaningful names than pin1, pin2 etc.

...R
Planning and Implementing a Program

MorganS and Robin2 - Really appreciate the help. I did read a lot of documentation, but didn't manage to find that info. Thanks :smiley: