Is This Moter Driving Problem a Hardware or Software Issue?

Here is my Code:

#include <Servo.h> 

Servo servoLeft;
Servo servoRight;
#define trigPin 3
#define echoPin 4
#define forwardSpeed 300
#define turnSpeed 300
#define pauseTime 50
int lastSpdR, lastSpdL;

void setup() {
  delay(2000);
  servoRight.attach(11);
  servoLeft.attach(12);
  servoRight.writeMicroseconds(1500);
  servoLeft.writeMicroseconds(1500);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
}

void loop() {
  float duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0344;
  if (distance < 40) {
    if (random(1) == 0) {
      //left
      if (distance < 45) {
        servoRun(turnSpeed, -turnSpeed);
      } else {
        servoRun(forwardSpeed, forwardSpeed);
      }
    } else {
      //right
      if (distance < 45) {
        servoRun(-turnSpeed, turnSpeed);
      } else {
        servoRun(forwardSpeed, forwardSpeed);
      }
    }
  } else {
    //check for blockages(AGAIN)
    servoRun(turnSpeed, -turnSpeed);
    delay(100);
    delay(50);
    digitalWrite(trigPin, LOW); 
    delayMicroseconds(2);
    
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0344;
    if (distance < 30) {
      if (random(1) == 0) {
        //left
        if (distance < 35) {
           servoRun(turnSpeed, -turnSpeed);
        } else {
          servoRun(forwardSpeed, forwardSpeed);
        }
      } else {
        //right
        if (distance < 35) {
          servoRun(-turnSpeed, turnSpeed);
        } else {
          servoRun(forwardSpeed, forwardSpeed);
        }
      }
    }
    servoRun(-turnSpeed, turnSpeed);
    delay(2000);
    digitalWrite(trigPin, LOW); 
    delayMicroseconds(2);
    
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    if (distance < 30) {
      if (random(1) == 0) {
        //left
        if (distance < 35) {
           servoRun(turnSpeed, -turnSpeed);
        } else {
          servoRun(forwardSpeed, forwardSpeed);
        }
      } else {
        //right
        if (distance < 35) {
          servoRun(-turnSpeed, turnSpeed);
        } else {
          servoRun(forwardSpeed, forwardSpeed);
        }
      }
    }
    servoRun(turnSpeed, -turnSpeed);
    delay(1000);
    servoRun(forwardSpeed, forwardSpeed);
    delay(1000);
  }
}

void servoRun(int speedLeft, int speedRight) {
  speedRight = -speedRight;// invert for servos facing opposite directions
  servoLeft.writeMicroseconds(speedLeft + 1500);
  servoRight.writeMicroseconds(speedRight + 1500);
}

void stopServos() {
  servoRun(0, 0);
}

I am driving two servos on a Parallax Boe-Bot, not the Arduino board but the BS2 one with a rigged servo hookup. All my wires are hooked up correctly and pins 11 & 12 are hooked up to the servos. My problem is the right wheel will intermittently stop working. Pins 3 & 4 are hooked up to a ultrasonic distance sensor, so no concerns there.

I would like to know if there is a problem with my code. :blush:

  float duration, distance;

pulseIn() returns a long, containing the number of microseconds that the event of interest took to happen.

It is pointless to store that in a float.

Expecting the distance to be accurate to less than 1 centimeter resolution is unrealistic.

random(1) will return random numbers in the range 0 to 0. That is hardly random. RTFM.

Thank you, I think my problem may just be not enough power, but the info helps :wink: