Line Follow Obstical avoidance Code Issues

I'm fairly new to Arduino but have have a few projects. For the life of me i cant figure out this code for my line follow obstacle avoidance robot. Any help would be greatly appreciated. below is my code that im trying to make work but always get a bunch of errors.

#include <NewPing.h>
#include <Servo.h>
#include <AFMotor.h>

//hc-sr04 sensor
#define TRIGGER_PIN A2
#define ECHO_PIN A3
#define max_distance 50

//ir sensor
#define irLeft A0
#define irRight A1

//motor
#define MAX_SPEED 200
#define MAX_SPEED_OFFSET 20

Servo servo;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, max_distance);

AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);

int distance = 0;
int leftDistance;
int rightDistance;
boolean object;

void setup() {
Serial.begin(9600);
pinMode(irLeft, INPUT);
pinMode(irRight, INPUT);
servo.attach(10);
servo.write(90);

motor1.setSpeed(120);
motor2.setSpeed(120);
motor3.setSpeed(120);
motor4.setSpeed(120);
}

void loop() {
if (digitalRead(irLeft) == 0 && digitalRead(irRight) == 0 ) {
objectAvoid();
//forword
}
else if (digitalRead(irLeft) == 0 && digitalRead(irRight) == 1 ) {
objectAvoid();
Serial.println("TL");
//leftturn
moveLeft();
}
else if (digitalRead(irLeft) == 1 && digitalRead(irRight) == 0 ) {
objectAvoid();
Serial.println("TR");
//rightturn
moveRight();
}
else if (digitalRead(irLeft) == 1 && digitalRead(irRight) == 1 ) {
//Stop
Stop();
}
}

void objectAvoid() {
distance = getDistance();
if (distance <= 15) {
//stop
Stop();
Serial.println("Stop");

lookLeft();
lookRight();
delay(100);
if (rightDistance <= leftDistance) {
  //left
  object = true;
  turn();
  Serial.println("moveLeft");
} else {
  //right
  object = false;
  turn();
  Serial.println("moveRight");
}
delay(100);

}
else {
//forword
Serial.println("moveforword");
moveForward();
}
}

int getDistance() {
delay(50);
int cm = sonar.ping_cm();
if (cm == 0) {
cm = 100;
}
return cm;
}

int lookLeft () {
//lock left
servo.write(150);
delay(500);
leftDistance = getDistance();
delay(100);
servo.write(90);
Serial.print("Left:");
Serial.print(leftDistance);
return leftDistance;
delay(100);
}

int lookRight() {
//lock right
servo.write(30);
delay(500);
rightDistance = getDistance();
delay(100);
servo.write(90);
Serial.print(" ");
Serial.print("Right:");
Serial.println(rightDistance);
return rightDistance;
delay(100);
}
void Stop() {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void moveForward() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void moveBackward() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);

}
void turn() {
if (object == false) {
Serial.println("turn Right");
moveLeft();
delay(700);
moveForward();
delay(800);
moveRight();
delay(900);
if (digitalRead(irRight) == 1) {
loop();
} else {
moveForward();
}
}
else {
Serial.println("turn left");
moveRight();
delay(700);
moveForward();
delay(800);
moveLeft();
delay(900);
if (digitalRead(irLeft) == 1) {
loop();
} else {
moveForward();
}
}
}
void moveRight() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void moveLeft() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Please post the full error message copied from the IDE using the "Copy error message" button in the IDE and use code tags when you post it

The code compiles OK for me

Have you got the Adafruit motor shield library installed ?

post the errors

Hi, @jerlam01
Welcome to the forum.

If this is your code, have you developed it in stages?
If you did, then you should have codes for specifically.

-Ultrasonic unit.
-Servo motor
-AF motor shield.

You should get these parts working separately and prove your connections, before combining them one at a time, each time getting rid of any bugs as you go.

Can you tell us what you changed in your code to go from a compiling code to a compile error code?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

I do have Adafruit installed, along with Newping.

here is the error messages im getting.

C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino: In function 'void loop()':
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:39:14: error: a function-definition is not allowed here before '{' token
 void setup() {
              ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:52:13: error: a function-definition is not allowed here before '{' token
 void loop() {
             ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:75:20: error: a function-definition is not allowed here before '{' token
 void objectAvoid() {
                    ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:105:19: error: a function-definition is not allowed here before '{' token
 int getDistance() {
                   ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:114:17: error: a function-definition is not allowed here before '{' token
 int lookLeft () {
                 ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:127:17: error: a function-definition is not allowed here before '{' token
 int lookRight() {
                 ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:140:13: error: a function-definition is not allowed here before '{' token
 void Stop() {
             ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:146:20: error: a function-definition is not allowed here before '{' token
 void moveForward() {
                    ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:152:21: error: a function-definition is not allowed here before '{' token
 void moveBackward() {
                     ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:159:13: error: a function-definition is not allowed here before '{' token
 void turn() {
             ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:189:18: error: a function-definition is not allowed here before '{' token
 void moveRight() {
                  ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:195:17: error: a function-definition is not allowed here before '{' token
 void moveLeft() {
                 ^

`Preformatted text`exit status 1

Compilation error: a function-definition is not allowed here before '{' token

Errors below.


C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino: In function 'void loop()':
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:39:14: error: a function-definition is not allowed here before '{' token
 void setup() {
              ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:52:13: error: a function-definition is not allowed here before '{' token
 void loop() {
             ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:75:20: error: a function-definition is not allowed here before '{' token
 void objectAvoid() {
                    ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:105:19: error: a function-definition is not allowed here before '{' token
 int getDistance() {
                   ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:114:17: error: a function-definition is not allowed here before '{' token
 int lookLeft () {
                 ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:127:17: error: a function-definition is not allowed here before '{' token
 int lookRight() {
                 ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:140:13: error: a function-definition is not allowed here before '{' token
 void Stop() {
             ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:146:20: error: a function-definition is not allowed here before '{' token
 void moveForward() {
                    ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:152:21: error: a function-definition is not allowed here before '{' token
 void moveBackward() {
                     ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:159:13: error: a function-definition is not allowed here before '{' token
 void turn() {
             ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:189:18: error: a function-definition is not allowed here before '{' token
 void moveRight() {
                  ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:195:17: error: a function-definition is not allowed here before '{' token
 void moveLeft() {
                 ^

exit status 1

Compilation error: a function-definition is not allowed here before '{' token
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino: In function 'void loop()':
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:39:14: error: a function-definition is not allowed here before '{' token
 void setup() {
              ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:52:13: error: a function-definition is not allowed here before '{' token
 void loop() {
             ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:75:20: error: a function-definition is not allowed here before '{' token
 void objectAvoid() {
                    ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:105:19: error: a function-definition is not allowed here before '{' token
 int getDistance() {
                   ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:114:17: error: a function-definition is not allowed here before '{' token
 int lookLeft () {
                 ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:127:17: error: a function-definition is not allowed here before '{' token
 int lookRight() {
                 ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:140:13: error: a function-definition is not allowed here before '{' token
 void Stop() {
             ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:146:20: error: a function-definition is not allowed here before '{' token
 void moveForward() {
                    ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:152:21: error: a function-definition is not allowed here before '{' token
 void moveBackward() {
                     ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:159:13: error: a function-definition is not allowed here before '{' token
 void turn() {
             ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:189:18: error: a function-definition is not allowed here before '{' token
 void moveRight() {
                  ^
C:\Users\jerla\AppData\Local\Temp\.arduinoIDE-unsaved20221026-17404-zmbk4b.bdiy\sketch_nov26a\sketch_nov26a.ino:195:17: error: a function-definition is not allowed here before '{' token
 void moveLeft() {
                 ^

exit status 1

Compilation error: a function-definition is not allowed here before '{' token

Try this sketch

#include <NewPing.h>
#include <Servo.h>
#include <AFMotor.h>

//hc-sr04 sensor
#define TRIGGER_PIN A2
#define ECHO_PIN A3
#define max_distance 50

//ir sensor
#define irLeft A0
#define irRight A1

//motor
#define MAX_SPEED 200
#define MAX_SPEED_OFFSET 20

Servo servo;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, max_distance);

AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);

int distance = 0;
int leftDistance;
int rightDistance;
boolean object;

void setup()
{
  Serial.begin(9600);
  pinMode(irLeft, INPUT);
  pinMode(irRight, INPUT);
  servo.attach(10);
  servo.write(90);
  motor1.setSpeed(120);
  motor2.setSpeed(120);
  motor3.setSpeed(120);
  motor4.setSpeed(120);
}

void loop()
{
  if (digitalRead(irLeft) == 0 && digitalRead(irRight) == 0 )
  {
    objectAvoid();
    //forword
  }
  else if (digitalRead(irLeft) == 0 && digitalRead(irRight) == 1 )
  {
    objectAvoid();
    Serial.println("TL");
    //leftturn
    moveLeft();
  }
  else if (digitalRead(irLeft) == 1 && digitalRead(irRight) == 0 )
  {
    objectAvoid();
    Serial.println("TR");
    //rightturn
    moveRight();
  }
  else if (digitalRead(irLeft) == 1 && digitalRead(irRight) == 1 )
  {
    //Stop
    Stop();
  }
}

void objectAvoid()
{
  distance = getDistance();
  if (distance <= 15)
  {
    //stop
    Stop();
    Serial.println("Stop");
    lookLeft();
    lookRight();
    delay(100);
    if (rightDistance <= leftDistance)
    {
      //left
      object = true;
      turn();
      Serial.println("moveLeft");
    }
    else
    {
      //right
      object = false;
      turn();
      Serial.println("moveRight");
    }
    delay(100);
  }
  else
  {
    //forword
    Serial.println("moveforword");
    moveForward();
  }
}

int getDistance()
{
  delay(50);
  int cm = sonar.ping_cm();
  if (cm == 0)
  {
    cm = 100;
  }
  return cm;
}

int lookLeft ()
{
  //lock left
  servo.write(150);
  delay(500);
  leftDistance = getDistance();
  delay(100);
  servo.write(90);
  Serial.print("Left:");
  Serial.print(leftDistance);
  return leftDistance;
  delay(100);
}

int lookRight()
{
  //lock right
  servo.write(30);
  delay(500);
  rightDistance = getDistance();
  delay(100);
  servo.write(90);
  Serial.print(" ");
  Serial.print("Right:");
  Serial.println(rightDistance);
  return rightDistance;
  delay(100);
}
void Stop()
{
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}
void moveForward()
{
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}
void moveBackward()
{
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}
void turn()
{
  if (object == false)
  {
    Serial.println("turn Right");
    moveLeft();
    delay(700);
    moveForward();
    delay(800);
    moveRight();
    delay(900);
    if (digitalRead(irRight) == 1)
    {
      loop();
    }
    else
    {
      moveForward();
    }
  }
  else
  {
    Serial.println("turn left");
    moveRight();
    delay(700);
    moveForward();
    delay(800);
    moveLeft();
    delay(900);
    if (digitalRead(irLeft) == 1)
    {
      loop();
    }
    else
    {
      moveForward();
    }
  }
}
void moveRight()
{
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}
void moveLeft()
{
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}

THANKS!!! That compiled ok. Ill look through yours to see where i screwed up.

compiles fine for me as well

all/most of the errors seem to be the same "function-definition not allowed"

It was a straight copy of your code in your original post :grinning:

All I did was to Auto Format it in the IDE to improve its layout but I did not change the code itself