Simple ultrasonic sensor

I have a student developing a simple project with an ultrasonic sensory to detect if a person is within 1.5m and use a buzzer as an alert and displays distance on an lcd. The student is using Tinkercad Circuit to develop and run code through it's simulator.
Here is a link to a copy of the project:
https://www.tinkercad.com/things/6HoZrWLPQkb-copy-of-final-hopefully-ist-project/editel?sharecode=-DuP-4e8Dw3ZuAGRJSravzEZQiM67JGHKrLkQb7Q4Oc

There are no errors but the code seems to stop on line 44.

Any help would be appreciated. Sorry i've copied and pasted text in but I can't see the '#' tool icon to paste the code to.

#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 = 0;
// 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() {
lcd.clear();
digitalWrite(TRIGGER, HIGH);
delay(2);
digitalWrite(TRIGGER, LOW);

time = pulseIn(ECHO,HIGH);

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);
if (distance < 150)
{
digitalWrite(buzzer, HIGH);
lcd.print("Social Distance! At least 1.5m!!!!");
}
else
{
digitalWrite(buzzer, LOW);
lcd.print("Great work! Stay safe & wear a mask");

}
}

Use actual hardware.

This seems more appropriate:

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

The code works with real hardware to the extent of the LCD works and the rangefinder works.

Do you mean the one that says:
distance = 0.01716*time;

#define ECHO 10
#define TRIGGER 9
const int trigPin = 9;
const int echoPin = 10;

Why are there 2 sets of trigger and echo pins defined but with the same pin numbers.

const int buzzer = 0;

Pin 0 is the hardware serial RX pin. Maybe use a different one.

Why measure the distance twice in one loop()?

float time =0;

Time should be unsigned long. That is the data type that pulseIn() returns.

In the bottom of the code, you write to the LCD then, right away, the loop starts over and you clear the LCD. The LCD displays the message for very little time (us).

 lcd.print("Social Distance! At least 1.5m!!!!");

You need to pay attention to how many characters that you write to the LCD. The typical LCD libraries do not do line wrap. Also use setCursor to control where the text is printed.

Use the </> in the menu above to put code in code tags.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

@blindstrom, I don't think that this is an emergency :wink: And it does not seem to be related to Covid. So your topic has been moved to a more suitable location on the forum.

You are looking for the wrong icon

For advice on posting code correctly see How to get the best out of this forum

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.