Why won't this code work?

I'm writing a code that will make a robot that I've built, follow the left wall of a maze to complete it. When I try to compile it however, it doesn't let me. Can someone please let me know what I'm missing? I'm quite new to code and especially C++ so I'm not familiar with requirements.

#include <Ultrasonic.h>

// Define pins for motors
const int motorLeftForward = 8;
const int motorLeftBackward = 9;
const int motorRightBackward = 6;
const int motorRightForward = 7;

// Define pins for ultrasonic sensors
const int trigPinFront = 10;
const int echoPinFront = 11;
const int trigPinLeft = 12;
const int echoPinLeft = 13;

// Create Ultrasonic objects
Ultrasonic ultrasonicFront(trigPinFront, echoPinFront);
Ultrasonic ultrasonicLeft(trigPinLeft, echoPinLeft);

// Define threshold distances (in cm)
const int frontThreshold = 25;
const int leftThreshold = 25;

//Coordinate and position variables
int ycord = 1;
int xcord = 1;
int direction = 1;

void setup() {
  // Initialize motor pins
  pinMode(motorLeftForward, OUTPUT);
  pinMode(motorLeftBackward, OUTPUT);
  pinMode(motorRightForward, OUTPUT);
  pinMode(motorRightBackward, OUTPUT);

  // Initialize serial communication
  Serial.begin(112500);
}

void loop() {
  while (xcord < 4 && ycord < 4) {

    // Read distances from ultrasonic sensors
    int distanceFront = ultrasonicFront.read();
    int distanceLeft = ultrasonicLeft.read();

    // Decision making based on sensor readings
    if (distanceLeft > leftThreshold) {
      turnLeft();
      direction - 1;
    } else if (distanceFront < frontThreshold) {
      turnRight();
      direction + 1;
    } else {
      // Otherwise, move forward
      moveForward();
      if (direction = 1) {
        ycord + 1
      } else if (direction = 2) {
        xcord + 1;
      } else if (direction = 3) {
        ycord - 1;
      } else if (direction = 4) {
        xcord - 1;
      }

      delay(100);  // Small delay to avoid rapid changes
    }
    if (direction > 4) {
      direction = 1;
    }
    if (direction < 1) {
      direction = 4;
    }
  }
 stop();
  void moveForward() {
    digitalWrite(motorLeftForward, HIGH);
    digitalWrite(motorLeftBackward, LOW);
    digitalWrite(motorRightForward, HIGH);
    digitalWrite(motorRightBackward, LOW);
    delay(400);
  }

  void turnLeft() {
    digitalWrite(motorLeftForward, LOW);
    digitalWrite(motorLeftBackward, HIGH);
    digitalWrite(motorRightForward, HIGH);
    digitalWrite(motorRightBackward, LOW);
    delay(195);  // Adjust delay for a proper turn
  }

  void turnRight() {
    digitalWrite(motorLeftForward, HIGH);
    digitalWrite(motorLeftBackward, LOW);
    digitalWrite(motorRightForward, LOW);
    digitalWrite(motorRightBackward, HIGH);
    delay(195);  // Adjust delay for a proper turn
  }
  void stop() {
    digitalWrite(motorLeftForward, LOW);
    digitalWrite(motorLeftBackward, LOW);
    digitalWrite(motorRightForward, LOW);
    digitalWrite(motorRightBackward, LOW);
  }

The error messages are:

C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino: In function 'void loop()':
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:48:7: error: 'turnLeft' was not declared in this scope
       turnLeft();
       ^~~~~~~~
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:48:7: note: suggested alternative: 'trigPinLeft'
       turnLeft();
       ^~~~~~~~
       trigPinLeft
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:51:7: error: 'turnRight' was not declared in this scope
       turnRight();
       ^~~~~~~~~
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:55:7: error: 'moveForward' was not declared in this scope
       moveForward();
       ^~~~~~~~~~~
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:55:7: note: suggested alternative: 'motorLeftForward'
       moveForward();
       ^~~~~~~~~~~
       motorLeftForward
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:58:7: error: expected ';' before '}' token
       } else if (direction = 2) {
       ^
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:75:2: error: 'stop' was not declared in this scope
  stop();
  ^~~~
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:75:2: note: suggested alternative: 'loop'
  stop();
  ^~~~
  loop
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:76:22: error: a function-definition is not allowed here before '{' token
   void moveForward() {
                      ^
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:84:19: error: a function-definition is not allowed here before '{' token
   void turnLeft() {
                   ^
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:92:20: error: a function-definition is not allowed here before '{' token
   void turnRight() {
                    ^
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:99:15: error: a function-definition is not allowed here before '{' token
   void stop() {
               ^
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:104:3: error: expected '}' at end of input
   }
   ^

exit status 1

Compilation error: 'turnLeft' was not declared in this scope

Thanks for using code tags to post the code! Now, please do the same for the error messages, so that forum members have some idea what you are asking about.

It's got all my file destinations so I'm reluctant, is there a way I can upload it without including those?

For help on this forum, you must provide the basic information required to determine what the problem is. That could be a shorter, separate program that demonstrates the problem

Please read and follow the directions in the "How to get the best out of this forum" post, linked at the head of every forum category.

The error messages have been uploaded

Use "==" for comparisons

      direction - 1;
...
     direction + 1;

These do nothing. You may be thinking of decrementing, direction--; or incrementing, direction++;

1 Like

This is not syntactically correct

 stop()
  void moveForward() {

Try using the IDE Autoformat tool. It can help to find missing or mismatched { braces }, which can cause all kindsa errors, which may be misleading you.

a7

Here is the revised code after trying what you said:

#include <Ultrasonic.h>

// Define pins for motors
const int motorLeftForward = 8;
const int motorLeftBackward = 9;
const int motorRightBackward = 6;
const int motorRightForward = 7;

// Define pins for ultrasonic sensors
const int trigPinFront = 10;
const int echoPinFront = 11;
const int trigPinLeft = 12;
const int echoPinLeft = 13;

// Create Ultrasonic objects
Ultrasonic ultrasonicFront(trigPinFront, echoPinFront);
Ultrasonic ultrasonicLeft(trigPinLeft, echoPinLeft);

// Define threshold distances (in cm)
const int frontThreshold = 25;
const int leftThreshold = 25;

//Coordinate and position variables
int ycord = 1;
int xcord = 1;
int direction = 1;

void setup() {
  // Initialize motor pins
  pinMode(motorLeftForward, OUTPUT);
  pinMode(motorLeftBackward, OUTPUT);
  pinMode(motorRightForward, OUTPUT);
  pinMode(motorRightBackward, OUTPUT);

  // Initialize serial communication
  Serial.begin(112500);
}

void loop() {
  while (xcord < 4 && ycord < 4) {

    // Read distances from ultrasonic sensors
    int distanceFront = ultrasonicFront.read();
    int distanceLeft = ultrasonicLeft.read();

    // Decision making based on sensor readings
    if (distanceLeft > leftThreshold) {
      turnLeft();
      direction--1;
    } else if (distanceFront < frontThreshold) {
      turnRight();
      direction++1;
    } else {
      // Otherwise, move forward
      moveForward();
      if (direction == 1) {
        ycord++1;
      } else if (direction == 2) {
        xcord++1;
      } else if (direction == 3) {
        ycord--1;
      } else if (direction == 4) {
        xcord--1;
      }

      delay(100);  // Small delay to avoid rapid changes
    }
    if (direction > 4) {
      direction = 1;
    }
    if (direction < 1) {
      direction = 4;
    }
  }

  stop();
  

  void moveForward() {
    digitalWrite(motorLeftForward, HIGH);
    digitalWrite(motorLeftBackward, LOW);
    digitalWrite(motorRightForward, HIGH);
    digitalWrite(motorRightBackward, LOW);
    delay(400);
  }

  void turnLeft() {
    digitalWrite(motorLeftForward, LOW);
    digitalWrite(motorLeftBackward, HIGH);
    digitalWrite(motorRightForward, HIGH);
    digitalWrite(motorRightBackward, LOW);
    delay(195);  // Adjust delay for a proper turn
  }

  void turnRight() {
    digitalWrite(motorLeftForward, HIGH);
    digitalWrite(motorLeftBackward, LOW);
    digitalWrite(motorRightForward, LOW);
    digitalWrite(motorRightBackward, HIGH);
    delay(195);  // Adjust delay for a proper turn
  }
  void stop() {
    digitalWrite(motorLeftForward, LOW);
    digitalWrite(motorLeftBackward, LOW);
    digitalWrite(motorRightForward, LOW);
    digitalWrite(motorRightBackward, LOW);
  }
}

Here are the errors:

C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino: In function 'void loop()':
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:48:7: error: 'turnLeft' was not declared in this scope
       turnLeft();
       ^~~~~~~~
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:48:7: note: suggested alternative: 'trigPinLeft'
       turnLeft();
       ^~~~~~~~
       trigPinLeft
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:49:18: error: expected ';' before numeric constant
       direction--1;
                  ^
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:51:7: error: 'turnRight' was not declared in this scope
       turnRight();
       ^~~~~~~~~
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:52:18: error: expected ';' before numeric constant
       direction++1;
                  ^
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:55:7: error: 'moveForward' was not declared in this scope
       moveForward();
       ^~~~~~~~~~~
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:55:7: note: suggested alternative: 'motorLeftForward'
       moveForward();
       ^~~~~~~~~~~
       motorLeftForward
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:57:16: error: expected ';' before numeric constant
         ycord++1;
                ^
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:59:16: error: expected ';' before numeric constant
         xcord++1;
                ^
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:61:16: error: expected ';' before numeric constant
         ycord--1;
                ^
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:63:16: error: expected ';' before numeric constant
         xcord--1;
                ^
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:76:3: error: 'stop' was not declared in this scope
   stop();
   ^~~~
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:76:3: note: suggested alternative: 'loop'
   stop();
   ^~~~
   loop
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:79:22: error: a function-definition is not allowed here before '{' token
   void moveForward() {
                      ^
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:87:19: error: a function-definition is not allowed here before '{' token
   void turnLeft() {
                   ^
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:95:20: error: a function-definition is not allowed here before '{' token
   void turnRight() {
                    ^
C:\Users\SPE0015\Documents\Micromouse Flood Fill\LeftHandWallAlgorithm\LeftHandWallAlgorithm.ino:102:15: error: a function-definition is not allowed here before '{' token
   void stop() {
               ^

exit status 1

Compilation error: 'turnLeft' was not declared in this scope

Missing brace(s). A function definition cannot appear inside another function. Did you not notice post #7?

I did notice it, it just said use IDE's autoformat tool to find missing or mismatched braces. I ran it and nothing happened so where would you suggest putting the braces that are needed?

Go through the loop function line by line and decide whether it is complete, should do what you want, and ends with a brace.

what it's meant to do is while it's not at the end coordinates, it should read the sensors, decide whether to turn right, left or go forward, and then increment relevant variables. After it has reached the end, it stops, hence stop().

Does the loop function begin and end with braces?

void loop()  {
...
}

yes

Look at the loop() function. it should end before any following functions. Where is its closing brace?

Hint: look at the indentation of loop(), and then look at the indentation of moveForward(). Their indentation should be the same. Are they?

1 Like

Hint: no, it does not.

I found that the brace ending the loop() was after the decleration of the other functions (stop, moveForward, turnRight and turnLeft). I moved the ending brace to before that (right after stop() ) and it compiled successfully, thanks for your help guys.

Great lesson learned ! Congratulations.

Sry if it was I who mislead you. I said

It can help to find missing or mismatched { braces }

I did not say it would find them, add them, take the away or make any syntactic changes.

It formats the code.

One reason to format code, about which the compiler does not care, is that it makes it easier to read.

@van_der_decken pointed out the appearance of a function at the wrong indentation level. This wouldn't happen if the syntax was correct…

So maybe I should always say formatting can help you see errors like missing or extra { braces }, I'll try to say that going forward.

The compiler or other automatic entity can't fix your code. You can make code that is syntactically correct but does not do what you want. Formatting hells here, too, as it can help you find see places where you've gathered up too many or too few lines in an if statement or something like that.

What really should happen sooner or later is that formatting lives in your elbows and when you write code, it comes out formatted.

Nearly q0 percent of my use of the tool is on noob code, and not because Om looking for errors, but because it makes code easier to read.

a7