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)