Head scratcher with h bridge

Hi,

I have an h-bridge with a single motor and I’ve got the motor rotating ‘forward’ at speed 100 for 4 seconds, I then double the speed and let it run for another 4 seconds. I then set the motor to rotate in reverse at speed 100 for 2 seconds followed by rotating in reverse at double the speed for another 2 seconds. The code then transitions back to the top of the loop and runs the same sequence over again.

When I run this code the first reverse step doesn’t execute ie the motor doesn’t run in reverse, however it does wait the required amount of time. The subsequent reverse step at twice the speed does work as expected as do the forward steps.

The code isn’t complicated, I just can’t think of why the first reverse wouldn’t execute, particularly when the second reverse call does.

// Constants
const int enableBridge1 = 2;
const int MotorForward1 = 3;
const int MotorReverse1 = 4;
const int MotorForward2 = 5;
const int MotorReverse2 = 6;
const int STOP    = 0;
const int FORWARD = 1;
const int REVERSE = 2;
const int LEFT  = 1;
const int RIGHT = 2 ;

void setup(){
  Serial.begin (115200);  
  pinMode(MotorForward1,OUTPUT);
  pinMode(MotorReverse1,OUTPUT);
  pinMode(enableBridge1,OUTPUT);
  digitalWrite(enableBridge1,HIGH); // Enable Hbridge 1
}
void loop(){
  Serial.println ("Forward 100");
  runMotor(LEFT, FORWARD, 100,4000);
  Serial.println ("Forward 200");
  runMotor(LEFT, FORWARD, 200,4000);
  Serial.println ("Reverse 100");
  runMotor(LEFT, REVERSE, 100,2000);
  Serial.println ("Reverse 200");
  runMotor(LEFT, REVERSE, 200,2000);
  
}
void runMotor(int motor, int mode, int power, int delayAmount){
  if (motor == LEFT){
    if (mode == FORWARD){
      analogWrite(MotorReverse1,0);
      analogWrite(MotorForward1,power);
    } 
    if (mode == REVERSE){
      Serial.println ("yup in here!");
      analogWrite(MotorForward1,0);
      analogWrite(MotorReverse1,power);
    }
    if (mode == STOP){
      analogWrite(MotorForward1,0);
      analogWrite(MotorReverse1,0);
    }
  } else if (motor == RIGHT){
    if (mode == FORWARD){
      analogWrite(MotorReverse2,0);
      analogWrite(MotorForward2,power);
    } 
    if (mode == REVERSE){
      analogWrite(MotorForward2,0);
      analogWrite(MotorReverse2,power);
    }
    if (mode == STOP){
      analogWrite(MotorForward2,0);
      analogWrite(MotorReverse2,0);
    }
  } else{
      // all stop
      analogWrite(MotorReverse1,0);
      analogWrite(MotorForward1,0);
      analogWrite(MotorReverse2,0);
      analogWrite(MotorForward2,0);
  }
  if (delayAmount >0){
      delay(delayAmount);
  }
}

Which driver do you use?
And which board do you use?

This reads like you have 2 motors - what do the lines really mean?

const int MotorForward1 = 3;
const int MotorReverse1 = 4;
const int MotorForward2 = 5;
const int MotorReverse2 = 6;

...R

Proper use of code tags on your first post!
Karma++

I see that you have a debugging statement in there. Do you get it on the serial monitor on the first execution where the motor mysteriously does not reverse?

const int MotorReverse1 = 4;
//
runMotor(LEFT, REVERSE, 100,2000);

Pin 4 is NOT a PWM pin, if you analogWrite less than 128, it will be set LOW, otherwise HIGH.
Try pin 11.

Thank you, that makes sense.

For completeness
Driver is a SN754410NE
Board is a Uno

example was copied from

Was coded for 2 motors however only one (the LEFT motor/MotorForward1) was used, the const in lines represented the pin numbers.

Debugging statements did execute on the first run through.

Thanks for all your responses.

Yeah, then the trouble is indeed that pin 4 isn't a PWM pin. Switch to pin pins 3, 9 10 and 11. Pins 5 and 6 have a different PWM frequency which is probably noticeable.

And you're missing enable for driver 2 :wink:

And if you want to do it all easier, switch to using the enable line as the PWM input to the driver and connect the A inputs to digital pins. That way you only need two PWM pins, one per motor.

As a next step in learning, you could probably squash all your if/else block into about ten lines with switch/case statements.

Reduce the repetition of code.
A simple move(direction) function would also help with that.