expected '}' at end of input

i am making an obstacle avoiding robot (with ultrasonic sensor and 2 360 servos), and when i press verafiy it says expected } at the end of input, the code will be listed below,

#include <Servo.h>
#define echoPin 7
#define trigPin 6
Servo leftservo;
Servo rightservo;

void setup (){

Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
leftservo.attach(9);
rightservo.attach(10);
}

void loop()
{
int distance,duration
;digitalWrite(trigPin,HIGH);
delay(200);
digitalWrite(trigPin,LOW);
duration-pulseIn(echoPin,HIGH);
distance=(duration/2)/29.1;
Serial.println("");
Serial.println(distance);
if(distance >7) // for going forward
{
leftservo.write(360); // left wheel forward
rightservo.write(360); // right wheel forward
}
else // obstacle detected turn left
{
leftservo.write(-360); // left wheel backward
rightservo.write(360); // right wheel forward
}
}

Please post your code using code tags so that it can be copied out of the post easily by others.

Your code compiles cleanly on my system.

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:

[code]

[color=blue]// your code is here[/color]

[/code]

Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool but it’s still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

I get a different error:

sketch_jan14a:37: error: expected unqualified-id before '?' token

 }?
  ^

It seems there is a hidden garbage character at the very end of the sketch. Remove it and the sketch compiles without error.

However, if you turn on warnings in your File > Preferences. You will get a helpful message:

C:\Users\accou\AppData\Local\Temp\arduino_modified_sketch_213566\sketch_jan14a.ino:23:10: warning: value computed is not used [-Wunused-value]

  duration-pulseIn(echoPin,HIGH);

          ^

I’ll give you a chance to look at that code and figure out what’s wrong.

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

What model Arduino are you using?

The code compiles for me as a UNO, Win7 and IDE 1.8.5

Thanks.. Tom.. :slight_smile: