Sketch operation changes when sound function is added

/*Final code for project submittal - due 10 May 2020 */
#include <AFMotor.h> //use with DC motors
#include <NewPing.h> //use with Ultra-sonic sensor only
#include <Servo.h> //

#define TRIG_PIN A0 //use with Ultra-sonic sensor only
#define ECHO_PIN A1 //use with Ultra-sonic sensor only
#define LED_RED A3
#define LED_BLU A4
#define LED_GRN A5
#define MAX_DISTANCE 200
#define MAX_SPEED 225 // sets speed of DC motors
#define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

AF_DCMotor motor1(1); //left back wheel
AF_DCMotor motor2(3); //right back wheel

Servo myservo;

boolean goesForward=false;
int distance = 100;
int speedSet = 0;
int buzzer = 9; //speaker connected to pin 9 and ground

void setup() {

myservo.attach(10);
myservo.write(115); //servo set to look forward
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
pinMode(LED_RED, OUTPUT);
pinMode(LED_BLU, OUTPUT);
pinMode(LED_GRN, OUTPUT);
}

void loop() {
int distanceR = 0;
int distanceL = 0;
//buzStop(); //audiable sound from speaker
//delay(40);

if(distance<=15) //avoidance detection set in cm
{
//loop algorithm
//if measured distance is less than or equal to 15 cm, LED (red) lights up, and audio sounds
//stop motors, reverse for 300 ms, stop motors
//look right and left and measure distance in each direction
//if distance in one direction is greater than another direction turn to the farthest distance
//right turns = LED (blue) and left turns = LED (grenn)

//moveStop();
//delay(100);
//buzStop(); //audiable sound from speaker
moveStop();
delay(100);
moveBackward();
delay(150);
//buzStop(); //audiable sound from speaker
//delay(150);
moveStop();
delay(200);
//buzStop(); //audiable sound from speaker
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);
//buzStop(); //audiable sound from speaker
if(distanceR>=distanceL)
{
turnLeft();
moveStop();
}else
{
turnRight();
moveStop();
}
}else
{
moveForward();
}
distance = readPing();
}

int lookRight()
{
myservo.write(10);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
}

int lookLeft()
{
myservo.write(180);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
delay(100);
}

int readPing() {
delay(70);
int cm = sonar.ping_cm();
if(cm==0)
{
cm = 250;
}
return cm;
}

void buzStop() {
tone(buzzer, 660);
delay(20);
noTone(buzzer);
delay(5);
tone(buzzer, 540);
delay(20);
noTone(buzzer);
delay(5);
//digitalWrite(LED_RED, HIGH);
//delay(25);
//digitalWrite(LED_RED, LOW);
//digitalWrite(LED_BLU, HIGH);
//delay(25);
//digitalWrite(LED_BLU, LOW);
//digitalWrite(LED_GRN, HIGH);
//delay(25);
//digitalWrite(LED_GRN, LOW);
}

void moveStop() {
motor1.run(RELEASE);
motor2.run(RELEASE);
}

void moveForward() {

if(!goesForward)
{
goesForward=true;
motor1.run(FORWARD);
motor2.run(FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
delay(5);
}
}
}

void moveBackward() {
goesForward=false;
motor1.run(BACKWARD);
motor2.run(BACKWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
delay(5);
}
}

void turnRight() {
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(2000);
motor1.run(FORWARD);
motor2.run(FORWARD);
//digitalWrite(LED_BLU, HIGH);
//delay(40);
//digitalWrite(LED_BLU, LOW);
}

void turnLeft() {
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(2000);
motor1.run(FORWARD);
motor2.run(FORWARD);
//digitalWrite(LED_GRN, HIGH);
//delay(40);
//digitalWrite(LED_GRN, LOW);
}

when I add my buzStop(); function to my code the operation changes, without these lines of code the program works great and my robot is capable of navigating my maze successfully. I would like to incorporate my sound and LEDs for my final project due next week Thursday.

project_code.ino (4.25 KB)

Welcome to the Forum. Please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question …
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The “Code: [Select]” feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing or review.

If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower right corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Go to the New Ping Wiki and search for "Timer 2 Conflict".

Timer 2 Conflict

Having a conflict with the tone library or another library using timer 2? Instead of the tone library, use my NewTone or toneAC libraries which instead uses timer 1 (and also has many other advantages). Or use my Timer Free Tone library which doesn't use any timers to generate tones.

Thanks for the advise, where can I find these files? Will I need to change my code?

Blues Eyes,

disregard my previous response, I'll use the link you provided in your reply to locate the files, thanks again.