Hello, I am trying to write a program to control a simple robot with 2 DC motors, an H-Bridge, and a Ping sensor on a servo.
This is on an Arduino UNO
I am using a standard servo to control the direction that the Ping sensor faces.
The H-Bridge controls the two wheels of the robot.
My problem is that when ever I have:
myservo.attach(PING_SERVO);
included in the setup and I tell my robot to go forward using the following command:
The second motor on the H-Bridge does not respond. As soon as I comment or delete the following line:
myservo.attach(PING_SERVO);
both motors work fine. :~ Anybody have any ideas why this is?
#include <Servo.h>
Servo myservo;
//NAME //PIN //DISCRIPTION
int ENableM1 = 4; //ENable pin of H-Bridge for Motor 1
int ENableM2 = 2; //ENable pin of H-Bridge for Motor 2
int A1output = 6; //H-Bridge input 1 for Motor 1
int A2output = 9; //H-Bridge input 2 for Motor 1
int A3output = 10; //H-Bridge input 3 for Motor 2
int A4output = 11; //H-Bridge input 4 for Motor 2
int PING_SERVO = 3;
///////////////////////////////////////////////////
///////////////// /////////////////
///////////////// SETUP /////////////////
///////////////// /////////////////
///////////////////////////////////////////////////
void setup()
{
myservo.attach(PING_SERVO); // attaches the servo
pinMode(ENableM1,OUTPUT);
pinMode(ENableM2,OUTPUT);
pinMode(A1output,OUTPUT);
pinMode(A2output,OUTPUT);
pinMode(A3output,OUTPUT);
pinMode(A4output,OUTPUT);
}
///////////////////////////////////////////////////
///////////////// /////////////////
///////////////// MAIN PROGRAM /////////////////
///////////////// /////////////////
///////////////////////////////////////////////////
void loop()
{
digitalWrite(ENableM1,HIGH);
digitalWrite(ENableM2,HIGH);
analogWrite(A1output,88);
analogWrite(A2output,0);
analogWrite(A3output,88);
analogWrite(A4output,0);
}
the only Servo is the one that rotates the ping sensor. I have that tied into the 5v from arduino.
The external 7.4v LiPo battery powers the 2 DC motors.
Arduino is powering the following(From 9v battery):
The servo library prevents use of PWM on pins 11 and 12 so you need to use an alternative pin for A4output.
What sort of 9V battery ? If it is a PP3 then it will last for a very short time before going flat particularly as it is powering a servo. Why not use the LiPo to power everything ?
The servo library prevents use of PWM on pins 11 and 12
Are you sure about that? (Pin 12 not being a PWM pin, and all that...)
Well, I could pretend that I thought that we were talking about a Mega and using between 12 and 23 motors, in which cas PWM is disabled on pins 11 and 12. However, I will own up to misreading the Servo reference page, in which case
On boards other than the Mega, the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.
So, the pins for A2output and A3output will need to be reassigned.
So so far I have the four H-Bridge inputs, the PING sensor, and the PING servo going to PWM pins.
Thats a total of 6 needed pins, but If I can not use 9 and 10, then there is not enough.
Does the PING sensor and the PING Servo need to go to PWM pins or is there a work around for this?
Sorry for all the questions, I am trying to work this out the best I can.
Would it be better to manually write to the servo rather than use the servo library?
I wanted to use the servo library so that I could easily assign a set angle to the servo (0, 45, 90, 180 degs).
What I meant is that I will lose the PWM use of thoes two pins. With the four HBridge inputs, the servo, and the PING sensor, I will need 6 PWM pins, but excluding pins 9 and 10, that only leaves me 4 PWM pins.
Is there a work around for this at all?
Does the HBridge actually need 4 PWM inputs ? In my admittedly limited experience of HBridges each motor has 2 digital inputs, a combination of HIGH and LOW on which controls motor direction, and a PWM input that controls the speed of rotation.
Can you provide a link to the device that you are using ?
No but he has 4 H-bridges, so you need one PWM pin per bridge to control the speed.
You only need two pins per H-Bridge as you can wire any enable permanently high.
No but he has 4 H-bridges, so you need one PWM pin per bridge to control the speed.
You only need two pins per H-Bridge as you can wire any enable permanently high.
I only have on H-Bridge chip that is powering two DC motors.
The Enabels are not an issue. I need 4 PWM pins for 1A, 2A, 3A, and 4A.
With these four, the PING sensor, and the PING servo, I am now short 2 PWM outputs. I was woundering if there was a work around for this?