Pages: [1]   Go Down
Author Topic: error: expected unqualified-id before 'if'  (Read 713 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi i have a problem with a error that says:  "error: expected unqualified-id before 'if'" i have checked it sevaral times and i dont find any errors? can you help me

void setup()
{
Serial.begin(9600);
}

void loop()
{
  analogWrite(4, 200);
  analogWrite(5, 200);

  long duration, cm;

  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
  delayMicroseconds(2);
  digitalWrite(2, HIGH);
  delayMicroseconds(5);
  digitalWrite(2, LOW);

  duration = pulseIn(2, HIGH);

  cm = microsecondsToCentimeters(duration);
 
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(100);
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}


if (cm < 10)
{
analogWrite(4, 0);
analogWrite(5, 0);
}

Project_control:38: error: expected unqualified-id before 'if'
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your if statement needs to be inside a function. Presumably inside loop(), just after:
Code:
  cm = microsecondsToCentimeters(duration);

Please remember to use [code]...[/code] tags when posting code. See point 6 in http://arduino.cc/forum/index.php/topic,97455.0.html
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks it worked smiley-lol
Logged

Pages: [1]   Go Up
Jump to: