Error code: expected primary-expression before 'if'

This has been bugging me for a few days and I keep coming back to it and having little sessions trying to work it out but frustratingly (considering how simple I know this will turn out to be) none of my attempts to guess how to fix it have prevailed.

Here is the piece of code that I keep getting the error message: "expected primary-expression before 'if'" on my 'if else' statement

#include <NewPing.h>

#define TRIGGER_PIN  21
#define ECHO_PIN     22
#define TRIGGER_PIN2  23
#define ECHO_PIN2     24
#define TRIGGER_PIN3  25
#define ECHO_PIN3     26
#define MAX_DISTANCE 300
NewPing Front(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
NewPing Left(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);
NewPing Right(TRIGGER_PIN3,ECHO_PIN3, MAX_DISTANCE);
int motorpin1 = 2;

void setup() {
 Serial.begin(115200); //(115200)
pinMode(pin1,OUTPUT);

}

void loop() {
  
 delay(50);
  int uS = Front.ping();
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM);
  Serial.println("Front cm"); 
 delay(50)
 
 (if (Front.ping < 50)); 
{
   int qT = Left.ping();
  Serial.print("Ping: ");
  Serial.print(qT / US_ROUNDTRIP_CM);
  Serial.println("Left cm"); 
  delay(50);

   int gU = Right.ping();
  Serial.print("Ping: ");
  Serial.print(gU / US_ROUNDTRIP_CM);
  Serial.println("Right cm"); 
  delay(50);
}
else
{
 analogWrite(pin1, 100);
}
}

I have tried adding and taking away various types of peculiar brackets eg (, { and [ but none of these have fixed it.
Looking at the Arduino reference pages for 'if' and 'else' I haven't been able to find any thing that should be before the 'if' statment.
I haven't been able to find a list of primary expressions to try before 'if' so if someone could give me a few of those or help look over this code for me (I expect i'm just ignorant when it comes to Arduino) then that would be greatly appreciated.

This is wrong.:-

(if (Front.ping < 50));{

Should be:-

if (uS < 50)
{

or

if (Front.ping() < 50)
{

But even that won't do what you want, since I assume you're checking that the distance is less than 50cm, not 50us.

I could write it for you, but it's better practice if you re-write the code to do what you're aiming for.

It better beif (Front.ping() < 50) {or, because the value is already thereif (uS < 50) {

Oops, missed it - you also left the semicolon off the end of first of these:-

delay(50)

Thanks alot guys, I'll have another crack at writing it to make sure it's checking the distance in the correct units :slight_smile:
Having made the changes you suggested it still doesn't seem to identify the 'if' part of the statment and I know get the error: 'else' without a previous 'if'.
Does anybody have any ideas as to what might be causing this?
I just caught that missed semicolon as I made the changes you suggested, the code is now:

#include <NewPing.h>

#define TRIGGER_PIN  21
#define ECHO_PIN     22
//#define MAX_DISTANCE 200
#define TRIGGER_PIN2  23
#define ECHO_PIN2     24
//#define MAX_DISTANCE 200
#define TRIGGER_PIN3  25
#define ECHO_PIN3     26
#define MAX_DISTANCE 300
NewPing Front(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
NewPing Left(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);
NewPing Right(TRIGGER_PIN3,ECHO_PIN3, MAX_DISTANCE);
int motorpin1 = 2;
int motorpin2 = 3;

void setup() {
  Serial.begin(115200); //(115200)
pinMode(motorpin1,OUTPUT);
pinMode(motorpin2,OUTPUT);
}

void loop() {
  
 delay(50);
  int uS = Front.ping();
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM);
  Serial.println("Front cm"); 
 delay(50);
 
if (uS < 50) ; //change 50 to 10cm more than the distance required by the chassis to carry out a 90 degree turn with the servo set at 30 degrees
{
   int qT = Left.ping();
  Serial.print("Ping: ");
  Serial.print(qT / US_ROUNDTRIP_CM);
  Serial.println("Left cm"); 
  delay(50);

   int gU = Right.ping();
  Serial.print("Ping: ");
  Serial.print(gU / US_ROUNDTRIP_CM);
  Serial.println("Right cm"); 
  delay(50);
}
else
{
 analogWrite(motorpin1, 100); // Test whether it needs to be motorpin 1 or 2 and the best value
}
}

Anybody have any ideas as to why it won't recognise the 'if'?

324632985738274:
Anybody have any ideas as to why it won't recognise the 'if'?

Because you still have a semicolon at the end of the 'if' statement.:-if (uS < 50) ;Lose that semicolon.

And you can make things easier for yourself by using the library's 'ping_cm()' method. No need to do a separate conversion.:-

int cm = Front.ping_cm();

Edit: Just one other small point, although you're doing nothing wrong - it's better to identify the analogue pins with an "A" prefix to avoid confusion.
Forget this. God only knows what I was thinking.

OldSteve:
Edit: Just one other small point, although you're doing nothing wrong - it's better to identify the analogue pins with an "A" prefix to avoid confusion.
ie

int motorpin1 = A2;

int motorpin2 = A3;

Nope.

The sketch does not use analog inputs, so this is totally wrong.

oqibidipo:
Nope.

The sketch does not use analog inputs, so this is totally wrong.

Oh @#$%, I don't know what I was thinking. You are correct.
Quite embarrassed, I don't know how I made such a stupid mistake. :frowning:
(Hanging my head in shame.)

Sorry @324632985738274