Expected Initializer Before } Token

Hi everyone, Im new here to Arduino



[code]
#include <Adafruit_MLX90614.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

#define trigPin 3
#define echoPin 2
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
void setup() {
  mlx.begin();
  lcd.backlight();
  lcd.begin(16, 2);
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  servo.attach(9);
  int calc_dis

}



void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;

}

if (calc_dis < 10)
{
  for (int i = 0; i <= 540; i++)
  {
    lcd.setCursor(0, 0);
    lcd.print("Ambient ");
    lcd.print(mlx.readAmbientTempC());
    lcd.print(" C");

    lcd.setCursor(0, 1);
    lcd.print("Target  ");
    lcd.print(mlx.readObjectTempC());
    lcd.print(" C");
    delay(3000);
    lcd.clear();
    servo.write(180);
    delay(1000);
    servo.write(0);


  }
  {
    int duration, distance;
    digitalWrite(trig, HIGH);
    delay(dt);
    digitalWrite(trig, LOW);
    duration = pulseIn(echo, HIGH);
    distance = (duration / 2) / 29.1;
    return distance;
  }
}
[/code]

Im creating a new project but I keep getting the Expected Initializer Before } Token error.

Heres the error:
At global scope:
test:37:1: error: expected unqualified-id before 'if'
if (calc_dis < 10)
^~
exit status 1
expected initializer before '}' token

Can anyone help me? Thanks alot.

Look carefully where the loop() function ends. As it is you have code starting with if (calc_dis < 10) outside of a function

1 Like

What dat?

take out the } before the if.

int calc_dis

But now I get the error here

Missing semicolon

Hi, @ycg
Thanks for using code tags.

In the IDE press CTRL-T, it will auto format your code and let you see where your { } problems are.
The post your code in a new post.

Tom... :smiley: :+1: :coffee::australia:

2 Likes

...which makes for a variable definition that immediately goes out of scope.
Hence reply #3.

Hi,
I think you aimed to make this a function?

 int duration, distance;
    digitalWrite(trig, HIGH);
    delay(dt);
    digitalWrite(trig, LOW);
    duration = pulseIn(echo, HIGH);
    distance = (duration / 2) / 29.1;
    return distance;

You repeat it twice in the void loop().

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Thanks! That was quite helpful!

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