Hi All, rookie Arduino programmer here...
I've got a system with 2 DC motors and one servo motor. I'm trying to control all of them via the letters A,B,C on the classic keypad. I'm using an L298 H-Bridge to control both DC motors. "A" toggles the position of the servo, "B" controls motor 1, and "C" controls motor 2. The issue I'm running into is this - if I exclude all references to the servo motor, then both DC motors run fine when their respective buttons are toggled. However, if I include a reference to the servo, attach it to a pin, and write some code to set its position, motor 1 doesnt turn on. Pushing "A" on the keypad toggles the servo successfully, pushing "C" toggles power to motor 2 successfully... but pushing "B" which should toggle power to motor 1, doesnt work. In fact, if I watch carefully, I see the servo "jump" or "spasm" for a split second on some presses of "B"...
The code I have written for this sketch has a bunch of other items in it, but the same issue presents itself if I use the code from this tutorial, shown below, and changed to match my pinouts and include a servo:
//#include <Servo.h>
//Servo myservo;
//// servo variables
//const int vpos = 100; // one position of servo
//const int hpos = 180; // 2nd pos of servo
// connect motor controller pins to Arduino digital pins
// motor one
int enA = 9;
int in1 = 2;
int in2 = 7;
// motor two
int enB = 3;
int in3 = 5;
int in4 = 4;
void setup()
{
// 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);
// myservo.attach(6); // set the PWM pin of the motor to pin 6
// myservo.write(vpos);
}
void loop()
{
// this function will run the motors in both directions at a fixed speed
// turn on motor A
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enA, 200);
// turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enB, 200);
delay(2000);
//myservo.write(hpos);
// now change motor directions
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
delay(2000);
//myservo.write(vpos);
}
I've commented out all the lines that reference the servo. In this case, the code works perfectly. It turns on both motors, reverse direction, then turns them off. If I uncomment all the servo lines, the code executes but motor A never turns on...
Here are some things I've tried:
- verified that both motors work
- switched the motors to the opposite output of the L298, and whichever motor is on side A of the L298 will not turn on
- if I disconnect the servo, but still maintain the code segments that reference the servo, the problem still persists - to me this means its not a wiring issue, am I right?
- I have 2 L298 H-bridges, and the problem exists for both...
Thanks for any advice/guidance!
Jason