#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <DHT.h>
// Definir pines de sensores y actuadores
#define TRIG_PIN 7
#define ECHO_PIN 6
#define DHT_PIN 8
#define SERVO_PIN 9
#define BUZZER_PIN 10
#define MOTOR_IN1 11
#define MOTOR_IN2 12
// Pines de los botones
#define BTN_ULTRASONIC 2
#define BTN_DHT22 3
#define BTN_SERVO 4
#define BTN_BUZZER 5
#define BTN_MOTOR 13
// Configuración del sensor DHT
#define DHTTYPE DHT22
DHT dht(DHT_PIN, DHTTYPE);
// Configuración del servo
Servo myServo;
// Configuración de la pantalla LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Variables para controlar el estado de los actuadores y sensores
bool motorState = false;
bool buzzerState = false;
bool servoState = false;
bool updateUltrasonic = false;
bool updateDHT22 = false;
unsigned long lastUpdateUltrasonic = 0;
unsigned long lastUpdateDHT22 = 0;
const unsigned long updateInterval = 1000; // Intervalo de actualización de 5 segundos
void setup() {
// Iniciar serial y pantalla LCD
Serial.begin(9600);
lcd.begin(16, 2);
lcd.backlight();
// Configuración de los pines de los sensores y actuadores
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(MOTOR_IN1, OUTPUT);
pinMode(MOTOR_IN2, OUTPUT);
// Configuración de los botones
pinMode(BTN_ULTRASONIC, INPUT_PULLUP);
pinMode(BTN_DHT22, INPUT_PULLUP);
pinMode(BTN_SERVO, INPUT_PULLUP);
pinMode(BTN_BUZZER, INPUT_PULLUP);
pinMode(BTN_MOTOR, INPUT_PULLUP);
// Inicializar el sensor DHT y el servo
dht.begin();
myServo.attach(SERVO_PIN);
myServo.write(0); // Servo en posición inicial
}
void loop() {
// Leer el estado de los botones
bool btnUltrasonicState = digitalRead(BTN_ULTRASONIC) == LOW;
bool btnDHT22State = digitalRead(BTN_DHT22) == LOW;
bool btnServoState = digitalRead(BTN_SERVO) == LOW;
bool btnBuzzerState = digitalRead(BTN_BUZZER) == LOW;
bool btnMotorState = digitalRead(BTN_MOTOR) == LOW;
// Manejo del sensor ultrasónico
if (btnUltrasonicState) {
updateUltrasonic = !updateUltrasonic;
if (updateUltrasonic) {
lastUpdateUltrasonic = millis(); // Iniciar temporizador
}
}
if (updateUltrasonic) {
if (millis() - lastUpdateUltrasonic >= updateInterval) { // Actualizar cada 5 segundos
float distance = readUltrasonic();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distancia: ");
lcd.setCursor(0, 1);
lcd.print(distance);
lcd.print(" cm");
lastUpdateUltrasonic = millis();
}
}
// Manejo del sensor DHT22
if (btnDHT22State) {
updateDHT22 = !updateDHT22;
if (updateDHT22) {
lastUpdateDHT22 = millis(); // Iniciar temporizador
}
}
if (updateDHT22) {
if (millis() - lastUpdateDHT22 >= updateInterval) { // Actualizar cada 5 segundos
float temp = dht.readTemperature();
float hum = dht.readHumidity();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(hum);
lcd.print("%");
lastUpdateDHT22 = millis();
}
}
// Manejo del servo
if (btnServoState) {
if (!servoState) {
servoState = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Moviendo servo");
myServo.write(90); // Mover a 90 grados
} else {
servoState = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Servo listo");
myServo.write(0); // Volver a 0 grados
}
delay(1000); // Pausa para evitar rebotes
}
// Manejo del buzzer
if (btnBuzzerState) {
if (!buzzerState) {
buzzerState = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Activando buzzer");
digitalWrite(BUZZER_PIN, HIGH);
} else {
buzzerState = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Buzzer apagado");
digitalWrite(BUZZER_PIN, LOW);
}
delay(1000); // Pausa para evitar rebotes
}
// Manejo del motor DC
if (btnMotorState) {
if (!motorState) {
motorState = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motor encendido");
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
} else {
motorState = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motor apagado");
digitalWrite(MOTOR_IN1, LOW);
digitalWrite(MOTOR_IN2, LOW);
}
delay(1000); // Pausa para evitar rebotes
}
}
// Función para leer la distancia del sensor ultrasónico
float readUltrasonic() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration * 0.034 / 2;
return distance;
}