//Sensor de ré UTILIZANDO Esp32_HC-SR04__LCD IC2_TRAB FINAL
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define Trigger_PIN 32 // Pino do trigger do sensor HC-SR04
#define Echo_PIN 35 // Pino do echo do sensor HC-SR04
#define BUZZER_PIN 18 // Pino para o buzzer
#define LED_PIN 27 // Pino para o LED
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
void setup()
{
pinMode(BUZZER_PIN, OUTPUT);
pinMode(Trigger_PIN, OUTPUT);
pinMode(Echo_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
lcd.begin(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
// Init
//lcd.init();
//lcd.backlight();
// Init
lcd.init();
lcd.backlight();
lcd.print(" Bem-vindo! ");
lcd.setCursor(3, 0);
lcd.print(" Ao Projeto ");
delay(2000);
lcd.clear();
}
void loop()
{
digitalWrite(Trigger_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(Trigger_PIN, LOW);
long duration=pulseIn(Echo_PIN,HIGH);
float distance_cm = duration/59;
// duration = pulseIn(Echo,HIGH);
//distance_cm =
// Ler o tempo de retorno do pulso
//duration = pulseIn(Echo_PIN, HIGH);
// Calcular a distância
// d = (t/2)/29.1 ;
// Mostrar a distância no display LCD
lcd.setCursor(2, 1);
lcd.print("distancia cm : ");
lcd.setCursor(7, 3);
lcd.print(distance_cm);
// Ligando o buzzer e acendendo o LED baseado na distância medida
if (distance_cm <= 30){
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
}
else
{
digitalWrite(BUZZER_PIN,LOW);
digitalWrite(LED_PIN, LOW);
}
delay(100);
}