Dev - C++ - necesito prender un mismo led, segun dos variables distintas

 
Vista:

necesito prender un mismo led, segun dos variables distintas

Publicado por Gabriel (1 intervención) el 03/10/2019 22:22:23
Hola tengo un problema con un codigo el cual necesito prender un mismo led, segun dos variables distintas, dos tipos de delay.
Ejemplo: que un led encienda a una hora 5segundos y a otra 3segundos.

Si no se puede realizar por favor me lo aclararian.

Este es mi codigo:
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
 
LiquidCrystal lcd(7,6,5,4,3,2); // inicializa la interfaz I2C del LCD 16x2
RTC_DS1307 RTC;                   // inicializa el modulo RTC
int r_diaSemana;                  // almacena el resultado del dia de la semana calculado
const int timbre = 8;             // Pin 7 encargado de activar el timbre, se conecta al Rele
int minuto;
int hora;
int segundo;
int tiempo_timbre=5000; // Tiempo continuo que dura el timbre sonando, en milisegundos 5000 = 5 segundos
 
//////////////////////////////// Horario 1 /////////////////////////
// Hora en la que suena el timbre escrito  h1=Hora, m1=Minutos, s1=Segundos
// Cuando no se programa ninguna hora se debe dejar escrito el numero 99  
// se pueden programar 16 timbres por cada horario, _c1 indica que es el horario 1
int h1_c1=7;   int m1_c1=25;   int s1_c1=0;
int h2_c1=8;   int m2_c1=05;  int s2_c1=0;
int h3_c1=8;   int m3_c1=45;  int s3_c1=0;
int h4_c1=9;   int m4_c1=40;  int s4_c1=0;
int h5_c1=10;  int m5_c1=20;  int s5_c1=0;
int h6_c1=11;  int m6_c1=15;  int s6_c1=0;
int h7_c1=11;  int m7_c1=55;  int s7_c1=0;
int h8_c1=12;  int m8_c1=35;  int s8_c1=0;
int h9_c1=13;  int m9_c1=45;  int s9_c1=0;
int h10_c1=14; int m10_c1=44;  int s10_c1=0;
int h11_c1=15; int m11_c1=05;  int s11_c1=0;
int h12_c1=15; int m12_c1=28;  int s12_c1=0;
int h13_c1=16; int m13_c1=35;  int s13_c1=0;
int h14_c1=17; int m14_c1=58;  int s14_c1=0;
int h15_c1=18; int m15_c1=05;  int s15_c1=0;
int h16_c1=99; int m16_c1=0;  int s16_c1=0;
 
//////////////////////////////// Horario 2 /////////////////////////
int h1_c2=99;   int m1_c2=0;   int s1_c2=0;
int h2_c2=99;   int m2_c2=0;  int s2_c2=0;
int h3_c2=99;   int m3_c2=0;  int s3_c2=0;
int h4_c2=99;   int m4_c2=0;  int s4_c2=0;
int h5_c2=99;   int m5_c2=0;  int s5_c2=0;
int h6_c2=99;  int m6_c2=0;  int s6_c2=0;
int h7_c2=99;  int m7_c2=0;  int s7_c2=0;
int h8_c2=99;  int m8_c2=0;  int s8_c2=0;
int h9_c2=99;  int m9_c2=0;  int s9_c2=0;
int h10_c2=99; int m10_c2=0;  int s10_c2=0;
int h11_c2=99; int m11_c2=0;  int s11_c2=0;
int h12_c2=99; int m12_c2=0;  int s12_c2=0;
int h13_c2=99; int m13_c2=0;  int s13_c2=0;
int h14_c2=99; int m14_c2=0;  int s14_c2=0;
int h15_c2=99; int m15_c2=0;  int s15_c2=0;
int h16_c2=99; int m16_c2=0;  int s16_c2=0;
 
//////////////////////////////// Horario 3 /////////////////////////
int h1_c3=99;   int m1_c3=0;   int s1_c3=0;
int h2_c3=99;   int m2_c3=0;  int s2_c3=0;
int h3_c3=99;   int m3_c3=0;  int s3_c3=0;
int h4_c3=99;   int m4_c3=0;  int s4_c3=0;
int h5_c3=99;   int m5_c3=0;  int s5_c3=0;
int h6_c3=99;  int m6_c3=0;  int s6_c3=0;
int h7_c3=99;  int m7_c3=0;  int s7_c3=0;
int h8_c3=99;  int m8_c3=0;  int s8_c3=0;
int h9_c3=99;  int m9_c3=0;  int s9_c3=0;
int h10_c3=99; int m10_c3=0;  int s10_c3=0;
int h11_c3=99; int m11_c3=0;  int s11_c3=0;
int h12_c3=99; int m12_c3=0;  int s12_c3=0;
int h13_c3=99; int m13_c3=0;  int s13_c3=0;
int h14_c3=99; int m14_c3=0;  int s14_c3=0;
int h15_c3=99; int m15_c3=0;  int s15_c3=0;
int h16_c3=99; int m16_c3=0;  int s16_c3=0;
 
 
////////////////////////////////// Void Setup() ///////////
void setup () {
 
 pinMode(timbre, OUTPUT);                    // Configura como salida el pin 7
 Wire.begin();
 RTC.begin();                                // Inicia la comunicaci¢n con el RTC
 
// RTC.adjust(DateTime(__DATE__, __TIME__)); // Lee la fecha y hora del PC (Solo en la primera carga)
                                             // el anterior se usa solo en la configuracion inicial luego se pone como comentario
 lcd.begin(16, 2);                                            // y se vuelve a cargar el programa sin esa linea.
 Serial.begin(9600);                         // Establece la velocidad de datos del puerto serie a 9600
 lcd.clear();                                // Borra el  LCD
}
////////////////////////////////// Void loop() ///////////
void loop(){
 DateTime now = RTC.now();          // Obtiene la fecha y hora del RTC
 
 int contacto1 = analogRead(A0);    //Lee el valor de los contactos para escoger el horario
 int contacto2 = analogRead(A1);
 int contacto3 = analogRead(A2);
 int contacto4 = analogRead(A3);    // contacto que activa o desactiva los fines de semana
 
 Serial.print(now.year(), DEC);  // A§o
 Serial.print('/');
 Serial.print(now.month(), DEC); // Mes
 Serial.print('/');
 Serial.print(now.day(), DEC);   // Dia
 Serial.print(' ');
 Serial.print(now.hour(), DEC);  // Horas
 Serial.print(':');
 Serial.print(now.minute(), DEC); // Minutos
 Serial.print(':');
 Serial.print(now.second(), DEC); // Segundos
 Serial.println();
 lcd.setCursor(0,0);
 lcd.print("D:");
 lcd.print(now.year(), DEC);
 lcd.print("/");
 lcd.print(now.month(), DEC);
 lcd.print("/");
 lcd.print(now.day(), DEC);
 lcd.print(" ");
 lcd.setCursor(0,1);
 lcd.print("T: ");
 lcd.print(now.hour(), DEC);
 lcd.print(":");
 lcd.print(now.minute(), DEC);
 lcd.print(":");
 lcd.print(now.second(), DEC);
 segundo=now.second();
 minuto=now.minute();
 hora=now.hour();
 
 if (contacto4 <= 1000){     // si el contacto 4 esta desactivado, despliega -e- entre semana funciona el timbre
 }
 else {                      // si el contacto 4 esta activado, despliega -F- el Fin de semana funciona el timbre
 }
 
 int r_diaSemana=dia_de_semana();   // llama a la funcion que calcula el dia de la semana y lo almacena en r_diaSemana
 
 if ((r_diaSemana == 6 || r_diaSemana == 0)&&(contacto4 <= 1000)){  // si el contacto4 de Fin de semana esta apagado y es fin de semana no hace nada
 }
 else {
   if (contacto1 >= 1000)   // Si el contacto 1 esta activo (Horario 1)    
     horario_1();           // llama a la funcion que activa el horario 1
 
   if (contacto2 >= 1000)  // Si el contacto 2 esta activo (Horario 2)
     horario_2();          // llama a la funcion que activa el horario2
 
   if (contacto3 >= 1000)  // Si el contacto 1 esta activo (Horario 3)
     horario_3();          // llama a la funcion que activa el horario 3
 }
 
 digitalWrite(timbre, LOW);     // apaga el timbre
 delay(500);                    // La informaci¢n se actualiza cada 1/2 seg.
 lcd.setCursor(9,0);            // Borra parte de la pantalla del LCD
 lcd.print("                ");
 lcd.setCursor(8,1);
 lcd.print("                ");
}   //////////////////////////////// Fin del void loop(), inician las Funciones/////////////////////////
 
/////////////////////////////// Funcion que activa el Timbre //////////////////////////////////
void activar_timbre(){
    digitalWrite(timbre, HIGH);
    lcd.setCursor(0,0);
    lcd.print("Timbre ON  ");
    Serial.println("Timbre Activo");
    delay(tiempo_timbre);
}
/////////////////////////////// Calcula el dia de la Semana //////////////////////////////////
int dia_de_semana(){
 // Encuentar el dia de la semana de una fecha
 int n_dia;
 int r_dia;
 int n_mes;
 int t_mes;
 int n_anno;
 int d_anno;
 int t_siglo=6;
 
 DateTime now = RTC.now(); //fecha y hora del RTC
 lcd.setCursor(13,1);
 
 n_anno=(now.year()-2000);
 d_anno=n_anno/4;
 n_dia=now.day();
 n_mes=now.month();
 
 switch (n_mes) {
    case 1:
      t_mes=0;
      break;
    case 2:
      t_mes=3;
      break;
    case 3:
      t_mes=3;
      break;
    case 4:
      t_mes=6;
      break;
    case 5:
      t_mes=1;
      break;
    case 6:
      t_mes=4;
      break;
    case 7:
      t_mes=6;
      break;
    case 8:
      t_mes=2;
      break;
    case 9:
      t_mes=5;
      break;
    case 10:
      t_mes=0;
      break;
    case 11:
      t_mes=3;
      break;
    case 12:
      t_mes=5;
      break;
    default:
      t_mes=t_mes;
    break;
 }
 
 r_dia=n_dia+t_mes+n_anno+d_anno+t_siglo;
 r_dia = r_dia % 7;
 
 switch (r_dia) {
    case 1:
       lcd.print("Lun");
        Serial.print(" Lun ");
      break;
      case 2:
       lcd.print("Mar");
       Serial.println(" Mar ");
      break;
      case 3:
       lcd.print("Mie");
       Serial.print(" Mie ");
      break;
      case 4:
       lcd.print("Jue");
       Serial.print(" Jue ");
      break;
      case 5:
       lcd.print("Vie");
       Serial.print(" Vie ");
      break;
      case 6:
       lcd.print("Sab");
       Serial.print(" Sab ");
      break;
      case 0:
       lcd.print("Dom");
       Serial.print(" Dom ");
      break;
      default:
       lcd.print("---");
       Serial.print(" ---");
      break;
 }
 return r_dia;
}
 
/////////////////////Condicional del Horario 1  //////////////////////////////////
void horario_1(){
   lcd.setCursor(13,0);
   lcd.print("1");
   Serial.print("Horario_1 ");
   if ((hora==h1_c1) && (minuto==m1_c1) && (segundo==s1_c1))  activar_timbre();
   if ((hora==h2_c1) && (minuto==m2_c1) && (segundo==s2_c1))  activar_timbre();
   if ((hora==h3_c1) && (minuto==m3_c1) && (segundo==s3_c1))  activar_timbre();
   if ((hora==h4_c1) && (minuto==m4_c1) && (segundo==s4_c1))  activar_timbre();
   if ((hora==h5_c1) && (minuto==m5_c1) && (segundo==s5_c1))  activar_timbre();
   if ((hora==h6_c1) && (minuto==m6_c1) && (segundo==s6_c1))  activar_timbre();
   if ((hora==h7_c1) && (minuto==m7_c1) && (segundo==s7_c1))  activar_timbre();
   if ((hora==h8_c1) && (minuto==m8_c1) && (segundo==s8_c1))  activar_timbre();
   if ((hora==h9_c1) && (minuto==m9_c1) && (segundo==s9_c1))  activar_timbre();
   if ((hora==h10_c1) && (minuto==m10_c1) && (segundo==s10_c1))  activar_timbre();
   if ((hora==h11_c1) && (minuto==m11_c1) && (segundo==s11_c1))  activar_timbre();
   if ((hora==h12_c1) && (minuto==m12_c1) && (segundo==s12_c1))  activar_timbre();
   if ((hora==h13_c1) && (minuto==m13_c1) && (segundo==s13_c1))  activar_timbre();
   if ((hora==h14_c1) && (minuto==m14_c1) && (segundo==s14_c1))  activar_timbre();
   if ((hora==h15_c1) && (minuto==m15_c1) && (segundo==s15_c1))  activar_timbre();
   if ((hora==h16_c1) && (minuto==m16_c1) && (segundo==s16_c1))  activar_timbre();
}
////////////////////Condicional del Horario 2  //////////////////////////////////
void horario_2(){
   lcd.setCursor(14,0);
   lcd.print("2");
   Serial.print("Horario_2 ");
   if ((hora==h1_c2) && (minuto==m1_c2) && (segundo==s1_c2))  activar_timbre();
   if ((hora==h2_c2) && (minuto==m2_c2) && (segundo==s2_c2))  activar_timbre();
   if ((hora==h3_c2) && (minuto==m3_c2) && (segundo==s3_c2))  activar_timbre();
   if ((hora==h4_c2) && (minuto==m4_c2) && (segundo==s4_c2))  activar_timbre();
   if ((hora==h5_c2) && (minuto==m5_c2) && (segundo==s5_c2))  activar_timbre();
   if ((hora==h6_c2) && (minuto==m6_c2) && (segundo==s6_c2))  activar_timbre();
   if ((hora==h7_c2) && (minuto==m7_c2) && (segundo==s7_c2))  activar_timbre();
   if ((hora==h8_c2) && (minuto==m8_c2) && (segundo==s8_c2))  activar_timbre();
   if ((hora==h9_c2) && (minuto==m9_c2) && (segundo==s9_c2))  activar_timbre();
   if ((hora==h10_c2) && (minuto==m10_c2) && (segundo==s10_c2))  activar_timbre();
   if ((hora==h11_c2) && (minuto==m11_c2) && (segundo==s11_c2))  activar_timbre();
   if ((hora==h12_c2) && (minuto==m12_c2) && (segundo==s12_c2))  activar_timbre();
   if ((hora==h13_c2) && (minuto==m13_c2) && (segundo==s13_c2))  activar_timbre();
   if ((hora==h14_c2) && (minuto==m14_c2) && (segundo==s14_c2))  activar_timbre();
   if ((hora==h15_c2) && (minuto==m15_c2) && (segundo==s15_c2))  activar_timbre();
   if ((hora==h16_c2) && (minuto==m16_c2) && (segundo==s16_c2))  activar_timbre();
 
}
//////////////////////Condicional del Horario 3  //////////////////////////////////
void horario_3(){
   lcd.setCursor(15,0);
   lcd.print("3");
   Serial.print("Horario_3 ");
   if ((hora==h1_c3) && (minuto==m1_c3) && (segundo==s1_c3))  activar_timbre();
   if ((hora==h2_c3) && (minuto==m2_c3) && (segundo==s2_c3))  activar_timbre();
   if ((hora==h3_c3) && (minuto==m3_c3) && (segundo==s3_c3))  activar_timbre();
   if ((hora==h4_c3) && (minuto==m4_c3) && (segundo==s4_c3))  activar_timbre();
   if ((hora==h5_c3) && (minuto==m5_c3) && (segundo==s5_c3))  activar_timbre();
   if ((hora==h6_c3) && (minuto==m6_c3) && (segundo==s6_c3))  activar_timbre();
   if ((hora==h7_c3) && (minuto==m7_c3) && (segundo==s7_c3))  activar_timbre();
   if ((hora==h8_c3) && (minuto==m8_c3) && (segundo==s8_c3))  activar_timbre();
   if ((hora==h9_c3) && (minuto==m9_c3) && (segundo==s9_c3))  activar_timbre();
   if ((hora==h10_c3) && (minuto==m10_c3) && (segundo==s10_c3))  activar_timbre();
   if ((hora==h11_c3) && (minuto==m11_c3) && (segundo==s11_c3))  activar_timbre();
   if ((hora==h12_c3) && (minuto==m12_c3) && (segundo==s12_c3))  activar_timbre();
   if ((hora==h13_c3) && (minuto==m13_c3) && (segundo==s13_c3))  activar_timbre();
   if ((hora==h14_c3) && (minuto==m14_c3) && (segundo==s14_c3))  activar_timbre();
   if ((hora==h15_c3) && (minuto==m15_c3) && (segundo==s15_c3))  activar_timbre();
   if ((hora==h16_c3) && (minuto==m16_c3) && (segundo==s16_c3))  activar_timbre();
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder