#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Stepper.h>
#define PASOS 2048
Servo puerta;
LiquidCrystal_I2C lcd(0x3F, 16, 2);
Stepper motor(PASOS, 8, 10, 11, 12);
int boton = 2;
int pot = A0;
int ledVerde = 6;
int ledRojo = 7;
bool estadoPuerta = false;
void setup() {
puerta.attach(9);
motor.setSpeed(10);
pinMode(boton, INPUT_PULLUP);
pinMode(ledVerde, OUTPUT);
pinMode(ledRojo, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Sistema Puerta");
}
void loop() {
int valorPot = analogRead(pot);
int velocidad = map(valorPot, 0, 1023, 5, 20);
motor.setSpeed(velocidad);
if (digitalRead(boton) == LOW) {
estadoPuerta = !estadoPuerta;
delay(300);
}
if (estadoPuerta) {
puerta.write(90);
digitalWrite(ledVerde, HIGH);
digitalWrite(ledRojo, LOW);
lcd.setCursor(0,1);
lcd.print("Puerta ABIERTA ");
} else {
puerta.write(0);
digitalWrite(ledVerde, LOW);
digitalWrite(ledRojo, HIGH);
lcd.setCursor(0,1);
lcd.print("Puerta CERRADA");
}
motor.step(50);
lcd.setCursor(11,0);
lcd.print("V:");
lcd.print(velocidad);
delay(200);
}