#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.begin(16,2);
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.setCursor(0,0);
lcd.print("PLEASE SCAN THE QR CODE ");
lcd.setCursor(0,1);
lcd.print(" THANK YOU!");
for (int positionCounter = 0; positionCounter < 13; positionCounter++)
lcd.scrollDisplayLeft();
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
delayMicroseconds(2);
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");
}
else
{
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
}
Summary
This text will be hidden