if statement errors

When I verify my code, the error message "expected unqualified-id before ‘if’ comes up on the first line of my if statement “if(cm=<5){”. What is the problem with the if statement?

int trigPin = 3;
int echoPin = 2;

int fwdRightMotor = 5;
int revRightMotor = 4;
int fwdLeftMotor = 7;
int revLeftMotor = 6;

void setup() {
Serial.begin (9600); //Starting Serial Terminal
}
void loop() {
long duration, inches, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds (20);
digitalWrite(trigPin, HIGH);
delayMicroseconds(100);
digitalWrite(trigPin, LOW);
pinMode (echoPin,INPUT);
duration=pulseIn(echoPin, HIGH);
inches=microsecondsToInches(duration);
cm=microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print(“cm”);
Serial.println();
delay(1000);

}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}

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

if(cm=<5){
digitalWrite(fwdRightMotor,HIGH);
digitalWrite(revRightMotor,LOW);
digitalWrite(fwdLeftMotor, HIGH);
digitalWrite(revLeftMotor, LOW);
delay(20);
}

Try “<=”.

This part of the code is out of any function;

if(cm=<5){ 
  digitalWrite(fwdRightMotor,HIGH);
  digitalWrite(revRightMotor,LOW);
  digitalWrite(fwdLeftMotor, HIGH);
  digitalWrite(revLeftMotor, LOW);
  delay(20);
}

That’s what the compiler tells you. It should be part of the loop or of another function. Move it before the closing braces of the loop.

And do what Blackfin suggests also…

A very helpful troubleshooting tool is the Auto Format feature (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor). If you do an Auto Format and then compare the resulting indentation to your intended program structure, it will quickly point you to where there is a missing or extra brace.

Another useful feature of the Arduino IDE/Arduino Web Editor is that when you place the cursor next to one bracket, it puts a box around the matching bracket. In the Arduino IDE, if the cursor is next to the closing bracket and the opening bracket is off the screen then it will show the opening bracket line in a tool tip after a short delay.

Your final code should look something like this:

int trigPin = 3;
int echoPin = 2;

int fwdRightMotor = 5;
int revRightMotor = 4;
int fwdLeftMotor = 7;
int revLeftMotor = 6;

void setup() {
Serial.begin (9600); //Starting Serial Terminal
}
void loop() {
  long duration, inches, cm;
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds (20);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(100);
  digitalWrite(trigPin, LOW);
  pinMode (echoPin,INPUT);
  duration=pulseIn(echoPin, HIGH);
  inches=microsecondsToInches(duration);
  cm=microsecondsToCentimeters(duration);
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(1000);

if(cm=<5){
  digitalWrite(fwdRightMotor,HIGH);
  digitalWrite(revRightMotor,LOW);
  digitalWrite(fwdLeftMotor, HIGH);
  digitalWrite(revLeftMotor, LOW);
  delay(20);
}

 
}
long microsecondsToInches(long microseconds) {
   return microseconds / 74 / 2;
}

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

I have placed this if(cm=<5) statement inside your loop function.

Your code says that you are using ultrasonic sensor and this if loop is for hurdle detection. When the distance is less than or equal to 5cm, then take a turn.