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.
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.
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.
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
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?
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().
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.
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.