Compilation error: a function-definition is not allowed here before '{' token

cant for the life of me figure out why im getting this error

void loop() {
// generate 10-microsecond pulse to TRIG pin
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
lcd.setCursor(1, 0);
lcd.print("ImperialCredits");
lcd.setCursor(1, 1);
lcd.print("Only");
lcd.clear();
delay(2000);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("no unsupervised");
lcd.setCursor(1, 1);
lcd.print("Jawas or Ewoks");

// measure duration of pulse from ECHO pin
duration_us = pulseIn(ECHO_PIN, HIGH);
// calculate the distance
distance_cm = 0.017 * duration_us;

}
if (distance_cm < DISTANCE_THRESHOLD) {
digitalWrite(LED_PIN, HIGH); // turn on LED
servo.attach(servoPin);
servo.write(180);
}

else {
digitalWrite(LED_PIN, LOW); // turn off LED

// print the value to Serial Monitor
Serial.print("distance: ");
Serial.print(distance_cm);
Serial.println(" cm");

delay(500);

}
}
}

Please post ALL the code, using code tags. Hint: use CTRL-T in the Arduino code editor to autoformat the code, before copy/paste.

See the "How to get the best out of this forum" post.

Read the forum guidelines to see how to properly post code and some good information on making a good post. Post all the code. You have left off important information about variables, libraries and the machine setup.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags. When you do use the autoformat tool, I think that you will see that you have curly brackets out of place. The loop() function is closed early and the rest of the code is outside of a function which is not legal.

Please go back and fix your original post.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

I have reformatted your sketch of post #1 and presented below. Check it line-by-line with the original sketch to see why you are getting compilation errors.

#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#include<Servo.h>
Servo servo;

#define TRIG_PIN 5
#define ECHO_PIN 6
#define LED_PIN 7
#define servoPin 8

#define DISTANCE_THRESHOLD 15
unsigned int  distance_cm;
unsigned long int   duration_us;

void setup()
{
  Serial.begin(9600);
  lcd.begin();//lcd.init();  //or lcd.begin();
  lcd.backlight();
}

void loop()
{
  // generate 10-microsecond pulse to TRIG pin
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  lcd.setCursor(1, 0);
  lcd.print("ImperialCredits");
  lcd.setCursor(1, 1);
  lcd.print("Only");
  lcd.clear();
  delay(2000);
  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("no unsupervised");
  lcd.setCursor(1, 1);
  lcd.print("Jawas or Ewoks");

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(ECHO_PIN, HIGH);
  // calculate the distance
  distance_cm = 0.017 * duration_us;

  if (distance_cm < DISTANCE_THRESHOLD) 
  {
    digitalWrite(LED_PIN, HIGH); // turn on LED
    servo.attach(servoPin);
    servo.write(180);
  }
  else
  {
    digitalWrite(LED_PIN, LOW); // turn off LED
    // print the value to Serial Monitor
    Serial.print("distance: ");
    Serial.print(distance_cm);
    Serial.println(" cm");
    delay(500);
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.