How to stop detected after 5 seconds using ultrasonic sensor

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);

#define trigPin 12 //Define the HC-SE04 triger on pin GPIO 12 (D6) on the arduino
#define echoPin 14 //Define the HC-SE04 echo on pin GPIO 14 (D5)on the arduino

#define door 0 //Define the relay signal on GPIO 0 (D3) on the arduino
#define motor 2 //Define the relay signal on GPIO 2 (D4) on the arduino
#define LGREEN 13 //Define the relay signal on GPIO 13 (D7) on the arduino

void setup()
lcd.init(); // initializing the LCD
lcd.backlight(); // Enable or Turn On the backlight

Serial.begin (9600); //Start the serial monitor
pinMode(trigPin, OUTPUT); //set the trigpin GPIO 12 to output
pinMode(echoPin, INPUT); //set the echopin GPIO 14 to input
pinMode(door,OUTPUT); //set the pintu GPIO 0 to output
pinMode(motor,OUTPUT); //set the motor on GPIO 2 to outpuT
pinMode(LGREEN, OUTPUT); //set the LGREEN on GPIO 13 to output


void loop()
lcd.print("PLEASE SCAN THE QR CODE ");

lcd.print("       THANK YOU!");

for (int positionCounter = 0; positionCounter < 13; positionCounter++)

int duration, distance; //Define two intregers duration and distance to be used to save data
digitalWrite(trigPin, HIGH); //write a digital high to the trigpin to send out the pulse
delayMicroseconds(10); //wait half a millisecond
digitalWrite(trigPin, LOW); //turn off the trigpin
duration = pulseIn(echoPin, HIGH); //measure the time using pulsein when the echo receives a signal set it to high
distance = (duration/2) / 29.1; //distance is the duration devided by 2 becasue the signal traveled from the trigpin then back to the echo pin, then divide by 29.1 to convert to centimeters

if (distance >= 33.02 || distance <= 0) //if the distance is less than 33.02cm
digitalWrite(door, HIGH); //turn ON the relay for door
digitalWrite(motor, HIGH); //turn ON the relay for motor
digitalWrite(LGREEN, LOW); //turn OFF the relay for ledgreen

Serial.println("TAK ADA MAKANAN");

Serial.print(distance); //Dispaly the distance on the serial monitor
Serial.println(" cm"); //in cm
delay (5000); //wait 5 seconds for motor // ! dia delay untuk semua (door,motor,led) !

digitalWrite(door, LOW); //turn OFF the relay NI 1 for door, when NO detection
digitalWrite(motor, LOW);
digitalWrite(LGREEN,HIGH); //turn ON the relay for ledgreen, When detected



This text will be hidden

It would be helpful if you posted your code using code tags. I'm not exactly sure what you are asking help with so if you provide more details we may be able to help.

Welcome syahira_zulkifli, what is your question, as it is asked It is hard to answer correctly. Posting links to the technical information on the hardware devices will help a lot. Can you post a schematic not a frizzy picture. I am assuming you are using an arduino Uno, is that correct. What relay?

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

You can RE-edit your first post by clicking on the pencil-icon below your post.

best regards Stefan

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