If statement help in code

Hello!
Im making an arduino project that requires it to turn a LED on when the HC-SR04 detects a object within 20cm.
I know how to use the if statement, but when tried being used in the project, the led doesnt work. ( i’ve tested the conections on the arduino, they are fine and also the led, it works). May someone please help me here with the code, because i dotn know what to put in when i say

if ({dont know what to put} < 15)
digitalWrite(ledPin, HIGH)
else
digitalWrite(ledPin, LOW)

thanks very much
Code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, A0, A1, A2, A3, A4, A5);

#define trigPin 12 
#define echoPin 13 

void setup()
{
  lcd.begin(16,2);             
  lcd.clear();
   
  ** LCD code ***

  delay(5000);

  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT);
}

int Dist(){
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  
  return distance;
}

void loop()
{

  lcd.setCursor(0,1);          
  lcd.print("Eco-Can Project");
  lcd.setCursor(0,0);           
  lcd.print("Distance: ");  

  lcd.setCursor(10,0);
  lcd.print(Dist());
  lcd.print("cm  ");

  delay(100);
}

thanks for helping

  • clock

clockpie_5436:
Hello!
Im making an arduino project that requires it to turn a LED on when the HC-SR04 detects a object within 20cm.
I know how to use the if statement, but when tried being used in the project, the led doesnt work. ( i’ve tested the conections on the arduino, they are fine and also the led, it works). May someone please help me here with the code, because i dotn know what to put in when i say

if ({dont know what to put} < 15)

digitalWrite(ledPin, HIGH)
else
digitalWrite(ledPin, LOW)




thanks very much
Code:


#include <LiquidCrystal.h>
LiquidCrystal lcd(2, A0, A1, A2, A3, A4, A5);

#define trigPin 12
#define echoPin 13

void setup()
{
  lcd.begin(16,2);           
  lcd.clear();
 
  ** LCD code ***

delay(5000);

pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

int Dist(){
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
 
  return distance;
}

void loop()
{

lcd.setCursor(0,1);         
  lcd.print(“Eco-Can Project”);
  lcd.setCursor(0,0);         
  lcd.print("Distance: ");

lcd.setCursor(10,0);
  lcd.print(Dist());
  lcd.print("cm  ");

delay(100);
}




thanks for helping
- clock

Use your serial monitor to display the values you receive from the sensor. Look at example programs and set your serial monitor to 9600 baud. Then Serial.write a value or Serial.println a value.

Paul