Driving two stepper motors simultaneously part 2

After buying the motors and getting into the programming to make them run together, I realized I should have been using brushed motors. I tried over and over to make the Stepper.h library work but couldn’t. I searched and found another sketch that would. So after combining it with an ultrasonic range finder sketch, I finally have something that is functional.

If anyone has any suggestions to streamline it, let me know. Otherwise it’s for anyone who wants to use it.
My next step is to make the parts for the robot; a three-wheeled version.

Here’s the code:

// This sketch for the Arduino is to drive
// two BYJ28-48 stepper motors using ULN2003 interface
// boards as a potential use on a small robot. An ultrasonic
// sensor is included for range finding. Note: the sensor uses
// independent conections for trigger and echo.

// For public domain. 

// Ultrasonic sensor pin connections.
const int pingoutPin = 2;
const int pinginPin = 3;

// Motor pin variables.
 int motorPin5 = 4;    
 int motorPin6 = 5;    
 int motorPin7 = 6;    
 int motorPin8 = 7;                        
 int motorPin1 = 8;    
 int motorPin2 = 9;    
 int motorPin3 = 10;    
 int motorPin4 = 11;
 
 int motorSpeed = 1200;  // Variable to set stepper speed.
           
 // Binary output for motor drive.
 int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
 
 void setup() {
  
   pinMode(motorPin1, OUTPUT);
   pinMode(motorPin2, OUTPUT);
   pinMode(motorPin3, OUTPUT);
   pinMode(motorPin4, OUTPUT);
   pinMode(motorPin5, OUTPUT);
   pinMode(motorPin6, OUTPUT);
   pinMode(motorPin7, OUTPUT);
   pinMode(motorPin8, OUTPUT);
   
   Serial.begin(9600);
 }
//////////////////////////////////////////////////////////////////////////////
 void loop(){
   
  // Establish variables for duration of the ultrasonic pulse, 
  // and the distance result in inches:
  long duration, inches;
  
  // The ultrasonic sensor is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingoutPin, OUTPUT);
  digitalWrite(pingoutPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingoutPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingoutPin, LOW);
  
  // Get the pulse return time.
  pinMode(pinginPin, INPUT);
  duration = pulseIn(pinginPin, HIGH);

  // Convert the time into distance.
  inches = microsecondsToInches(duration);
  
  // Check distance and turn if too close to object.
   if(inches <= 8)
     pivotright();
    else
       forward(); 
 }

 //Sound travels one inch in 73.746 microseconds.
 //Get distance from ultrasonic sensor to object by dividing by 2.
 long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

//////////////////////////////////////////////////////////////////////////////
 // Set pins to ULN2003 high in sequence from 1 to 4.
 // Delay "motorSpeed" between each pin setting (to determine speed):
 
 void pivotright() // Turn routine.
 // Stop motor 2, reverse motor one.
 {
   for(int i = 0; i < 8; i++) 
   {
     setOutput2(i);
     delayMicroseconds(motorSpeed);
   }
 }
 
void forward() // Forward routine.
 {
   for(int i = 7; i >= 0; i--)
   {
     setOutput(i);
     delayMicroseconds(motorSpeed);
   }
 }
 
void setOutput(int out) //Forward output.
 {
   // Motor one output.
   digitalWrite(motorPin1, bitRead(lookup[out], 0));
   digitalWrite(motorPin2, bitRead(lookup[out], 1));
   digitalWrite(motorPin3, bitRead(lookup[out], 2));
   digitalWrite(motorPin4, bitRead(lookup[out], 3));
   // Motor two output.
   digitalWrite(motorPin5, bitRead(lookup[out], 0));
   digitalWrite(motorPin6, bitRead(lookup[out], 1));
   digitalWrite(motorPin7, bitRead(lookup[out], 2));
   digitalWrite(motorPin8, bitRead(lookup[out], 3)); 
 }
 void setOutput2(int out) //Turn output.
 {
   digitalWrite(motorPin1, bitRead(lookup[out], 0));
   digitalWrite(motorPin2, bitRead(lookup[out], 1));
   digitalWrite(motorPin3, bitRead(lookup[out], 2));
   digitalWrite(motorPin4, bitRead(lookup[out], 3));
 }