Reversing Sensor Code error - trying to trigger buzzer using 'if' statement

Hi there,

I'm new to coding and have written a code for a Reversing sensor, which consists of an ultrasonic sensor, LED, and LCD using an Arduino Mega 2560.

I managed to get the sensor and display to work properly using sample code templates but I want to take it a step further and trigger a 'LED' or a 'Buzzer' if the measurement is less than 5cm.

I have researched and tried a few things but always end up with the following error:

/Users/logic101/Documents/Arduino/Car_Reverse_Sensor_Practise/Car_Reverse_Sensor_Practise.ino: In function 'void loop()':
/Users/logic101/Documents/Arduino/Car_Reverse_Sensor_Practise/Car_Reverse_Sensor_Practise.ino:56:19: warning: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if ("Distance" <= 4);

  • ^*

I would be grateful if someone can have a look over my code below and error as above and provide some guidance on how I can make the 'LED' or 'Buzzer' turn on at a specific measurement is reached.

See below for actual code that contains the error.

#include <LiquidCrystal.h>

#define trigger 18
#define echo 19
int led = 9;

LiquidCrystal lcd(2,3,4,5,6,7);

float time=0,distance=0;

void setup()
{
lcd.begin(16,2);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
pinMode(led,OUTPUT);

pinMode(led, OUTPUT);

lcd.print(" Car Reverse ");
lcd.setCursor(0,1);
lcd.print(" Sensor ");
delay(3000);
lcd.clear();
lcd.print(" Distance Meter");
delay(3000);
lcd.clear();
lcd.print("logic101's Creation");
lcd.setCursor(0,1);
lcd.print ("DCC Y8 Science");
delay(3000);
}

void loop()
{
lcd.clear();
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
delayMicroseconds(2);
time=pulseIn(echo,HIGH);
distance=time*340/20000;
lcd.clear();
lcd.print("Distance:");
lcd.print(distance);
lcd.print("cm");
lcd.setCursor(0,1);
lcd.print("Distance:");
lcd.print(distance/100);
lcd.print("m");
delay(100);

if ("Distance" <= 4);
digitalWrite(led, HIGH);

}

Have you ever seen an if statement with quote marks around a variable name like that?

Also, watch that trailing semicolon.

Thanks for the reply and those that messaged.

I removed the quote marks, and ';' and now it works perfectly :slight_smile:

if (distance <= 10) digitalWrite(led, HIGH);
if (distance >= 10) digitalWrite(led, LOW);

(deleted)