#include <ESP32Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Puertos definidos del ULtrasonico
const int TRIG_PIN = 25; //define TRIG_PIN 18 trigger (salida)
const int ECHO_PIN = 26; //define ECHO_PIN 5 entrada
const int LED = 2; //Define LED 2 soldado en placa
//Los leds que pide el ejercicio
const int led1 = 23;
const int led2 = 22;
const int led3 = 21;
//valores del ultrasonico
float duration_us , distance_cm;
//Pin del NTC
const int sensorPin = 27; // Pin del sensor NTC conectado al ESP32
//Cosa para el NTC
const float BETA = 3950; // Coeficiente BETA para la formula
//Pin del Servomotor
const int servoPin = 5;
Servo myServo;
void setup()
{
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(LED, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
myServo.attach(servoPin);
//Esto es para el NTC
analogReadResolution(10);
pinMode(27,INPUT);
//Pantalla LCD
Serial.begin(115200);
lcd.init(); // initialize the lcd
lcd.backlight();
}
void loop()
{
//PROGRAMACION DEL ULTRASONICO
digitalWrite(TRIG_PIN, HIGH); //generacion e pulso en 10 microsegundos
delayMicroseconds(10); //10 MICROSEGUNDOS
digitalWrite(TRIG_PIN, LOW);
duration_us = pulseIn(ECHO_PIN, HIGH);// MEDICION DE LA DURACION EL PULSO
distance_cm = 0.017 * duration_us; //calcula la distancia dn cm
Serial.print("Distancia: ");
Serial.print(distance_cm);
Serial.println(" cm");
//Programacion del NTC (La formula)
int analogValue = analogRead(27);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.println(" ℃");
if(distance_cm < 30)
{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
// Gira el servomotor 20 grados en sentido horario
myServo.write(20);
// Espera un momento para que el servomotor alcance la posición deseada
delay(1000);
lcd.init();
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("distance_cm"); // print message at (0, 0)
lcd.setCursor(0, 1); // move cursor to (2, 1)
lcd.print("Led 1 ON"); // print message at (2, 1)
delay(2000); // display the above for two
lcd.init();
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Servo a 20°"); // print message at (0, 0)
delay(2000); // display the above for two seconds
}
else if (distance_cm > 50 && celsius < 20)
{
digitalWrite(led2, HIGH);
digitalWrite(led1, LOW);
digitalWrite(led3, LOW);
// Gira el servomotor 20 grados en sentido horario
myServo.write(150);
lcd.init();
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("distance_cm"); // print message at (0, 0)
lcd.setCursor(0, 1); // move cursor to (2, 1)
lcd.print("celsius"); // print message at (2, 1)
delay(2000); // display the above for two seconds
lcd.init();
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Servo a 150°"); // print message at (0, 0)
delay(2000); // display the above for two seconds
}
else
{
digitalWrite(led3, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
// Gira el servomotor 20 grados en sentido horario
myServo.write(90);
lcd.init();
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Distancia 0"); // print message at (0, 0)
lcd.setCursor(0, 1); // move cursor to (2, 1)
lcd.print("celsius"); // print message at (2, 1)
delay(2000); // display the above for two seconds
lcd.init();
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Servo a 90°"); // print message at (0, 0)
delay(2000); // display the above for two seconds
}
}