-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
112 lines (95 loc) · 2.45 KB
/
main.c
File metadata and controls
112 lines (95 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <Keypad.h>
#include <Servo.h>
#define PINA A0
#define PINB A1
#define PINC A2
#define PINSENSOR 3
const byte rowsCount = 4;
const byte columsCount = 4;
Servo servoMotor;
int pos = 0;
#define DESARMADO 0
#define ARMADO 1
#define ENALERTA 2
String password="1529"; // Password del sistema
String inputPassword=""; //Variable para almacenar las entradas
byte val = 0;
byte estado = 0; // 0 desarmado, 1 sistema está armado, 2 el sistema ha detectado movimiento.
char keys[rowsCount][columsCount] = {
{ '1','2','3', 'A' },
{ '4','5','6', 'B' },
{ '7','8','9', 'C' },
{ '*','0','#', 'D' }
};
byte rowPins[rowsCount] = { 11, 10, 9, 8 };
byte columnPins[columsCount] = { 7, 6, 5, 4 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, columnPins, rowsCount, columsCount);
void setup() {
Serial.begin(9600);
pinMode(PINA, OUTPUT);
pinMode(PINB, OUTPUT);
pinMode(PINC, OUTPUT);
pinMode(PINSENSOR, INPUT);
servoMotor.attach(12);
}
void loop() {
leerTeclado();
if (estado == ARMADO ) //Si el estado es 0, no detectamos movimiento.
detectarMovimiento();
//Serial.println(inputPassword);
}
void leerTeclado(){
char key = keypad.getKey();
if (key) {
inputPassword += key;
}
if ( inputPassword.length() == 4) {
if (password == inputPassword) {
if (!estado ){
estado = ARMADO; // El sistema ha sido armado
Serial.println("El sistema ha sido armado");
}else if (estado == ENALERTA) {
estado = DESARMADO; // El sistema estaba en alerta y ahora ha sido desarmado.
apagarLeds();
desbloquearPuertas();
Serial.println("El sistema ha sido desarmado!");
}
}
else {
Serial.println("Clave Incorrecta!");
}
inputPassword = "";
}
}
void detectarMovimiento(){
val = digitalRead(PINSENSOR);
if (val == HIGH) {
encenderLeds();
bloquearPuertas();
estado = ENALERTA;
}
}
void encenderLeds(){
digitalWrite(PINA, HIGH);
digitalWrite(PINB, HIGH);
digitalWrite(PINC, HIGH);
}
void apagarLeds(){
digitalWrite(PINA, LOW);
digitalWrite(PINB, LOW);
digitalWrite(PINC, LOW);
}
void desbloquearPuertas(){
for (pos = 0; pos <= 180; pos += 1) {
servoMotor.write(pos);
delay(15);
}
Serial.println("Las puertas han sido desbloqueadas.");
}
void bloquearPuertas(){
for (pos = 180; pos >= 0; pos -= 1) {
servoMotor.write(pos);
delay(15);
}
Serial.println("Las puertas han sido bloqueadas.");
}