-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWi-Fire.ino
420 lines (357 loc) · 9.28 KB
/
Wi-Fire.ino
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#ifdef ARDUINO_AVR_MEGA2560 // Arduino MEGA
#define SD_CS_PIN 53
HardwareSerial &LoRa = Serial3;
#else // Arduino UNO
#define SD_CS_PIN 10
#include <SoftwareSerial.h>
SoftwareSerial LoRa(2, 3);
#endif // ARDUINO_AVR_MEGA2560
#define PROPER_BOARD 1 // Quando utilizar a placa construída para o sistema
#define USE_RGB (1 && PROPER_BOARD)
#define USE_BUTTONS (1 && PROPER_BOARD)
#define BuZZ (1)
#define BEEPING (BuZZ && 1)
#if USE_RGB
#define LED_ON 0
#define LED_R 32
#define LED_G 36
#define LED_B 34
#endif // USE_RGB
#if USE_BUTTONS
#define B_ON 1
#define B_B 24
#define B_R 26
#define B_G 28
#define B_Y 30
#endif // USE_BUTTONS
#if BuZZ
#define buzzPin 22 //Pin that the buzzer is connected
// #define buzzPin 13 //Pin that the buzzer is connected
#define buzzCmd LOW //Buzzer is on in high state
#endif // BuZZ
#if PROPER_BOARD
#define IGN A0
#else
#define IGN_1 36 /*act1*/
#define IGN_2 61 /*act2*/
#define IGN_3 46 /*act3*/
#define IGN_4 55 /*act4*/
#endif //PROPER_BOARD
#define ON_SIG 1
#define holdIGN 10
#define M0_LORA_PIN 12 // Pinos adicionais do LoRa
#define M1_LORA_PIN 11 // Pinos adicionais do LoRa
#define AUX_LORA_PIN A8 // Pinos adicionais do LoRa
int data = ' ';
int holder = ' ';
int pingCheck = 0;
bool pingResponse = false;
#if USE_BUTTONS
enum Buttons{button_blue=1, button_red=2, button_green=3, button_yellow=4};
const Buttons password[] = {button_blue, button_green, button_blue, button_blue, button_green};
int passwordIndex = 0;
const int passwordLen = sizeof(password)/sizeof(password[0]);
#endif // USE_BUTTONS
#if USE_RGB
enum Colors:int{white=0b111, magenta=0b101, blue=0b001, cyan=0b011, green=0b010, yellow=0b110, red=0b100, black=0b000};
#endif // USE_RGB
class Helpful {
private:
unsigned long conEach = 0;
unsigned long timerEach = 0;
unsigned long lapseT = 0;
unsigned long T0 = 0;
unsigned long endT = 0;
bool forTstate = 0;
bool one = 0;
unsigned long count = 0;
float maxi = 0, mini = 0;
public:
bool mem;
void begin();
unsigned long counter();
unsigned long getCount();
unsigned long counterReset();
bool eachN(unsigned int N);
bool eachT(float T);
void forT(float T);
bool forT();
float lapse();
float sinceBegin();
bool oneTime();
void oneTimeReset();
void comparer(float n);
float getMax();
float getMin();
};
Helpful ajuda;
#define holdT .1
#if BEEPING
Helpful beeper;
#endif // BEEPING
#define SYSTEM_n 5
#if USE_RGB
inline void rgb(bool r, bool g, bool b){
digitalWrite(LED_R, r);
digitalWrite(LED_G, g);
digitalWrite(LED_B, b);
}
inline void rgb(Colors color){
switch(color){
case white: rgb(LED_ON,LED_ON,LED_ON); break;
case magenta: rgb(LED_ON,!LED_ON,LED_ON); break;
case blue: rgb(!LED_ON,!LED_ON,LED_ON); break;
case cyan: rgb(!LED_ON,LED_ON,LED_ON); break;
case green: rgb(!LED_ON,LED_ON,!LED_ON); break;
case yellow: rgb(LED_ON,LED_ON,!LED_ON); break;
case red: rgb(LED_ON,!LED_ON,!LED_ON); break;
case black: rgb(!LED_ON,!LED_ON,!LED_ON); break;
}
}
#endif // USE_RGB
void setup() {
pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, LOW);
Serial.begin(9600);
LoRa.begin(9600);
pinMode(M0_LORA_PIN,OUTPUT); digitalWrite(M0_LORA_PIN,LOW);
pinMode(M1_LORA_PIN,OUTPUT); digitalWrite(M1_LORA_PIN,LOW);
// pinMode(AUX_LORA_PIN,OUTPUT); digitalWrite(AUX_LORA_PIN,LOW);
#if PROPER_BOARD
pinMode(IGN, OUTPUT); digitalWrite(IGN, !ON_SIG);
#else
pinMode(IGN_1, OUTPUT); digitalWrite(IGN_1, !ON_SIG);
pinMode(IGN_2, OUTPUT); digitalWrite(IGN_2, !ON_SIG);
pinMode(IGN_3, OUTPUT); digitalWrite(IGN_3, !ON_SIG);
pinMode(IGN_4, OUTPUT); digitalWrite(IGN_4, !ON_SIG);
#endif // PROPER_BOARD
#if USE_BUTTONS
pinMode(B_G, INPUT);
pinMode(B_R, INPUT);
pinMode(B_B, INPUT);
pinMode(B_Y, INPUT);
#endif //USE_BUTTONS
#if USE_RGB
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_B, OUTPUT);
#endif //USE_RGB
ajuda.begin();
#if BuZZ
pinMode(buzzPin, OUTPUT);
digitalWrite(buzzPin, !buzzCmd);
#endif // BuZZ
#if USE_RGB
rgb(white); delay(100); // Serial.println("Branco");
rgb(magenta); delay(100); // Serial.println("Magenta");
rgb(blue); delay(100); // Serial.println("Azul");
rgb(cyan); delay(100); // Serial.println("Ciano");
rgb(green); delay(100); // Serial.println("Verde");
rgb(yellow); delay(100); // Serial.println("Amarelo");
rgb(red); delay(100); // Serial.println("Vermelho");
rgb(black); delay(100); // Serial.println("Preto");
#endif // USE_RGB
#if BEEPING
beep();
#endif // BEEPING
}
void loop() {
if (LoRa.available()) {
while (LoRa.available()) {
data = LoRa.read();
Serial.write(data);
if (data == 'S') // Aciona a ignição
{
Serial.println("Ign!");
ajuda.forT(holdIGN);
holder = data;
pingCheck = -5;
} else if (data == '*') // Retorna estado do sinal
{
LoRa.print("T = ");
LoRa.println(ajuda.sinceBegin());
pingCheck = 3;
pingResponse = true;
} else if (data == 'T') // Retorna estado do sinal
{
pingCheck = 3;
}
}
}
if (Serial.available()) {
LoRa.write(Serial.read());
digitalWrite(LED_BUILTIN, HIGH);
} else digitalWrite(LED_BUILTIN, LOW);
#if USE_BUTTONS
/* Botão Vermelho */
if (digitalRead(B_R) == B_ON) {
Serial.println("B_R");
#if USE_RGB
rgb(white);
#endif //USE_RGB
while (digitalRead(B_R) == B_ON);
if(passwordIndex == passwordLen) {
LoRa.write('S');
Serial.print("Enviou: S");
passwordIndex = 0;
}
if(password[passwordIndex] == button_red) passwordIndex++;
else passwordIndex = 0;
}
/* Botão Azul */
if (digitalRead(B_B) == B_ON) {
Serial.println("B_B");
#if USE_RGB
rgb(white);
#endif // USE_RGB
while (digitalRead(B_B) == B_ON);
if(password[passwordIndex] == button_blue) passwordIndex++;
else passwordIndex = 0;
}
/* Botão Verde */
if (digitalRead(B_G) == B_ON) {
Serial.println("B_G");
#if USE_RGB
rgb(white);
#endif // USE_RGB
while (digitalRead(B_G) == B_ON);
if(password[passwordIndex] == button_green) passwordIndex++;
else passwordIndex = 0;
}
/* Botão Amarelo */
if (digitalRead(B_Y) == B_ON) {
Serial.println("B_Y");
#if USE_RGB
rgb(white);
#endif // USE_RGB
while (digitalRead(B_Y) == B_ON);
if(password[passwordIndex] == button_yellow) passwordIndex++;
else passwordIndex = 0;
LoRa.write('*');
Serial.println("Enviou: *");
}
#endif // USE_BUTTONS
if(passwordIndex == passwordLen) pingCheck = 2;
#if USE_RGB
if(passwordIndex == passwordLen) // Estado armado
rgb(red);
else if(passwordIndex>0) // Senha em percurso
rgb(yellow);
else if(pingCheck > 0) // Ping com resposta positiva
rgb(pingResponse?cyan:green);
else if(ajuda.forT()) // Estado de acionamento
rgb(magenta);
else // Estado normal
{
rgb(blue);
pingResponse = false;
}
#endif // USE_RGB
if (!ajuda.forT()) holder = ' ';
#if PROPER_BOARD
digitalWrite(IGN, holder == 'S' ? ON_SIG : !ON_SIG);
#else
digitalWrite(IGN_1, holder == 'S' ? ON_SIG : !ON_SIG);
digitalWrite(IGN_2, holder == 'S' ? ON_SIG : !ON_SIG);
digitalWrite(IGN_3, holder == 'S' ? ON_SIG : !ON_SIG);
digitalWrite(IGN_4, holder == 'S' ? ON_SIG : !ON_SIG);
#endif // PROPER_BOARD
// digitalWrite(buzzPin, holder == 'S' ? ON_SIG : !ON_SIG);
// if(holder == 'S') Serial.println("ON_SIG");
#if BEEPING
if(pingCheck != 0){
if(beep(pingCheck > 0?3:1)) pingCheck=pingCheck+((pingCheck > 0) ? (-1) : (1));
}
else beep();
#endif // BEEPING
}
///Coleaao de utilitarios
void Helpful::begin() {
T0 = micros();
}
unsigned long Helpful::counter() {
return count++;
}
unsigned long Helpful::getCount() {
return count;
}
unsigned long Helpful::counterReset() {
unsigned long C = count;
count = 0;
return C;
}
bool Helpful::eachN(unsigned int N) {
conEach++;
return (N == 0) ? false : conEach % N == 0;
}
bool Helpful::eachT(float T) {
if (micros() - timerEach > (unsigned long)(T * 1000000.0)) {
timerEach = micros();
return true;
}
return false;
}
void Helpful::forT(float T) {
endT = micros() + (long)(T * 1000000.0);
forTstate = 1;
}
bool Helpful::forT() {
if (micros() > endT && forTstate) {
endT = 0;
return forTstate = 0;
}
return forTstate;
}
float Helpful::lapse() {
unsigned long tnow = micros(), t = tnow - lapseT;
lapseT = tnow;
return (float)t / 1000000.0;
}
float Helpful::sinceBegin() {
return float(micros() - T0) / 1000000.0;
}
bool Helpful::oneTime() {
if (!one) {
one = 1;
return true;
}
return false;
}
void Helpful::oneTimeReset() {
if (one) one = 0;
}
void Helpful::comparer(float n) {
maxi = (n > maxi) ? n : maxi;
mini = (n < mini) ? n : mini;
}
float Helpful::getMax() {
return maxi;
}
float Helpful::getMin() {
return mini;
}
#if BEEPING
inline bool beep(unsigned int N)
{
if (beeper.eachT(holdT*SYSTEM_n * 4) || beeper.oneTime())
{
beeper.mem = buzzCmd;
beeper.counterReset();
beeper.forT(holdT);
return true;
}
if (beeper.getCount() < (N + 1) * 2) if (!beeper.forT())
{
digitalWrite(buzzPin, beeper.mem);
beeper.mem = !beeper.mem;
beeper.counter();
beeper.forT(holdT);
}
return false;
}
inline bool beep()
{
digitalWrite(buzzPin, !buzzCmd);
beeper.counterReset();
return false;
}
#endif // BEEPING