Hola a todos,
Estoy armando un sistema de alarma con puerta automática en Tinkercad y llevo varias horas atascado en un error de compilación que no logro ubicar.
Componentes:
- Arduino UNO
- Pantalla LCD I2C 16x2 (librería Adafruit_LiquidCrystal)
- Teclado matricial 4x4 (librería Keypad)
- Servomotor (librería Servo)
- Sensor ultrasónico HC-SR04
- Sensor PIR
- 2 LEDs + buzzer
El problema: Al intentar simular, Tinkercad muestra el error: "Sorry, it seems like your code has some errors. invalid header file"
Y ni siquiera deja iniciar la simulación. Lo raro es que:
- Probé cada librería por separado (LCD sola, LCD+Servo, LCD+Keypad, LCD+HC-SR04) y TODAS funcionan perfecto de manera individual.
- Probé también las 4 librerías juntas con un loop() simple (sin la lógica completa de la alarma) y funcionó bien.
- El error solo aparece cuando agrego la lógica completa del programa (varios "if" anidados, dos bucles "while", y una función personalizada que reemplaza a Serial.parseInt() leyendo del teclado matricial).
- Revisé manualmente el balance de llaves {} y paréntesis () y están correctos.
// C++ code
//
#include <Adafruit_LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
Adafruit_LiquidCrystal lcd(0);
Servo servoPuerta;
const int pinServo = 2;
const byte FILAS = 4;
const byte COLUMNAS = 4;
char keys[FILAS][COLUMNAS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte pinesFilas[FILAS] = {10, 9, 8, 7};
byte pinesColumnas[COLUMNAS] = {4, 3, A2, A3};
Keypad teclado = Keypad(makeKeymap(keys), pinesFilas, pinesColumnas, FILAS, COLUMNAS);
String cadenaTeclado = "";
int codigo;
boolean estado = false;
boolean estado2 = false;
int led1 = 12;
int led2 = 13;
int bocina = 11;
int sensor = A0;
int contra = 1234;
int cerrar = 4321;
int intensidad;
void setup() {
pinMode(6, OUTPUT);
pinMode(5, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(sensor, INPUT);
pinMode(bocina, OUTPUT);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setBacklight(1);
lcd.clear();
lcd.print("Prueba 6: OK");
delay(1000);
servoPuerta.attach(pinServo);
servoPuerta.write(0);
}
void loop() {
intensidad = analogRead(A1);
lcd.setCursor(0, 0);
lcd.print("Intens:");
lcd.print(intensidad);
lcd.print(" ");
if (intensidad >= 0 && intensidad < 300) {
long t;
long d;
digitalWrite(6, HIGH);
delayMicroseconds(10);
digitalWrite(6, LOW);
t = pulseIn(5, HIGH);
d = t / 59;
lcd.setCursor(0, 1);
lcd.print("d=");
lcd.print(d);
lcd.print(" ");
delay(200);
if (d < 45) {
if (estado == false) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Abra la puerta:");
codigo = obtenerCodigo();
estado2 = false;
lcd.setCursor(0,1);
lcd.print("cod=");
lcd.print(codigo);
delay(500);
if (codigo == cerrar) {
servoPuerta.write(90);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PUERTA ABIERTA");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1234:Cerrar+Act");
lcd.setCursor(0, 1);
lcd.print("4321:Solo Cerrar");
while (estado2 == false) {
codigo = obtenerCodigo();
if (codigo == contra) {
servoPuerta.write(0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Puerta Cerrada");
lcd.setCursor(0, 1);
lcd.print("Alarma: ACTIVA");
estado2 = true;
estado = true;
}
else if (codigo == cerrar) {
servoPuerta.write(0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Puerta Cerrada");
estado2 = true;
}
}
}
}
}
}
}
int obtenerCodigo() {
unsigned long tiempoInicio = millis();
while (millis() - tiempoInicio < 3000) {
if (Serial.available() > 0) {
return Serial.parseInt();
}
char tecla = teclado.getKey();
if (tecla) {
tiempoInicio = millis();
if (tecla >= '0' && tecla <= '9') {
cadenaTeclado += tecla;
lcd.setCursor(0, 1);
lcd.print("Clave: ");
lcd.setCursor(7, 1);
for (int i = 0; i < cadenaTeclado.length(); i++) {
lcd.print("*");
}
}
else if (tecla == '*') {
cadenaTeclado = "";
lcd.setCursor(0, 1);
lcd.print("Borrado... ");
delay(400);
}
else if (tecla == '#') {
if (cadenaTeclado.length() > 0) {
int valorDefinitivo = cadenaTeclado.toInt();
cadenaTeclado = "";
return valorDefinitivo;
}
}
if (cadenaTeclado.length() == 4) {
int valorDefinitivo = cadenaTeclado.toInt();
cadenaTeclado = "";
lcd.setCursor(0, 1);
lcd.print("Procesando... ");
delay(350);
return valorDefinitivo;
}
}
}
return 0;
}
Titulo editado por moderador de acuerdo a punto 5 de las normas
