error message NewPingExample.ino: In function 'void loop()': NewPingExample.ino:

So I’m trying to make some LEDs light up if an ultrasonic distance sensor detects an obstacle within a certain distance.
I’ve used an example sketch for the actual distance sensor, which worked very well and compiled before I tried the LEDs. Here’s my code:

#include <NewPing.h>

#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
for(int pinNumber = 5; pinNumber<7; pinNumber++){
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}

void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println(“cm”);
if (uS / US_ROUNDTRIP_CM = 0){
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}
else if (uS / US_ROUNDTRIP_CM >= 301){
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}
else if (uS / US_ROUNDTRIP_CM >= 201 && uS / US_ROUNDTRIP_CM <= 300){
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
}
else if (uS / US_ROUNDTRIP_CM >= 101 && uS / US_ROUNDTRIP_CM <= 200){
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
}
else if (uS / US_ROUNDTRIP_CM >= 5 && uS / US_ROUNDTRIP_CM <= 100){
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}
else if (uS / US_ROUNDTRIP_CM >= 1 && uS / US_ROUNDTRIP_CM <= 5){
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
}
}

The error is:
NewPingExample.ino: In function ‘void loop()’:
NewPingExample.ino:27:28: error: lvalue required as left operand of assignment
I have no idea what it means… Please help! :frowning:

UltrasonicSensor_LandingLights.ino (2.09 KB)

  if (uS / US_ROUNDTRIP_CM = 0){A single = is an assignment not a test of equality.

Hi, Welcome to the forum.

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.

Thanks... Tom... :)

Instead of using this over and over:-uS / US_ROUNDTRIP_CM
and doing the ping like this:-sonar.ping();
You can do this:-unsigned long distance = sonar.ping_cm();  // Measure distance in cm
then use ‘distance’ everywhere that you now use:-uS / US_ROUNDTRIP_CM
Edit: And something else I just noticed - In this you only make pins 5 and 6 outputs:-

for(int pinNumber = 5; pinNumber<7; pinNumber++){
    pinMode(pinNumber, OUTPUT);
    digitalWrite(pinNumber, LOW);
  }

I think you meant:-

for(int pinNumber = 5; pinNumber<=7; pinNumber++){
    pinMode(pinNumber, OUTPUT);
    digitalWrite(pinNumber, LOW);
  }