analogWrite(en1Pin,70); //speed A
analogWrite(en2Pin,100); //speed B
digitalWrite(in1Pin,LOW); //1-2 run at speed A
digitalWrite(in2Pin,HIGH);
delay(1500);
digitalWrite(in1Pin,LOW); //1-2 stop
digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,HIGH); //3-4 run at speed B
digitalWrite(in4Pin,LOW);
delay(5000);
digitalWrite(in3Pin,LOW); //3-4 stop
digitalWrite(in4Pin,LOW);
digitalWrite(in2Pin,LOW); //1-2 run at speed A
digitalWrite(in1Pin,HIGH);
delay(1500);
digitalWrite(in1Pin,LOW); //1-2 stop
digitalWrite(in2Pin,LOW);
I have annotated it to help me understand what I think you are trying to do
Can you confirm that the names you have given the pins correspond to the actual inputs on the H bridges ?
I've connected the en1Pin to the ENB and the en2Pin to the ENA, the INPUT pins are correctly connected.
And yes I try to use one H-Bridge. And without trying to regular the speed, it works well.
What H bridge are you using ? Can you provide a link to what you are using ?
If it has ENA and ENB inputs then it is almost certainly 2 H bridges, even if they are in 1 chip, possibly an L293 or similar.
Did you build the circuit yourself or buy it ready made ?
If you built it yourself what is the circuit ?
If ready made is there a link to the product ?
I am all in favour of using functions but unless the inline code works when the inputs to the H bridge are set correctly then a function is not going to help.
To make things even easier to understand I have written a small class to control a motor attached to an H bridge but that does depend on the basic code working, of course.
Does each motor work as expected when it is the only one being controlled ?
A thought. Have you got anything using interrupts, such as a sensor using timer 2, attached to the Arduino ?
I had problems with motor control when I added a ping sensor to my buggy. (Arduino Uno) It stopped PWM working on pins 3 and 11 because its library uses timer 2 which is also used for PWM on those pins. I had to switch the enable pin of one motor from pin 3 to pin 10.
OK, the picture is in the attach.
Yes, you can control one motor, but then the other one does nothing.
And no, I don't use any sensor, just a button and a few LED's