Go Down

Topic: a function-definition is not allowed here before '{' token (Read 70 times) previous topic - next topic

daniyald700

Hello, I've written this piece of code, yet I get a "a function-definition is not allowed here before '{' token" error, how can I fix this?

Code: [Select]
void loop()

{
 
  int threshold = analogRead(threshold_pot);
  int distance  = ping();  //call the ping function to get the distance in front of the robot
//  int distance = 17;
  Serial.print("Thresh:");
  Serial.print(threshold);
  Serial.print("(");
  Serial.print(threshold/2);
  Serial.print("), L=");
  SensorLeft = analogRead(LeftLightSensor);  //read the left light sensor
  Serial.print(SensorLeft);
  Serial.print(", R=");

  SensorRight = analogRead(RightLightSensor); //read the right light sensor
  Serial.print(SensorRight);
  Serial.print(", Diff=");

  SensorDifference = abs(SensorLeft - SensorRight);  //Calcuate the difference between
                                                     // left and right light sensors
  Serial.print(SensorDifference);

  Serial.print(", Obstacle:");
  Serial.print(distance);

  Serial.print("cm, Move:");

  if (distance > collisionThresh) //if path is clear, the robot can use the light sensors to navigate towards the light
  {
  // Check the conditions for moving forward
  if ( SensorLeft       <= threshold     &&
       SensorRight      <= threshold     &&
       SensorLeft       >= threshold/2   &&
       SensorRight      >= threshold/2   &&
       SensorDifference <= closeness)
       { 
          Serial.print("Forward");
          leftMotor.write(LForward);
          rightMotor.write(RForward);
          digitalWrite(LeftLEDIndicator, HIGH);
          digitalWrite(RightLEDIndicator, HIGH);
          delay(300); 
        }

  //This condition indicates that the robot has reached the light source,
  //so it will stop
  else if ( SensorLeft       >= threshold &&
            SensorRight      >= threshold &&
            SensorDifference <= closeness)
       { 
          Serial.print("STOP");
          leftMotor.write(LNeutral);
          rightMotor.write(RNeutral);
          digitalWrite(RightLEDIndicator, LOW);
          digitalWrite(LeftLEDIndicator, LOW);
          delay(500);
        }
       
  // If the left light sensor value is smaller than that of the right sensor,
  // and the difference between the values is large, then
  // the light source is towards the left, so robot should turn left.
  else if ( SensorLeft       <  SensorRight &&       
            SensorRight       >= threshold/2 )
            {
              Serial.print("Right");
              leftMotor.write(LForward);
              rightMotor.write(RBackward);
              digitalWrite(RightLEDIndicator, LOW);
              digitalWrite(LeftLEDIndicator, HIGH);
              delay(80);
            }

  // If the right light sensor value is smaller than that of the left sensor,
  // and the difference between the values is large, then
  // the light source is towards the right, so robot should turn right.
  else if ( SensorLeft       >  SensorRight  &&       
            SensorLeft       >= threshold/2 )
            {
                Serial.print("Left");
                leftMotor.write(LBackward);
                rightMotor.write(RForward);
                digitalWrite(RightLEDIndicator, HIGH);
                digitalWrite(LeftLEDIndicator, LOW);
                delay(80);
            }

  //This condition indicates that both sensor of the robot are heading away from,
  //the light, so it should turn around
  else if ( SensorLeft  < threshold/2 &&
            SensorRight < threshold/2)
            {               
                Serial.print("LOST");
                leftMotor.write(LBackwardHalf);
                rightMotor.write(RForwardHalf);
                delay(300);
                digitalWrite(RightLEDIndicator, LOW);
                digitalWrite(LeftLEDIndicator, LOW);
            }
  }
  else    // The path is blocked, so the robot will use the distance sensor to find a clear path
  {
      Serial.print(" blocked ");
      leftMotor.write(LNeutral);   // Stop the left motor
      rightMotor.write(RNeutral);  // Stop the right motor
      panMotor.write(0);           // Turn the distance sensor to the left
      delay(400);                  // Wait for the sensor to move into position
      leftDistance = ping();       // scan to the right
      delay(400);                  // Wait for the sensor to move into position
      panMotor.write(180);         // Turn the distance sensor to the right
      delay(600);                  // Wait for the sensor to move into position
      rightDistance = ping();      // Scan to the left
      delay(400);                  // Wait for the sensor to move into position
      panMotor.write(90);          // Return to center
      delay(400);                  // Wait for the sensor to move into position
      compareDistance();
  }
 
  Serial.println();


}

UKHeliBob

Quote
how can I fix this?
Please start by posting your complete program and the full error message
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Montmorency

I've written this piece of code, yet I get a "a function-definition is not allowed here before '{' token" error, how can I fix this?
Which specific line in the code you posted is giving you that error? Why haven't you provided this information in your question?


PaulS

Fix your suck ass indenting, and it will become perfectly clear what the problem is.
The art of getting good answers lies in asking good questions.

Go Up