Error with SR-04 ultrasonic sensor

This is the error I am getting from the code "exit status 1" and "expected primary-expression before '>' token" It's probably something basic but I'm not sure what it is asking for.

 if (NewPing > 10)

Huntechr1:
This is the error I am getting from the code "exit status 1" and "expected primary-expression before '>' token" It's probably something basic but I'm not sure what it is asking for.

 if (NewPing > 10)

What is that code line supposed to do?
Please post your full code, between code tags. Code tags are </> in the "Reply" window.

The code should make a motor spin with an H-bridge if the ultrasonic sensor gets a reading of less than 10.

#include <NewPing.h>
#define TRIGGER_PIN 11
#define ECHO_PIN 12
#define MAX_DISTANCE 50 //max distance to ping for
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
const int rightMotor1 = 6;
const int rightMotor2 = 7;
const int enablePin = 9;
void setup() 
{
  Serial.begin(9600);
  digitalWrite(enablePin, LOW);
}

void loop() 
{
  delay(50);
  unsigned int uS = sonar.ping();
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM);
  Serial.println("cm");
  digitalWrite(enablePin, HIGH);
  if (NewPing sonar > 10)
{
  digitalWrite(rightMotor1, HIGH);
  digitalWrite(rightMotor2, LOW);
}
}

See anything wrong here ...

  if (NewPing sonar > 10)

Honestly, wish I did because I probably don't look very intelligent by not knowing

You can't use "NewPing" like that. (As Lloyd pointed out while I was typing. :slight_smile: )
This is the nearest thing to your code that will compile, but I think you'll still have other problems with the operation of your motors.

#include <NewPing.h>
#define TRIGGER_PIN 11
#define ECHO_PIN 12
#define MAX_DISTANCE 50 //max distance to ping for
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
const int rightMotor1 = 6;
const int rightMotor2 = 7;
const int enablePin = 9;
void setup()
{
    Serial.begin(9600);
    digitalWrite(enablePin, LOW);
}

void loop()
{
    delay(50);
    unsigned int uS = sonar.ping();
    int distance = uS / US_ROUNDTRIP_CM;
    Serial.print("Ping: ");
    Serial.print(distance);
    Serial.println("cm");
    digitalWrite(enablePin, HIGH);
    if (distance > 10)
    {
        digitalWrite(rightMotor1, HIGH);
        digitalWrite(rightMotor2, LOW);
    }
}

By the way, the conversion from microseconds to cm can be done automatically by the "NewPing" library.
Just use

int distance = sonar.ping_cm()

instead of 'sonar.ping()' and the separate conversion.

Could you elaborate on why the motor doesn't spin with this code please?

Huntechr1:
Could you elaborate on why the motor doesn't spin with this code please?

I didn't say the motor won't spin. I have no idea how your motor is connected, or even if you're using a shield. Edit: I just noticed - you're using a H-bridge of some sort. L298N?
What I meant was that you take no different action if the distance is less than or equal to 10cm.
The main thing was to get your ultrasonic ranging working, then you can experiment with the rest of the code and get a feel for what you're doing. Have fun. :slight_smile: (That's what it's all about.)

I'm using an L293d, thanks for the help with the ultrasonic code :slight_smile:

Huntechr1:
I'm using an L293d, thanks for the help with the ultrasonic code :slight_smile:

I was close. :slight_smile:
No problem, glad to help. Yell out if you strike further problems.

Huntechr1:
I'm using an L293d

Did you get that sorted as we discussed in this thread a few days ago?

Yep I got that figured out and have moved on to bigger and better problems! :slight_smile: