Getting Motor and Servo to run at the same time

Dear All

I have a problem similar to Can't Get Motor and Servo to Run at Same Time - Uno, which didn't come to a solution afaics.

I'm working on the robot-car thing. The deal is I have an L298 test-program for the wheel-motors (motor A and motor B) that I lifted from somewhere (hat-tip to the author and apols for not crediting) that works fine

// connect motor controller pins to Arduino digital pins
// motor A
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor B
int enB = 5;
int in3 = 6;
int in4 = 7;

void setup()
{
  Serial.begin(9600);
  // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}

void loop()
{
  demoOne();
  delay(5000);
}

void demoOne()
{
  // this function will run the motors in both directions at a fixed speed

  // turn on motor A
  digitalWrite(in1, HIGH); // This is the pattern for Motor A going forward
  digitalWrite(in2, LOW);  // This is the pattern for Motor A going forward
  Serial.println("Motor A engaged");
  
  // set speed to 200 out of possible range 0~255
  analogWrite(enA, 200);


  // turn on motor B
  digitalWrite(in3, LOW);  // This is the pattern for Motor B going forward
  digitalWrite(in4, HIGH); // This is the pattern for Motor B going forward
  Serial.println("Motor B engaged");

  // set speed to 200 out of possible range 0~255
  analogWrite(enB, 200);

  Serial.println("Both motors in reverse");
  delay(2000);

  // now change motor directions

  digitalWrite(in1, LOW);  // This is the pattern for Motor A in reverse
  digitalWrite(in2, HIGH); // This is the pattern for Motor A in reverse

  digitalWrite(in3, HIGH); // This is the pattern for Motor B in reverse
  digitalWrite(in4, LOW);  // This is the pattern for Motor B in reverse

  Serial.println("Both motors going forward");
  delay(2000);

  // now turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  Serial.println("Both motors off");
}

``

However when I incorporate a tweak of this code into the robot-car program only motor B starts - I can swap the order the motors start, I can even remark-out the code for motor B - motor A just won't start.

After further fiddling about I've discovered that if I remark-out the servo-code (which moves the ultrasonic-head left and right to check for obstacles), motor A starts as does motor B.

I'm powering the wheelmotors from a stack of assorted batteries and I know from the L298 test-code that power from that lot isn't a problem. I'm powering the Arduino (and indirectly the servo motor)  via the USB - once I've got the whole thing working I will power the Arduino separately from the wheel-motors.

The earlier thread suggested all those analogWrites() in close conjunction might be a problem, but they work fine in the L298 test-code so why not in the main car-code?

The servo motor is powered from pin 4 on the Arduino if that's in any way relevant. I have a separate servo-test program that works fine.

Help welcome!

Edited to include the missing back-quote. Sorry!

The reference page for the Servo library is quite informative.
Note the bit where it describes the loss of PWM function on certain pins.

1 Like

The Servo library uses timer1 of the processor and so does analogWrite() on pins 9 and 10. Hence when using the Servo library analogWrite() cannot be used on those pins

1 Like

Thanks to both TheMemberFormerlyKnownAsAWOL and UKHeliBob. The code now reads ...

int enA = 11;
int in1 = 12;
int in2 = 13;

... and works both in the L298 test code and in the robot-car code.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.