Motor DC not turning when combining two Arduino codes

Hi there, I have successfully controlled two Motor DCs using L298P Motor driver shield via Bluetooth. I have also created a new separate Arduino code and successfully controlled servo via Bluetooth (for experiment purposes). Now when I combine these two codes together, I get the servo to work, but only one of my Motor DC is turning. When I remove the part (of my combined code) related to servo control, I manage to get both of my Motor DC to turn again. What seems to be the problem?

#include <Servo.h>

Servo servo;

int angle = 90;

int SA = 10;
int SB = 11;
int MA = 12;
int MB = 13;

char command;

void setup(){
servo.attach(A0);

pinMode (SA, OUTPUT);
pinMode (SB, OUTPUT);
pinMode (MA, OUTPUT);
pinMode (MB, OUTPUT);
Serial.begin(9600);
}

void loop(){
if(Serial.available()) {
  command = Serial.read();
  
if (command == '0'){
digitalWrite(MA, LOW);
digitalWrite(MB, LOW);
analogWrite(SA, 0);
analogWrite(SB, 0);
}

if (command == '1'){
digitalWrite(MA, HIGH);
digitalWrite(MB, LOW);
analogWrite(SA, 100);
analogWrite(SB, 0);
}

if (command == '2'){
digitalWrite(MA, LOW);
digitalWrite(MB, HIGH);
analogWrite(SA, 0);
analogWrite(SB, 100);
}

if (command == '3'){
digitalWrite(MA, HIGH);
digitalWrite(MB, HIGH);
analogWrite(SA, 100);
analogWrite(SB, 100);
}

if (command == '4'){
digitalWrite(MA, HIGH);
digitalWrite(MB, LOW);
analogWrite(SA, 100);
analogWrite(SB, 100);
}

if (command == '5'){
digitalWrite(MA, LOW);
digitalWrite(MB, HIGH);
analogWrite(SA, 100);
analogWrite(SB, 100);
}

if (command == '6'){
digitalWrite(MA, LOW);
digitalWrite(MB, LOW);
analogWrite(SA, 100);
analogWrite(SB, 100);
}

if (command == '7'){
digitalWrite(MA, LOW);
digitalWrite(MB, LOW);
analogWrite(SA, 100);
analogWrite(SB, 0);
}

if (command == '8'){
digitalWrite(MA, LOW);
digitalWrite(MB, LOW);
analogWrite(SA, 0);
analogWrite(SB, 100);
}

if(command == 'a'){
constrain(angle, 0, 180);
angle = angle + 3;
Serial.print(angle);
}

if(command == 'b'){
constrain(angle, 0, 180);
angle = angle - 3;
Serial.print(angle);
}

if(command == 'c'){
angle = 90;
Serial.print(angle);
}

servo.write(angle);
delay(100);
}
}

Servo libraries use hardware timers. So do pwm pins. Usually, a servo library will stop some of the pwm pins working. Try moving the analogWrite pins to others.

thanks for the suggestion, i tried moving the servo pin to other PWM pin like pin 3, still the same result, only one motor DC working. I am 100% sure its not the battery problem, because like I said, when I removed all the servo related control, both motors turn like crazy. Is it because I use too much "if" statement?

Don't move your servo pin, move your DC motor pins. The servo library uses Timer1 which is the same timer used for PWM on pins 9 and 10 so that is why that motor is not working.

2 Likes

I see... Thank god somebody finally told me about this, otherwise I could have spent another fruitless whole day troubleshooting. But does that also mean I can no longer use my L298P motor shield? Because the description reads that the motor pins are connected to 10, 11, 12 and 13. Is that possible to move the motor pin somewhere else? I might consider buying another motor driver (probably L298N).

That information is readily available in Servo - Arduino Reference It's often worth checking references when using something new.

You may be able to move SA to another PWM pin, e.g. 3 or 5 but that depends on exactly what motor shield you're using. Some have links set up for that purpose, some don't.

Steve

I see.. Thanks a lot, I will experiment even more.

UPDATE: so I found out that my L298P Motor Shield can only read PIN 10, 11, 12 and 13 (moving motor PIN 10 to another PIN in arduino code will NOT work). So for those who require to use 2 motor DCs and at least a servo like me, I advise two options : either avoid using this motor shield (probably L298N is better option) or buy Arduino Mega.

Or, just use the Servo2 library which doesn't use Timer1.

2 Likes

Thank you for your suggestion, unfortunately I have tried using ServoTimer2; previously only left motor worked, now after using ServoTimer2, ONLY the right motor worked. I found out from the other arduino forum (link below) that ServoTimer2 loses PWM on pins 3 &11 instead of 9 & 10, whereas I still need pins 11 :frowning: I guess I really should consider other motor driver or shield. I have no regrets, though, as I learn new things. Thank you very much :slight_smile:

How to use servo library without disabling PWM pins 9 & 10? - Using Arduino / Programming Questions - Arduino Forum

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