Error in running both the servos and the DC motors powering the wheels

Hi.

If anyone can help me out with this issue, I would greatly appreciate it.

Im planning to build a robot car that not only can I move the wheels using 2 brushed DC motors, but at the same time, I can control the gripper installed to the car as well, which uses servo motors to set it into motion.

I choose to run everything on my car using an L298P 2A motor shield that is placed on top of my Arduino Uno.

So far, I managed to get the car moving using this manual code I attached to power these DC motors.

And I also managed to control the servos for gripper motion using the Servo code I attached.

But now, I decided to combine the two together, so that I can control the motors running the wheels and the servo motors together. But the problem is that, the servos still function as expected, but my DC motors seem to glitch out a little. Only the right DC motor moves as how it should be, but the left one just doesn't respond well. Here is the code:

int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;
char incoming_byte = 0;

#include<Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;

int pos = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode(M1, OUTPUT); //Left wheel//
pinMode(M2, OUTPUT); //Right wheel
myservo1.attach(3);
myservo1.write(70);
myservo2.attach(5);
myservo2.write(70);
myservo3.attach(7);
myservo3.write(25);

}

void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0)
{
incoming_byte=Serial.read();

////////////Servo1////////////
if(incoming_byte=='F')
{ myservo1.write(30);
}
else if(incoming_byte=='G')
{ myservo1.write(70);
}
else if(incoming_byte=='H')
{ myservo1.write(45);
}
else if(incoming_byte=='J')
{
myservo1.write(104);
}
else if(incoming_byte=='L')
{ myservo1.write(116);
}

////////////Servo2////////////
if(incoming_byte=='E')
{ myservo2.write(30);
}
else if(incoming_byte=='R')
{ myservo2.write(45);
}
else if(incoming_byte=='T')
{
myservo2.write(70);
}
else if(incoming_byte=='Y')
{ myservo2.write(104);
}
else if(incoming_byte=='U')
{ myservo2.write(116);
}
////////////Servo3////////////

if(incoming_byte=='I')
{ myservo3.write(25);
}
else if(incoming_byte=='O')
{
myservo3.write(110);
}
else if(incoming_byte=='P')
{ myservo3.write(120);
}

////////////Forward////////////
if (incoming_byte == 'w')
{
digitalWrite(M1, HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 120);
analogWrite(E2, 120);
}
else if (incoming_byte == 'X') {

digitalWrite(M1, HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 0);
analogWrite(E2, 0);

}

////////////Reverse////////////
if (incoming_byte == 's')
{

digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 60);
analogWrite(E2, 60);
}
else if (incoming_byte == 'X')
{

digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 0);
analogWrite(E2, 0);
}

/////Left/////
if (incoming_byte == 'a')
{
digitalWrite(M1, HIGH);
digitalWrite(M2, LOW);
analogWrite(E1, 120);
analogWrite(E2, 120);
}
else if (incoming_byte == 'X')
{ digitalWrite(M1, HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 0);
analogWrite(E2, 0);
}

////Right/////
if (incoming_byte == 'd')
{
digitalWrite(M1, LOW);
digitalWrite(M2, HIGH);
analogWrite(E1, 120);
analogWrite(E2, 120);
}
else if (incoming_byte == 'X')
{ digitalWrite(M1, HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 0);
analogWrite(E2, 0);
}

}

}

This is one of my first Arduino projects. I know this may be something simple for you out there and hopefully I can learn from you guys. I would sincerely appreciate if you can help me out.

Thanks a lot!

Manual_control_for_TeamONE.ino (1.72 KB)

Servo_control_for_Team_ONE.ino (1.49 KB)

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What are you powering your project with?
The motor driver you have chosen is not the most efficient, you will loose 2V across it for a start.

Thanks.. Tom... :slight_smile:

You seem to be using the same pins for the servos and the motors

int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;

 myservo2.attach(5);
 myservo2.write(70);
 myservo3.attach(7);

That's why it is always a good idea to give the pins meaningful names and not just use the numbers.

...R

It worked!!! Thanks a lot Robin!!! Problem solved! Maann I didn't notice that mistake earlier...

Hi,
Good to hear you got it working.
Can you post your working code please?
This is so anyone else using this thread will have the solution and this thread will be of use to many others.

Thanks.. Tom.. :slight_smile:

Hi.
This is the working code. I've changed the pins to connect my 4 servos to, such that they wouldn't disrupt the power for the DC motors (by setting the pin numbers other than 4, 5, 6 & 7), and that solved the problem. (CORRECTED PORTION IS MARKED)

int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;
char incoming_byte = 0;

#include<Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;

int pos = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode(M1, OUTPUT); //Left wheel//
pinMode(M2, OUTPUT); //Right wheel
myservo1.attach(2); ////START OF CORRECTION
myservo1.write(70);
myservo2.attach(3);
myservo2.write(70);
myservo3.attach(8);
myservo3.write(140);
myservo4.attach(9);
myservo4.write(50);
////END OF CORRECTION

}

void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0)
{
incoming_byte=Serial.read();

////////////Servo1////////////
if(incoming_byte=='F')
{ myservo1.write(30);
}
else if(incoming_byte=='G')
{ myservo1.write(70);
}
else if(incoming_byte=='H')
{ myservo1.write(45);
}
else if(incoming_byte=='J')
{
myservo1.write(104);
}
else if(incoming_byte=='L')
{ myservo1.write(116);
}

////////////Servo2////////////
if(incoming_byte=='E')
{ myservo2.write(30);
}
else if(incoming_byte=='R')
{ myservo2.write(45);
}
else if(incoming_byte=='T')
{
myservo2.write(70);
}
else if(incoming_byte=='Y')
{ myservo2.write(104);
}
else if(incoming_byte=='U')
{ myservo2.write(116);
}
////////////Servo3////////////

if(incoming_byte=='I')
{ myservo3.write(25);
}
else if(incoming_byte=='O')
{
myservo3.write(110);
}
else if(incoming_byte=='P')
{ myservo3.write(120);
}

////////////Forward////////////
if (incoming_byte == 'w')
{
digitalWrite(M1, HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 120);
analogWrite(E2, 120);
}
else if (incoming_byte == 'X') {

digitalWrite(M1, HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 0);
analogWrite(E2, 0);

}

////////////Reverse////////////
if (incoming_byte == 's')
{

digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 60);
analogWrite(E2, 60);
}
else if (incoming_byte == 'X')
{

digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 0);
analogWrite(E2, 0);
}

/////Left/////
if (incoming_byte == 'a')
{
digitalWrite(M1, HIGH);
digitalWrite(M2, LOW);
analogWrite(E1, 120);
analogWrite(E2, 120);
}
else if (incoming_byte == 'X')
{ digitalWrite(M1, HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 0);
analogWrite(E2, 0);
}

////Right/////
if (incoming_byte == 'd')
{
digitalWrite(M1, LOW);
digitalWrite(M2, HIGH);
analogWrite(E1, 120);
analogWrite(E2, 120);
}
else if (incoming_byte == 'X')
{ digitalWrite(M1, HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 0);
analogWrite(E2, 0);
}

}

}

Hi,
For the umpteenth time.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Tom... :o :o :o

It worked!!!

Taht's good news. Now improve it

    else if (incoming_byte == 'X')You seem obsessed with testing whether you have received an 'X' because you do it multiple times.

Your code would be more efficient if it were to use if/else throughout to avoid the need for the tests after a successful one has executed the required code. incoming_byte can, after all, only have one value at any one time.

Better still, in my opinion, would be to use switch/case based on the value of incoming_byte. At the very least it would make the code easier to read and maintain.

Whilst making changes you should consider changing your variable declarations to use the smallest data type necessary and to make them const where they will never change. For example

const byte E1 = 5;
const byte M1 = 4;
const byte E2 = 6;
const byte M2 = 7;

Meaningful names for the servos would also help