Very simple Ultrasonic sensor and distance sensor

I am trying to create a code which does the following:

Display some text
When an object is within 1.5m of the device, create an alert and and write some text
Else, write some text.

I am using tinkercad's simulation (link can be found here: Login | Tinkercad) and the code is bellow (sorry i don't know how to post in tags).

At the very end, the code is overlapping and I can't seem to figure out why. It also is printing 0 for the distance, where as it should be printing the distance that the object is from the device.

Any advice and help is much appreciated.

#include <LiquidCrystal.h>
#define ECHO 10
#define TRIGGER 9

float time =0;
float distance = 0;

LiquidCrystal lcd(12,11,5,4,3,2);
int num=1;

const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 6;
// defines variables
long duration;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Inputer
pinMode (buzzer, OUTPUT); //Makes the buzzer an output
Serial.begin(9600); // Starts the serial communication

pinMode(TRIGGER, OUTPUT);
pinMode(ECHO, INPUT);
lcd.begin(16, 2);
lcd.print("COVID");
delay(2000);
lcd.clear();
lcd.print("Social Distance");
delay(2000);
lcd.clear();
lcd.print("1.5 Metres");
delay(2000);
}

void loop() {

digitalWrite(TRIGGER, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER, LOW);
duration = pulseIn(ECHO, HIGH);

//time = pulseIn(ECHO,HIGH); This line is wrong

distance = 0.01716*time;
Serial.println(time);
lcd.setCursor(5,0);
lcd.print(float(distance/100));
lcd.setCursor(11,0);
lcd.print("m");

delay(1000);

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);

lcd.clear();

if (distance < 150)
{
digitalWrite(buzzer, HIGH);
lcd.setCursor(5,0);
lcd.print("Social Distance!");
lcd.setCursor(5,1);
lcd.print("At least 1.5m!");
}
else
{
digitalWrite(buzzer, LOW);
lcd.setCursor(5,0);
lcd.print("Great work! Stay safe");
lcd.setCursor(5,1);
lcd.print("and wear a mask");
}
}

Same question and same code can be found at Simple ultrasonic sensor.

Hi, @dstamoulos
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

Do you have any hardware to experiment with?
MUCH, MUCH better than simulations.

Tom... :smiley: :+1: :coffee: :australia:

Unfortunately, we're in a lockdown and can't access any hardware :frowning:

Hi,
Is this a school/educational institution project?

Can you post your code in tags and the Tinkercad image of your project/circuit?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

I'm sorry i'm not quite sure how to post in tags. The link however is above which takes you straight to the simulation. I've attached a photo too.

To add code please click this link;

Tom.. :smiley: :coffee: :coffee: :australia:

You commented out the line that sets the value of 'time'. You should change that last line to use 'duration' instead of 'time'.