GETTING ERROR: a function-definition is not allowed here before '{' token

Hi! I'm working on the project for university and I'm getting this error no matter how many times I check for missing brackets { } / [ ].. I get the a function-definition is not allowed here before '{' token in line where it says :
*// if (scanStopDistances[scanPosition] > longestDistance) {

  • void setup() { //
const int myServoPin= 12;

const int leftForwardPin=10;
const int leftReversePin=11;
const int rightForwardPin=8;
const int rightReversePin=9;

const int triggerPin=7;
const int echoPin=6;

const int minimumDistance=400;

int runningScanPositions[3] = {83 ,90, 97};

int stoppedScanPositions[13] = {30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150};
int scanPosition;

int scanRunDistances[3];
int scanStopDistances[13];

int okToDrive = 0; //=stop if =1 no obstacle detected

#include "Servo.h"

Servo myServo;

void forwardRight() {
Serial.println("left motor forwards, turn right");
digitalWrite(leftForwardPin,HIGH);
digitalWrite(leftReversePin,LOW);
digitalWrite(rightForwardPin,LOW);
digitalWrite(rightReversePin,LOW);
}

void reverseRight() {
Serial.println("Reverse left motor, reverse right");
digitalWrite(leftForwardPin,LOW);
digitalWrite(leftReversePin,HIGH);
digitalWrite(rightForwardPin,LOW);
digitalWrite(rightReversePin,LOW);
}

void forwardLeft() {
Serial.println("right motor forwards, turn left");
digitalWrite(leftForwardPin,LOW);
digitalWrite(leftReversePin,LOW);
digitalWrite(rightForwardPin,HIGH);
digitalWrite(rightReversePin,LOW);
}

void reverseLeft() {
Serial.println("reverse right motor, reverse left");
digitalWrite(leftForwardPin,LOW);
digitalWrite(leftReversePin,LOW);
digitalWrite(rightForwardPin,LOW);
digitalWrite(rightReversePin,HIGH);
}

void stopMotors() {
Serial.println("Stop motors");
digitalWrite(leftForwardPin,LOW);
digitalWrite(leftReversePin,LOW);
digitalWrite(rightForwardPin,LOW);
digitalWrite(rightReversePin,LOW);
}

void driveForward() {
Serial.println("Both motors forwards");
digitalWrite(leftForwardPin,HIGH);
digitalWrite(leftReversePin,LOW);
digitalWrite(rightForwardPin,HIGH);
digitalWrite(rightReversePin,LOW);
}

void driveReverse() {
Serial.println("Both motors reverse");
digitalWrite(leftForwardPin,LOW);
digitalWrite(leftReversePin,HIGH);
digitalWrite(rightForwardPin,LOW);
digitalWrite(rightReversePin,HIGH);
}

int getDistanceHCSSR04(int funcTriggerPin, int funcEchoPin) {
unsigned long duration;
int myDistance;
digitalWrite(funcTriggerPin,LOW);
delayMicroseconds(2);

digitalWrite(funcTriggerPin,HIGH);
delayMicroseconds(10);

digitalWrite(funcTriggerPin,LOW);

duration = pulseIn(funcEchoPin, HIGH);

myDistance= duration * 0.34 /2;
return myDistance;
}

void findStoppedBestDirection() {
Serial.println("Doing Full Scan");
int steerDelay = 10;
int bestDirection=0;
int longestDistance=0;

for (scanPosition = 0; scanPosition < 13; scanPosition++) {
myServo.write(stoppedScanPositions[scanPosition]);
delay(250);
scanStopDistances[scanPosition]= getDistanceHCSSR04(triggerPin, echoPin);

if(scanStopDistances[scanPosition]> 5000) {
scanStopDistances[scanPosition] = 0;
}

Serial.println(scanPosition);
Serial.println(scanStopDistances[scanPosition]);
}


if (scanStopDistances[scanPosition] > longestDistance) {

void setup() { 
Serial.begin(9600);
Serial.println("RobotCarSingleSensorv1....");
Serial.println(" ");

Serial.println("L298N Pins set");
pinMode(leftForwardPin, OUTPUT);
pinMode(leftReversePin, OUTPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(rightReversePin, OUTPUT);

Serial.println("HC-SR04 Ultrasonic Sensor pins set");
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);

myServo.attach(myServoPin);
Serial.println("Testing Servo");
myServo.write(30);
delay(500);
myServo.write(150);
delay(500);
myServo.write(90);
delay(500);
Serial.println("Servo Ready");
}
}
}

void loop() {
okToDrive =1; //set to drive
for (scanPosition=0; scanPosition < 3; scanPosition++) {
myServo.write(runningScanPositions[scanPosition]);
delay(100);
scanRunDistances[scanPosition]=getDistanceHCSSR04(triggerPin, echoPin);
Serial.println(scanRunDistances[scanPosition]);
if (scanRunDistances[scanPosition] < minimumDistance) {
okToDrive = 0;
Serial.println("Normal path blocked");
}
}

if (okToDrive < 1) {
stopMotors();
findStoppedBestDirection();
}
else {
driveForward();
}
}

You have no indentation in your code.
Indentation does not influence the code, but helps you to keep track of matching {}.
In the IDE you can hit control-t, and the IDE will add indentations for you.
Then copy and paste your code again.
Most of the times your error is from a mismatch of {}.
It could also be that you tried to define a function inside another function (which is not allowed).

After

if (scanStopDistances[scanPosition  blablabla {

There should at least be:

   } // close the if
} // close the definition of your function 

Before:

setup() {
   Blabla bla

Try counting braces again, this doesn't look good:

if (scanStopDistances[scanPosition] > longestDistance) {

void setup() { 

Common compiler errors caused by mismatched brackets:

"does not name a type" or
"expected declaration before" or
"expected unqualified-id before" or
"expected initializer before" or
"expected constructor, destructor, or type conversion before '(' token"
Usually means you forgot a '{' or put in an extra '}' in the previous function. Since all of the open brackets have been closed, the compiler is looking for further global declarations (variables or functions). If it finds something that looks like executable code instead of a global declaration it emits an error. Make sure that the brackets in the preceding function are in matching pairs '{' followed by '}'.

"a function-definition is not allowed here before '{' token"
(can cause: "'functionName' was not declared in this scope")
Usually means you forgot a '}' or put in an extra '{' in the previous function. Since a set of brackets has not been closed yet the compiler is looking for more code to put in the function. You can't declare a function inside a function so if the compiler finds a function declaration it emits an error. Make sure that the brackets in the preceding function are in matching pairs '{' followed by '}'.

"expected '}' at end of input"
Usually means you forgot a '}' or put in an extra '{' in the last function in the sketch. Since a set of brackets has not been closed yet, the compiler is looking for more code to put in the function. When it hits the end of the file instead, it emits an error. Make sure that the brackets in the last function are in matching pairs '{' followed by '}'.

"expected primary-expression before '}' token"
Usually means you have an incomplete statement before a '}'. The block statement (between '{' and matching '}') can only contain complete statements. Complete statements always end with ';' (or '}' for a block statement).

turns out there was an extra '}' thank you!

didn't know about ctrl+t, thank you for the info, I will try it! hopefully it fixes the problem

It dies not fix problems. But it helps you to keep an overview on pieces that belong together and on matching {}.

Did you know that clicking immediately to the right of any curly brace will usually show you the matching curly brace (provided there is one) by drawing a box around the match? It's not immediately obvious, is why I ask. I find it heaps helpful.