Reversing DC motor with Arduino Leonardo and L298N

I am using an Arduino Leonardo hooked up to two Mabuchi FC280-SA (2470) DC motors through an L298N. This is the data sheet for the L298N for reference:

I currently am using the following set-up to run the motors at different speeds relative to one another dependent on the feedback from two Ultrasonic Module Hc-Sr04 Distance Measuring Transducer Sensors for Arduino:

Pin 4 to a voltage an external power source (not the Arduino).
Pin 8 directly to GND
Pin 9 to +5v from Arduino
Pin 7, 12 to a digital output each
Pin 6, 11 to an analogue PWM output each
Pin 3,14 to a motor each
Pin 1,15 to GND with a very low 5Watt resistor each.

I am able to accomplish all my forward motion, turning, and stopping needs with this minimalistic set-up, but now I would like to implement a reverse function. I want the robot to reverse when both sensors fail their respective boolean functions, without requiring any action from an external user. What steps do I need to take to achieve this sort of reverse function? I'm willing to buy more parts if it's necessary.

I should add that I've owned an Arduino unit for all of a day at this point and have found these forums very helpful thus far, but I'm a total beginner who's just getting by with Google searches, a decent understanding of C language, and a bunch of patience.

Add something along these lines to your code.

if (sensor1 == low && sensor2 == low) {
// do whatever digitalWrite magic you need to reverse the motors.
}

Reverse should just be a reversal of the IO pins' polarity to the motor.

EDIT... shouldn't all 4 outputs from the 298 be connected?- 2 & 3 to one motor, 13 & 14 to the other. Those are the polarity you reverse.... high/low becomes low/high

That's actually what I currently have in my code. The "digitalMagic" is the issue.

My problem is that I don't know how to reverse the polarity without physically doing so myself.

EDIT in reply to your edit: ...hold on I need to try

See my EDIT above...

There should be 2 pins to a motor, which I didn't explain well.

There should be two Arduino -> 298 signales per motor too... 5/7 and 10/12... those you need to switch from high/low to low/high

(The 2x inputs per motor are just reflected back out on the respective 2x outputs, just at the right voltage and current for the motor. So there are 3x pins per motor: the enable and two inputs. PWM the enable, and have direction polarity on the inputs = direction polarity on the outputs but PWM'd)

Uh, the third reply muddled my confidence a bit, but I sort of get it. Regardless, the extra pins to a motor answer is something I can experiment with now. Unfortunately I left all my project tools at school so I can't start playing at this moment, but I will either report back on successful implementation after I try tomorrow or quietly praise you in the real world and let my thread die.

Sorry... maybe this is clearer...

Each motor is controlled by 3x 298 pins: 1x Enable and 2x Input. The enable is PWM'd on one of your Arduino outputs. The 2x inputs are given opposite polarity on 2x Arduino outputs, say high and low digitalWrites respectively. That gives a + and - on the motor outputs, and let's say that's "forward"... and those + and - are pulsed for speed according to your PWM input.

So to "reverse" direction, you need to swing the Arduino output digitalWrites to give low and high (as opposed to high and low) on the inputs to the 298... that in turn comes out the other side as - and + as opposed to + and - and turns the motor the other way.

I've had time to go through it now and I can't get reverse to happen.

Running OUTPUT 2 to one side of the motor and OUTPUT 1 to the other, then declaring INPUT 2 to be LOW and INPUT 1 to be HIGH does not make the motor turn. This makes sense to me as there's no ground, but I had tot try it to see if that's what you were saying.

What I really thought you meant was setting up the motor with OUTPUT 2 going in one side of the motor, and going to ground on the other, and running OUTPUT 1 through the first circuits grounded side and having it be grounded on the opposite side. This way I could use HIGH and LOW to run forward through one circuit and reverse through the other.
However, setting up the motor to have these two circuits flowing through it has stumped me. How do I set up the circuit so that there's a ground on either on either side of the motor with the voltage sources on either side as well? It seems like I'm just going to create a short circuits on either side of the motor.

Test the motor by itself with power directly from your supply- take the Arduino and the 298 out of the equation.

Put the power on one way +/ - then reverse it -/+ and the motor should go both ways....

Yes, the motor is able to spin both ways.

Best post your complete code then, and a circuit diagram

My current Set-up with the L298N is:

Pin 4 to a voltage an external power source (not the Arduino).
Pin 8 directly to GND
Pin 9 to +5v from Arduino
Pin 7,5, 12,10 to a digital output each
Pin 6, 11 to an analogue PWM output each
Pin 2,3,13,14 to a motor each
Pin 1,15 to GND with a very low 5Watt resistor each.

Each pin is plugged into the digital input/output pin on the arduino board of the same number (this set-up does plug 6 and 11 into PWM outputs as necessary).

My code is set up to try to run the motors without ground at the moment, as this was my last ditch effort before posting for further help. No sensors are incorporated into the code right now, and is very simple:

# define expression 'forward' // boolean choice
# define forinputA 5 // input 1 (pin 5)
# define forinputB 10 // input 3 (pin 10)
# define revinputA 7 // input 2 (pin 7)
# define revinputB 12 // input 4 (pin 12)
# define enableB 11 // Enable (B)
# define enableA 6 // Enable (A) 

void setup() {
  pinMode(forinputB, OUTPUT);
  pinMode(forinputA, OUTPUT);
  pinMode(revinputB, OUTPUT);
  pinMode(revinputA, OUTPUT);
  pinMode(enableB, OUTPUT);
  pinMode(enableA, OUTPUT);
}

void loop() {
  //'forward' 
  if ( expression == 'forward' ) {
    digitalWrite(forinputB, HIGH);
    digitalWrite(revinputB, LOW);
    digitalWrite(forinputA, HIGH);
    digitalWrite(revinputA, LOW);
    analogWrite(enableB, 255);
    analogWrite(enableA, 255);
    delay(250);
  }
  
  else if ( expression == 'left' ) {
    digitalWrite(forinputB, HIGH);
    digitalWrite(revinputB, LOW);
    digitalWrite(forinputA, HIGH);
    digitalWrite(revinputA, LOW);
    analogWrite(enableB, 255);
    analogWrite(enableA, 40);
    delay(250);
  }
  
  else if ( expression == 'right' ) {
    digitalWrite(forinputB, HIGH);
    digitalWrite(revinputB, LOW);
    digitalWrite(forinputA, HIGH);
    digitalWrite(revinputA, LOW);
    analogWrite(enableB, 40);
    analogWrite(enableA, 255);
    delay(250);
  }
  
  else if ( expression == 'reverse' ) {
    digitalWrite(forinputB, LOW);
    digitalWrite(revinputB, HIGH);
    digitalWrite(forinputA, LOW);
    digitalWrite(revinputA, HIGH);
    analogWrite(enableB, 255);
    analogWrite(enableA, 255);
  }
  
  else {
    digitalWrite(forinputB, HIGH);
    digitalWrite(revinputB, LOW);
    digitalWrite(forinputA, HIGH);
    digitalWrite(revinputA, LOW);
    analogWrite(enableA, 100);
    analogWrite(enableB, 100);
    delay(10);
  }
}

Not sure from your description, which is why a diagram is better 8), if the Arduino's ground and the 298 's ground are connected?

It is yes. Pin 8 to Arduino's GND is GND to GND. It runs forward with motors set-up as in the original post, and not at all without grounding the motors.

If diagram is still necessary, I shall figure out how to get one up on here tomorrow.

I don't know what you mean by "grounding the motors"... they only have two connections? One is + and one is - (or vice versa) as you proved by direct conn to the power.

How does "expression" get changed. You should put some serial monitor debugs into the various legs of the if to see where it's actually going... Serial.println("I'm in the left turn part");

Easiest way to do a circuit is to draw it by hand, take a photo with phone. Go to Additional Options below the typing area and attach it.

Diagram: (seriously big thank you for your continuous help, I'll try to keep adding "karma".

For referencing in my drawing, this is the Ardunio Leonardo:

this is the L298N:

and these are essentially the motors. The little metal clips pictured on top are what I refer to as "CLIP 1" and "CLIP 2":

The way I had the motors working before was by having only one output connected to the clip, and the other one connected ground. This is why I'm fixated on needing to have one one of the clips of the motor connected to ground.

The expression statement needs to be changed manually. In actual application expression doesn't exist and I have senors dictating which boolean statement I need to be in. I have left them out of the code right now and am manually choosing which direction I want to be traveling in order to learn how to drive the motors backwards and forwards.
I have added Serial.print statements to every block of code now. Because it is such a minor change I have not re-uploaded the code.

I am having the same issue for my project. Other groups bought the "h-bridge" but we just have the L298N. Am I missing any other electrical components in order for the driver to work?

I have added Serial.print statements to every block of code now.

and?.... what do they say? Is the code going to the right places?

EDIT: take the motors off and put your volt meter across the pins on the 298- check that you have +- in one direction and -+ with the sketch going into reverse, and measure that you have enough volts.

It has been said here somewhere, that some motors require a higher voltage in one direction compared to the other.

On a side note, you are aware that the 298 eats volts?- as a minimum you lose 2 volts through there even at no load; that loss rises to 3-4V (from memory- check the data sheet) at higher current, so you need to throw lots more volts in that you need at the motor.