Error in a function-definition is not allowed here before '{' token

hi there is a simple code of arduino C language ,and i am not able to resolve this issue ,i have this error in last 7 line of the code which is the line no:307 ,please tell me how can i resolve this issue

  long duration, distance;
  String dist;
  digitalWrite(trigPin, LOW);  // Added this line
  delay(50); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delay(50); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance < 1) {  // This is where the LED On/Off happens
    Serial.print(distance);



    //for 2 proximity


    long secondduration, seconddistance;
  String seconddist;
  digitalWrite(trig1Pin, LOW);  // Added this line
  delay(50); // Added this line
  digitalWrite(trig1Pin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delay(50); // Added this line
  digitalWrite(trig1Pin, LOW);
  secondduration = pulseIn(echo1Pin, HIGH);
  seconddistance = (secondduration/2) / 29.1;
  if (seconddistance < 1) 
  {  // This is where the LED On/Off happens
    Serial.print(seconddistance);
    
}
  seconddist = String(seconddistance);
  if (seconddistance >= 50 || seconddistance <= 0)
  {
//    Serial.println("Out of range");
    seconddist = "Out of range";
  }
  else {
    Serial.print(seconddistance);
    Serial.println(" cm");
  }
  delay(1000);
 // HC12.print(String(distance) + "\n"); 

//htjghjghdjdtgje
//for 2 prximity


  seconddist = String(seconddistance);
  if (seconddistance >= 50 || seconddistance <= 0)
  {
//    Serial.println("Out of range");
    seconddist = "Out of range";
  }
  else {
    Serial.print(seconddistance);
    Serial.println(" cm");
  }
  delay(1000);
 // HC12.print(String(distance) + "\n"); 

 
  
  float flat, flon;
  unsigned long age;
  int heat =0;

  gps.f_get_position(&flat, &flon, &age);
  /***************/
  HC12.print("LAT="+String(flat,6)  +" | LONG=" + String(flon,6)   +  ", " "| Detected cm=" + String(dist)+  ", " "| TEMP[°C]="+ String(temp) +  ", " "| Humidity[%]=" + String(humidity) +  ", " "| Pressure[hPa]=" + String(pres) +", " "| Altitude[m]="+ String(alti)  + String(dist)+  "\n");             // Transmit non-command message
  /***************/
  smartdelay(1000);
  }


static void smartdelay(unsigned long ms)


{ 
  unsigned long start = millis();
  do 
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

This is not the correct syntax do a do-while loop.

1 Like

Also, it would really help if you were to put your code into a formatter to see where the braces line up. I don’t know if they all correctly align.