Arduino Forum

Using Arduino => Programming Questions => Topic started by: daniyald700 on Apr 17, 2019, 11:43 pm

Title: a function-definition is not allowed here before '{' token
Post by: daniyald700 on Apr 17, 2019, 11:43 pm
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();


}
Title: Re: a function-definition is not allowed here before '{' token
Post by: UKHeliBob on Apr 17, 2019, 11:52 pm
Quote
how can I fix this?
Please start by posting your complete program and the full error message
Title: Re: a function-definition is not allowed here before '{' token
Post by: Montmorency on Apr 18, 2019, 12:03 am
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?

Title: Re: a function-definition is not allowed here before '{' token
Post by: PaulS on Apr 18, 2019, 12:07 pm
Fix your suck ass indenting, and it will become perfectly clear what the problem is.