Crazy Problem: Functions Correct When Connected and Incorrect When Disconnected

I have tried multiple programs and my ultrasound sensor WORKS when serial is online and DOES NOT when it is offline. :astonished: ::slight_smile:

ShortCircut:
I have tried multiple programs and my ultrasound sensor WORKS when serial is online and DOES NOT when it is offline. :astonished: ::slight_smile:

How are you powering the board when you unplug the USB?

ShortCircut:
I have tried multiple programs and my ultrasound sensor WORKS when serial is online and DOES NOT when it is offline.

What model of Arduino and what does your sketch look like?
Are you using an Arduino with a 32u4 processor like a Leonardo or Micro?

I Am using a Uno.
Here is my code:

#include <Servo.h> 

Servo servoLeft;
Servo servoRight;
#define psp 10

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

void setup() {
  delay(2000);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(psp, OUTPUT);
  Serial.begin(9600);
  
}

void loop() {
  long 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) {
    wall(distance);
  } else {
    //check for blockages(AGAIN)
    servoRun(turnSpeed, -turnSpeed);
    delay(200);
    stopServos();
    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) {
      wall(distance);
    }
    servoRun(-turnSpeed, turnSpeed);
    delay(400);
    stopServos();
    digitalWrite(trigPin, LOW); 
    delayMicroseconds(2);
    
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    if (distance < 30) {
      wall(distance);
    }
    servoRun(turnSpeed, -turnSpeed);
    delay(200);
    servoRun(forwardSpeed, forwardSpeed);
    delay(1000);
  }
}

void servoRun(int speedLeft, int speedRight) {
  speedRight = -speedRight;// invert for servos facing opposite directions
  /*while (lastSpdL != speedLeft && lastSpdR != speedRight) {
    if (lastSpdL < speedLeft) {
      lastSpdL += 10;
      servoLeft.writeMicroseconds(lastSpdL + 1500);
      delay(1);
    }
    if (lastSpdR < speedRight) {
      lastSpdR += 1;
      servoRight.writeMicroseconds(lastSpdR + 1500);
      delay(1);
    }
    if (lastSpdL > speedLeft) {
      lastSpdL -= 1;
      servoLeft.writeMicroseconds(lastSpdL + 1500);
      delay(1);
    }
    if (lastSpdR > speedRight) {
      lastSpdR -= 1;
      servoRight.writeMicroseconds(lastSpdR + 1500);
      delay(1);
    }
  }*/
  servoRight.attach(11);
  servoLeft.attach(12);
  digitalWrite(psp, HIGH);
  servoLeft.writeMicroseconds(speedLeft + 1500);
  servoRight.writeMicroseconds(speedRight + 1500);
}

void stopServos() {
  servoRun(0, 0);
  digitalWrite(psp, LOW);
}

void wall (int distance) {
  Serial.println("wall");
  Serial.println(distance);
  if (random(2) == 0) {
    //left
      servoRun(300, -300);
  } else {
    //right
      servoRun(-300, 300);
  }
}

I am using 5 AA rechargable batteries.

BUT

Here is my problem:
When I open the serial monitor, it detects walls and avoids them. When it is still connected to my PC but the serial monitor is not open, It hits the walls.

Does it help if you add this to your wall function?

if(Serial)
{
   Serial.println("wall");
   Serial.println(distance);
}

It's not clear from the sketch what the 'psp' pin does. Is it an on/off switch for the servo power?

    if (distance < 30) {
      wall(distance);
    }
    servoRun(turnSpeed, -turnSpeed);
    delay(200);

Since wall() sets the servo outputs and servoRun() sets the servo outputs a different way a few microseconds later the servos are not going to be doing much wall avoiding.

Here is what I think you might have been going for:

#include <Servo.h>

Servo LeftMotor;
Servo RightMotor;

const int PS_PIN = 10;
const int MAX_WALL_DISTANCE = 40;

const int TRIGGER_PIN = 3;
const int ECHO_PIN = 4;

const int FORWARD_SPEED = 300;
const int TURN_SPEED = 300;
const int LEFT_MOTOR_PIN = 12;
const int RIGHT_MOTOR_PIN = 11;

//#define pauseTime 50
//int lastSpdR, lastSpdL;

void setup() {
  delay(2000);
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(PS_PIN, OUTPUT);
  Serial.begin(9600);

  RightMotor.attach(11);
  LeftMotor.attach(12);
}

void loop() {
  static boolean approachingWall = false;
  static int turnDirection;

  int distance = GetDistance();

  if (distance < 40) {
    if (approachingWall == false) {
      approachingWall = true;
      // Just spotted a new wall.  Pick a turn direction
      turnDirection = random(2);
      Serial.print("wall distance: ");
      Serial.print(distance);
      Serial.print(" so turning ");
      Serial.println(turnDirection ? "right." : "left.");
    }
    if (turnDirection == 0)
      servoRun(TURN_SPEED, -TURN_SPEED);  //left
    else
      servoRun(-TURN_SPEED, TURN_SPEED);  //right
  }
  else { // No wall in sight
    approachingWall = false;
    // Switch to forward motion
    servoRun(FORWARD_SPEED, FORWARD_SPEED);
  }
}

void servoRun(int speedLeft, int speedRight) {
  speedRight = -speedRight;// invert for servos facing opposite directions

  digitalWrite(PS_PIN, HIGH);
  LeftMotor.writeMicroseconds(speedLeft + 1500);
  RightMotor.writeMicroseconds(speedRight + 1500);
}

void stopServos() {
  servoRun(0, 0);
  digitalWrite(PS_PIN, LOW);
}

int GetDistance() {
  unsigned long duration;

  digitalWrite(TRIGGER_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);

  duration = pulseIn(ECHO_PIN, HIGH, MAX_WALL_DISTANCE * 58);
  if (duration == 0)
    return MAX_WALL_DISTANCE + 1; // Dummy value for Too Far To Sense
  else
    return duration / 58;
}

How are your 5 AAs wired up to everything else? What pins?