Coding Motors forward/backward through L293D

Background: I’m creating a storage solution that will roll out from under a piece of furniture when a momentary button(turned into latch) is pushed. Using Ultrasonic sensors the distance measured by the first sensor(one on either side of the storage sensor) will dictate whether the motors run forward or backward - ie if the distance is <200cm the motors will run forward. The motors are about 12VDC each to move a total of 5kg on carpet.
Issue: the problem is I’m relatively new to coding and robotics thus I’m not sure what coding I’d need to change the direction of the motor and how to wire it up to the L293D dual H-Bridge DC motor control that I’m using.
The current code, I have developed from a combination of other people’s forum is below, if you spot any problems with it currently please post about them - all advice is greatly appreciated.

// FIND LIBRARY RELEVANT TO MY SHIELD// you need this so you can use the adafruit motor shield library:
#include <AFMotor.h>
// this created our first motor (called "motor") on port 3 of the motorshield:
AF_DCMotor motor(1, MOTOR12_64KHZ);
// we do it again, this time our second motor will be called "motor2" on port
// 4 of the motorshield:
AF_DCMotor motor2(4, MOTOR12_64KHZ);
// so now we have 2 motors called "motor" and "motor2"
/* not sure if above is correct and what other coding is needed to make motors run*/

#define trigPin 12
#define echoPin 13
#define trigPin2  15
#define echoPin2  16
#define inputPin 14 //momentary switch
int pushButton = 0; //This is the default "Button Off" and 1 is "Button On"
int OldButton = 0; 
const int Motor1Pin1 = 8;
const int Motor1Pin2 = 9;
const int Motor2Pin1 = 10;
const int Motor2Pin2 = 11;
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

Ultrasonic ultrasonic1(trig1, echo1);
Ultrasonic ultrasonic2(trig2, echo2); //not sure if need these lines

  void setup() 

  {   pinMode(trigPin, OUTPUT);
      pinMode(echoPin, INPUT);
      pinMode(trigPin2, OUTPUT);
      pinMode(echoPin2, INPUT);
      pinMode(Motor1Pin1, OUTPUT);   
      pinMode(Motor1Pin2, OUTPUT);   
      pinMode(Motor2Pin1, OUTPUT);   
      pinMode(Motor2Pin2, OUTPUT);
      // set the speed to 200 of 255 of "motor".  Note that 0 is stop,
      // 255 is full speed:
      // example above    
      // set the speed to 200 of 255 of "motor2":
      // example above 

    void loop() 
{ A_label:
     if(digitalRead(pushButton) == HIGH) 
          { pushButton = 1 - pushButton; /* Button is set as 0 then Button = 1 - 0 (=1) (on) 
                             and if Button is 1 then Button=1 - 1 (=0) (off) */
           delay(2000);        //Delay for 2 seconds
           if(pushButton == 1 && OldButton == 0)  /*If Button is 1 and OldButton is 
                      0 (This means basically means "If the button was just pressed 
                      "pushButton" is on" */
                   {OldButton = Button; //The data is now old
                    void MeasureDistance1();
                            if (distance < 200) 
                            { GoForward();
                            do {void MeasureDistance2() } 
                                while (distance > 200); 
                                stop motor;
                                goto A_label;
                            } else 
                                {if(digitalRead(microswitch) == HIGH
                                 do {void MeasureDistance1() } 
                                 while (distance < 200); 
                                 stop motor;
                                 goto A_label; 
                                    {goto A_label;
                   } else 
                      {goto A_label;}
              else (Button == 0 && OldButton == 1) 
              { OldButton = Button; //The data is now old
            }else {delay(4000);

void MeasureDistance1()  // furthest out of furniture
  long duration1, distance1; 
  digitalWrite(trigPin, LOW); 
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);  
  duration1 = pulseIn(echoPin, HIGH);
  distance1 = duration1 / 58; // distance in cm

void MeasureDistance2()  //furthest Ultrasonic sensor under furniture
  long duration2, distance2;
  digitalWrite(trigPin2, LOW); 
  digitalWrite(trigPin2, HIGH);
  digitalWrite(trigPin2, LOW);  
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = duration2 / 58;  // distance in cm

// not sure how to make motors - 2 both run forwrd and both run back
void GoForward()
 digitalWrite(Motor1Pin2, LOW);
 digitalWrite(Motor1Pin1, HIGH);
 digitalWrite(Motor2Pin2, LOW);
 digitalWrite(Motor2Pin1, HIGH);

void GoBackward()
 digitalWrite(Motor1Pin1, LOW);
 digitalWrite(Motor1Pin2, HIGH);
 digitalWrite(Motor2Pin1, LOW);
 digitalWrite(Motor2Pin2, HIGH);

// not sure if needed below;      // turn it on going forward;  // motor 2 goes forward as well
 delay(1000);;     // the other way;  //again for motor 2
 delay(1000);;      // stopped;  // command motor 2 to stop

// below is phase coding found from another forum
  digitalWrite(enablePin, HIGH);
  digitalWrite(phasePin, HIGH);
  digitalWrite(enablePin, LOW);
  digitalWrite(phasePin, LOW);
  digitalWrite(enablePin, HIGH);
  digitalWrite(phasePin, LOW);
  digitalWrite(enablePin, LOW);
  digitalWrite(phasePin, LOW);

Why don’t you start by telling us if it compiles and loads and then telling us what you want it to do that it isn’t doing?

And exactly what motors and power source you’re using plus a diagram showing how it’s all connected would be useful.

BTW L293D drivers will not handle very much power. Check that your motors can manage on only 600mA.


khoy91: The motors are about 12VDC each to move a total of 5kg on carpet.

That's not nearly enough information to determine if the basic mechanics are plausible.

Motors have a speed and torque rating - it you are propelling something linearly there must be a mechanism converting rotation to linear motion - what is it? Gear ratios? Shaft/wheel diameter? etc etc.

Motors also have a stall current and its important to ensure this is not more than the motor driver can handle without frying, or you'll fry it.