Hello, I am working on the folowing code to run a obstacle avoidance robot ...

has a mini servo (SG90) hc-sr04 sensor, 8 leds, 595 8bit shift register, and buzzer, arduino uno, and AFMotor shield v.1. and 2/ 6volt DC motors.

when it encounters an obstacle the LEDs will light in sequence, the closer the robot gets and finally the buzzer- buzzes.

trying to figure out, I have pieced together a few codes.

errors ("C:\Users\Desktop\RO_ME2.4\RO_ME2.4\RO_ME2.4.ino: In function ‘void setup()’:

RO_ME2.4:69: error: ‘checkPath’ was not declared in this scope

checkPath(); // run the CheckPath routine to find the best path to begin travel

^

RO_ME2.4:72: error: ‘moveForward’ was not declared in this scope

moveForward(); // run function to make robot move forward

^

C:\Users\MARK LILES\Desktop\RO_ME2.4\RO_ME2.4\RO_ME2.4.ino: At global scope:

RO_ME2.4:77: error: expected initializer before ‘}’ token

void loop() }")

I am still trying to work it out.

I joined and did my best not to piss anyone off, and to do what is required in order to hopefully receive
a bit of guidance.

If I missed something, I am sorry. Thanks for ANY guidance.

Code:
<>

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

#define TRIG_PIN A4 // Pin A4 on the Motor Drive Shield soldered to the ultrasonic sensor
#define ECHO_PIN A5 // Pin A5 on the Motor Drive Shield soldered to the ultrasonic sensor
#define MAX_DISTANCE 200 // sets maximum use sensor measure distance to 200cm
#define MAX_SPEED 180 // sets speed of DC traction motors to 180/256 or about 70%.
#define MAX_SPEED_OFFSET 10 // this sets offset to allow for differences between the two DC traction motors
#define COLL_DIST 10 // sets distance at which robot stops and reverses to 10cm
#define TURN_DIST COLL_DIST+10 // sets distance at which robot veers away (not reverse) to 20cm (10+10)
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); // sets up sensor library

AF_DCMotor motor3(3, MOTOR12_1KHZ); // create motor #1 using M3 output on Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor motor4(4, MOTOR12_1KHZ); // create motor #2, using M4 output, set to 1kHz PWM frequency
Servo myservo; // control a servo

int tonePin = 4; //Tone - Red Jumper
int trigPin = 9; //Trig - violet Jumper
int echoPin = 10; //Echo - yellow Jumper
int clockPin = 11; //IC Pin 11 - white Jumper
int latchPin = 12; //IC Pin 12 - Blue Jumper
int dataPin = 13; //IC Pin 14 - Green Jumper

byte possible_patterns[9] = {
B00000000,
B00000001,
B00000011,
B00000111,
B00001111,
B00011111,
B00111111,
B01111111,
B11111111,
};
int proximity=0;
int duration;
int distance;

int pos = 0; // this sets up variables for use in the sketch
int maxDist = 0;
int maxAngle = 0;
int maxRight = 0;
int maxLeft = 0;
int maxFront = 0;
int course = 0;
int curDist = 0;
String motorSet = “”;
int speedSet = 0;

//- SETUP LOOP -
void setup() {

//Serial Port
Serial.begin (9600);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(tonePin, OUTPUT);
myservo.attach(9); // attaches the servo on pin 9 (SERVO_2 on the Motor Drive Shield to the servo object
myservo.write(90); // tells the servo to position at 90-degrees ie. facing forward.
delay(2000); // delay for two seconds
checkPath(); // run the CheckPath routine to find the best path to begin travel
motorSet = “FORWARD”; // set the director indicator variable to FORWARD
myservo.write(90); // make sure servo is still facing forward
moveForward(); // run function to make robot move forward
};

//-MAIN LOOP -
void loop() }

digitalWrite(latchPin, LOW);
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

/if (distance >= 45 || distance <= 0){
Serial.println(“Out of range”);
}
else {
Serial.print(distance);
Serial.println(" cm");
}
/

proximity=map(distance, 0, 45, 8, 0);
//Serial.println(proximity);

if (proximity <= 0){
proximity=0;
}
else if (proximity >= 3 && proximity <= 4){
tone(tonePin, 200000, 200);
}
else if (proximity >= 5 && proximity <= 6){
tone(tonePin,5000, 200);
}
else if (proximity >= 7 && proximity <= 8){
tone(tonePin, 1000, 200);
}
shiftOut(dataPin, clockPin, MSBFIRST, possible_patterns[proximity]);
digitalWrite(latchPin, HIGH);

delay(600);
noTone(tonePin);
}

checkForward(); // check that if the robot is supposed to be moving forward, that the drive motors are set to move forward - this is needed to overcome some issues with only using 4 AA NiMH batteries
checkPath(); // set ultrasonic sensor to scan for any possible obstacles
}
//—
void checkPath() {
int curLeft = 0;
int curFront = 0;
int curRight = 0;
int curDist = 0;
myservo.write(144); // set servo to face left 54-degrees from forward
delay(120); // wait 120milliseconds for servo to reach position
for(pos = 144; pos >= 36; pos-=18) // loop to sweep servo (& sensor) from 144-degrees left to 36-degrees right at 18-degree intervals.
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(90); // wait 90ms for servo to get to position
checkForward(); // check the robot is still moving forward
curDist = readPing(); // get the current distance to any object in front of sensor
if (curDist < COLL_DIST) { // if distance to object is less than collision distance
checkCourse(); // run the checkCourse function
break; // jump out of this loop
}
if (curDist < TURN_DIST) { // if current distance is less than the turn distance
changePath(); // run the changePath function
}
if (curDist > curDist) {maxAngle = pos;}
if (pos > 90 && curDist > curLeft) { curLeft = curDist;}
if (pos == 90 && curDist > curFront) {curFront = curDist;}
if (pos < 90 && curDist > curRight) {curRight = curDist;}
}
maxLeft = curLeft;
maxRight = curRight;
maxFront = curFront;
}
//—
void setCourse() { // set direction based on basic distance map, which direction turning right or left?
if (maxAngle < 90) {turnRight();}
if (maxAngle > 90) {turnLeft();}
maxLeft = 0;
maxRight = 0;
maxFront = 0;
}
//–
void checkCourse() { // move backwards, stop, find where empty path is.
moveBackward();
delay(500);
moveStop();
setCourse();
}
//–
void changePath() {
if (pos < 90) {veerLeft();} // if current pos of sensor is less than 90-degrees,object on right side veer left
if (pos > 90) {veerRight();} // if current pos of sensor is greater than 90-degrees, object on left side so veer right
}
//–

int readPing() { // read the ultrasonic sensor distance
delay(70);
unsigned int uS = sonar.ping();
int cm = uS/US_ROUNDTRIP_CM;
return cm;
}
//–
void checkForward() { if (motorSet==“FORWARD”) {motor3.run(FORWARD); motor4.run(FORWARD); } } // make sure motors are going forward
//-----
void checkBackward() { if (motorSet==“BACKWARD”) {motor4.run(BACKWARD); motor3.run(BACKWARD); } } // make sure motors are going backward
//-

//–
void moveStop() {motor3.run(RELEASE); motor4.run(RELEASE);} // stop motor.
//–
void moveForward() {
motorSet = “FORWARD”;
motor3.run(FORWARD); // turn it on going forward
motor4.run(FORWARD); // turn it on going forward
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed uploading down the batteries too quickly
{
motor3.setSpeed(speedSet+MAX_SPEED_OFFSET);
motor4.setSpeed(speedSet);
delay(5);
}
}
//--------
void moveBackward() {
motorSet = “BACKWARD”;
motor3.run(BACKWARD); // turn on going forward
motor4.run(BACKWARD); // turn on going forward
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up
{
motor3.setSpeed(speedSet+MAX_SPEED_OFFSET);
motor4.setSpeed(speedSet);
delay(5);
}
}
//
void turnRight() {
motorSet = “RIGHT”;
motor3.run(FORWARD); // turn motor 3 forward
motor4.run(BACKWARD); // turn motor 4 backward
delay(400); // run motors this way for 400ms
motorSet = “FORWARD”;
motor3.run(FORWARD); // set both motors back to forward
motor4.run(FORWARD);
}
//
void turnLeft() {
motorSet = “LEFT”;
motor3.run(BACKWARD); // turn motor 3 backward
motor4.run(FORWARD); // turn motor 4 forward
delay(400); // run motors this way for 400ms
motorSet = “FORWARD”;
motor3.run(FORWARD); // turn it on going forward
motor4.run(FORWARD); // turn it on going forward
}
//
void veerRight() {motor3.run(BACKWARD); delay(400); motor4.run(FORWARD);} // veering right? set right motor backwards for 400ms
//----
void veerLeft() {motor4.run(FORWARD); delay(400); motor3.run(BACKWARD);} // veering left? set left motor backwards for 400ms

</>

Format the code using CTRL + T and post your code using code tag.

Did some formatting of your code. Couple of ‘{’ were missed and corrected it. Code formatting is important to find out these kind of syntax errors.

Try to compile the below code, I havent compiled the code as I dont have those libraries and lazy to set it up.

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


#define TRIG_PIN A4 // Pin A4 on the Motor Drive Shield soldered to the ultrasonic sensor
#define ECHO_PIN A5 // Pin A5 on the Motor Drive Shield soldered to the ultrasonic sensor
#define MAX_DISTANCE 200 // sets maximum use sensor measure distance to 200cm
#define MAX_SPEED 180 // sets speed of DC traction motors to 180/256 or about 70%.
#define MAX_SPEED_OFFSET 10 // this sets offset to allow for differences between the two DC traction motors
#define COLL_DIST 10 // sets distance at which robot stops and reverses to 10cm
#define TURN_DIST COLL_DIST+10 // sets distance at which robot veers away (not reverse) to 20cm (10+10)
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); // sets up sensor library

AF_DCMotor motor3(3, MOTOR12_1KHZ); // create motor #1 using M3 output on Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor motor4(4, MOTOR12_1KHZ); // create motor #2, using M4 output, set to 1kHz PWM frequency
Servo myservo;  // control a servo

int tonePin = 4;    //Tone - Red Jumper
int trigPin = 9;    //Trig - violet Jumper
int echoPin = 10;   //Echo - yellow Jumper
int clockPin = 11;  //IC Pin 11 - white Jumper
int latchPin = 12;  //IC Pin 12 - Blue Jumper
int dataPin = 13;   //IC Pin 14 - Green Jumper

byte possible_patterns[9] = {
  B00000000,
  B00000001,
  B00000011,
  B00000111,
  B00001111,
  B00011111,
  B00111111,
  B01111111,
  B11111111,
};
int proximity = 0;
int duration;
int distance;


int pos = 0; // this sets up variables for use in the sketch
int maxDist = 0;
int maxAngle = 0;
int maxRight = 0;
int maxLeft = 0;
int maxFront = 0;
int course = 0;
int curDist = 0;
String motorSet = "";
int speedSet = 0;


//- SETUP LOOP -
void setup() {

  //Serial Port
  Serial.begin (9600);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(tonePin, OUTPUT);
  myservo.attach(9);  // attaches the servo on pin 9 (SERVO_2 on the Motor Drive Shield to the servo object
  myservo.write(90); // tells the servo to position at 90-degrees ie. facing forward.
  delay(2000); // delay for two seconds
  checkPath(); // run the CheckPath routine to find the best path to begin travel
  motorSet = "FORWARD"; // set the director indicator variable to FORWARD
  myservo.write(90); // make sure servo is still facing forward
  moveForward(); // run function to make robot move forward
};

//-MAIN LOOP -
void loop()
{
  digitalWrite(latchPin, LOW);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  
  /*if (distance >= 45 || distance <= 0){
    Serial.println("Out of range");
    }
    else {
    Serial.print(distance);
    Serial.println(" cm");
    }*/
  
  proximity = map(distance, 0, 45, 8, 0);
  //Serial.println(proximity);
  
  if (proximity <= 0) {
    proximity = 0;
  }
  else if (proximity >= 3 && proximity <= 4) {
    tone(tonePin, 200000, 200);
  }
  else if (proximity >= 5 && proximity <= 6) {
    tone(tonePin, 5000, 200);
  }
  else if (proximity >= 7 && proximity <= 8) {
    tone(tonePin, 1000, 200);
  }
  shiftOut(dataPin, clockPin, MSBFIRST, possible_patterns[proximity]);
  digitalWrite(latchPin, HIGH);
  
  
  delay(600);
  noTone(tonePin);

  checkForward(); // check that if the robot is supposed to be moving forward, that the drive motors are set to move forward - this is needed to overcome some issues with only using 4 AA NiMH batteries
  checkPath(); // set ultrasonic sensor to scan for any possible obstacles
}
//---
void checkPath() {
  int curLeft = 0;
  int curFront = 0;
  int curRight = 0;
  int curDist = 0;
  myservo.write(144); // set servo to face left 54-degrees from forward
  delay(120); // wait 120milliseconds for servo to reach position
  for (pos = 144; pos >= 36; pos -= 18)  // loop to sweep servo (& sensor) from 144-degrees left to 36-degrees right at 18-degree intervals.
  {
    myservo.write(pos);  // tell servo to go to position in variable 'pos'
    delay(90); // wait 90ms for servo to get to position
    checkForward(); // check the robot is still moving forward
    curDist = readPing(); // get the current distance to any object in front of sensor
    if (curDist < COLL_DIST) { // if distance to object is less than collision distance
      checkCourse(); // run the checkCourse function
      break; // jump out of this loop
    }
    if (curDist < TURN_DIST) { // if current distance is less than the turn distance
      changePath(); // run the changePath function
    }
    if (curDist > curDist) {
      maxAngle = pos;
    }
    if (pos > 90 && curDist > curLeft) {
      curLeft = curDist;
    }
    if (pos == 90 && curDist > curFront) {
      curFront = curDist;
    }
    if (pos < 90 && curDist > curRight) {
      curRight = curDist;
    }
  }
  maxLeft = curLeft;
  maxRight = curRight;
  maxFront = curFront;
}
//---
void setCourse() { // set direction based on basic distance map, which direction turning right or left?
  if (maxAngle < 90) {
    turnRight();
  }
  if (maxAngle > 90) {
    turnLeft();
  }
  maxLeft = 0;
  maxRight = 0;
  maxFront = 0;
}
//--
void checkCourse() { //  move backwards, stop, find where empty path is.
  moveBackward();
  delay(500);
  moveStop();
  setCourse();
}
//--
void changePath() {
  if (pos < 90) {
    veerLeft(); // if current pos of sensor is less than 90-degrees,object on right side veer left
  }
  if (pos > 90) {
    veerRight(); // if current pos of sensor is greater than 90-degrees, object on left side so veer right
  }
}
//--

int readPing() { // read the ultrasonic sensor distance
  delay(70);
  unsigned int uS = sonar.ping();
  int cm = uS / US_ROUNDTRIP_CM;
  return cm;
}
//--
void checkForward() {
  if (motorSet == "FORWARD") {
    motor3.run(FORWARD);  // make sure motors are going forward
    motor4.run(FORWARD);
  }
}
//-----
void checkBackward() {
  if (motorSet == "BACKWARD") {
    motor4.run(BACKWARD);  // make sure motors are going backward
    motor3.run(BACKWARD);
  }
}
//-


//--
void moveStop() {
  motor3.run(RELEASE);  // stop motor.
  motor4.run(RELEASE);
}
//--
void moveForward() {
  motorSet = "FORWARD";
  motor3.run(FORWARD);      // turn it on going forward
  motor4.run(FORWARD);      // turn it on going forward
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2) // slowly bring the speed uploading down the batteries too quickly
  {
    motor3.setSpeed(speedSet + MAX_SPEED_OFFSET);
    motor4.setSpeed(speedSet);
    delay(5);
  }
}
//--------
void moveBackward() {
  motorSet = "BACKWARD";
  motor3.run(BACKWARD);      // turn on going forward
  motor4.run(BACKWARD);     // turn on going forward
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2) // slowly bring the speed up
  {
    motor3.setSpeed(speedSet + MAX_SPEED_OFFSET);
    motor4.setSpeed(speedSet);
    delay(5);
  }
}
//
void turnRight() {
  motorSet = "RIGHT";
  motor3.run(FORWARD);      // turn motor 3 forward
  motor4.run(BACKWARD);     // turn motor 4 backward
  delay(400); // run motors this way for 400ms
  motorSet = "FORWARD";
  motor3.run(FORWARD);      // set both motors back to forward
  motor4.run(FORWARD);
}
//
void turnLeft() {
  motorSet = "LEFT";
  motor3.run(BACKWARD);     // turn motor 3 backward
  motor4.run(FORWARD);      // turn motor 4 forward
  delay(400); // run motors this way for 400ms
  motorSet = "FORWARD";
  motor3.run(FORWARD);      // turn it on going forward
  motor4.run(FORWARD);      // turn it on going forward
}
//
void veerRight() {
  motor3.run(BACKWARD);  // veering right? set right motor backwards for 400ms
  delay(400);
  motor4.run(FORWARD);
}
//----
void veerLeft() {
  motor4.run(FORWARD);  // veering left? set left motor backwards for 400ms
  delay(400);
  motor3.run(BACKWARD);
}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Did you write this code in stages?
That is;
Write code ONLY to get the hc-sr04 sensor working.
Write code ONLY to get the shift register/LEDs working.

Write code ONLY to get the motor shield and motors working.

Write code ONLY to get the buzzer and hc-sr04 working.

Write code ONLY to get the hc-sr04 and shift register/LEDs working.

Then when they are all working separately, combine them, ONE at a TIME, getting the combined code to work before adding the next stage?

That way you minimise compiler errors and keep a from of structure to your code.

Tom... :slight_smile:

Hi,

  delay(2000); // delay for two seconds
  checkPath(); // run the CheckPath routine to find the best path to begin travel
  motorSet = "FORWARD"; // set the director indicator variable to FORWARD
  myservo.write(90); // make sure servo is still facing forward
  moveForward(); // run function to make robot move forward
};

//-MAIN LOOP -
void loop()
{
  digitalWrite(latchPin, LOW);

The ; after the } will fix quite a few of your errors.

Tom..... :slight_smile:

Missed that error Tom :), was lazy in the morning