-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDATE_FLx3P.ino
583 lines (546 loc) · 26 KB
/
DATE_FLx3P.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#include <Arduino.h>
#include <OneButton.h>
#include <arduino-timer.h>
#include <EEPROM.h>
#define ARRAY_SIZE(array) ((sizeof(array))/(sizeof(array[0])))
auto autoOffTimerAfterDisplay = timer_create_default();
auto blinkTimer = timer_create_default();
int oHighBeam = 3; // Output high beam
int oLowBeam = 4; // Output low beam
int oYRL = A4;// Output circle YELLOW light
int oDRL = 2; // Output circle WHITE light
int oLedRED = 12; // Output RED led button small circle
int oLedGREEN = 13; // Output GREEN led button small circle
int oLedBLUE = A5; // Output BLUE led button small circle
int oSecondaryLight = 6; // Output secondary light on double tap
int oPoliceLights = 7; // Output on triple tap police lights / red+blue / red + red / blue + blue
int oTurnLeft = 8;//1102; // Output separate fluid left turn signal front side
int oTurnRight = 10;//1104; // Output separate fluid right turn signal front side
int oPositionLeft = 9; // Output separate fluid left turn signal rear side "position light"
int oPositionRight = 11; // Output separate fluid right turn signal rear side "position light"
int oLoudHorn = 5; // Output loud horn
int iSmartButton = A2; // Input switch rgb button "one touch"
int iTurnLeft = A7; // Input left turn signal
int iTurnRight = A6; // Input right turn signal
int iOriginalLightOn = A3; // Input original light
int iHorn = A1; // Input horn
int iDisplayOn = A0; // Input "display on"
enum HeadLightState { ManualOff, AutoOff, ManualOn, AutoOn };
HeadLightState headLightState = ManualOff;
int lastDisplayOnState;
int turnSignalIntervalOn = 550; //flash turn signal every ms
int turnSignalIntervalOff = 350; //wait duration between two turn signals
int policeLightsIntervalOn = 3000; //flash turn signal every ms
int policeLightsIntervalOff = 0; //wait duration between two turn signals
bool prevOriginalLightLowBeamState = false;
bool prevOriginalLightState = false;
bool prevTurnLeftState = false;
bool prevTurnRightState = false;
//how many flashes we still do (initally 1 = one flash when turns on)
int shouldStillBlinkLeftTurnSignal = 2, shouldStillBlinkRightTurnSignal = 2;
unsigned long ShutDownAfterDisplayInSecondsAfterDisplayTurnsOff = 3; //should be 10s. auto off when the display is turned off
unsigned long TurnOffAfterSecondsEvenWhenDisplayIsStillOn = 1200; //auto off when no manual comands (button or turn signals)
unsigned long sinceLastCommand; //last millis date/time of the last command (when we pushed the one touch button / turn signals)
//short blinks every 5 sec flashes
unsigned long lastBlink; //the millis when we blinked last time (for every 5s flashes)
unsigned long lastBlinkState = 0; //the sate we had last time (for every 5s flashes, so we can flash multiple times)
unsigned long blinkEveryMs = 5000;//every x ms to blink (for every 5s flashes e.g. 5000ms for 5s)
unsigned long blinkForMs = 40; //the duratio of y ms to blink (for every 5s flashes)
unsigned long blinkXTimes = 4; //how many times we should blink (for every 5s flashes)
//store the YRL / DRL state to be saved/restored automatically
enum State { None = 0, AutoRestore = 1, LowBeam = 2, HighBeam = 4, DRL = 8, YRL = 16, DRLFirstTap = 32, SecondaryLight = 64, WLED = 128, YRLFlash = 256, Turn3x = 512, DisableHorn = 1024, DisableTurnSignalSound = 2048};
enum Blink { TurnSignalsOff = 0, TurnLeft = 1, TurnRight = 2, PositionTurnLeftShouldBeOff = 4, PositionTurnRightShouldBeOff = 8, BlinkFlash = 16, BlinkFlashYRL = 32 };
enum Police { PoliceLigthsOff = 0, PoliceLights = 1 };
State currentState = (State)(WLED | DRLFirstTap | YRLFlash | Turn3x | AutoRestore);
Blink currentBlink = (Blink)(TurnSignalsOff);
Police currentPolice = (Police)(PoliceLigthsOff);
int stateEEPROMAddress = 2;//the address where we save the current state
OneButton button = OneButton(iSmartButton, false, false);
bool blinkPoliceLightsOn(void *argument /* optional argument given to in/at/every */) {
if ((currentPolice & PoliceLights) > 0) {
digitalWrite(oPoliceLights, digitalRead(oPoliceLights) == HIGH ? LOW : HIGH);
}
}
bool blinkPoliceLightsOff(void *argument /* optional argument given to in/at/every */) {
//digitalWrite(oPoliceLights, LOW);
}
bool blinkOn(void *argument /* optional argument given to in/at/every */) {
currentBlink = (Blink)(currentBlink & ~PositionTurnLeftShouldBeOff & ~PositionTurnRightShouldBeOff);
bool shouldTurnLeft = analogRead(iTurnLeft) > 200, shouldTurnRight = analogRead(iTurnRight) > 200;
if (shouldTurnRight) { shouldStillBlinkLeftTurnSignal = 0; }
if (shouldTurnLeft) { shouldStillBlinkRightTurnSignal = 0; }
if ((currentState & Turn3x) != 0) {
if (!shouldTurnLeft && prevTurnLeftState && !shouldTurnRight) { shouldStillBlinkLeftTurnSignal = 3; }
if (!shouldTurnRight && prevTurnRightState && !shouldTurnLeft) { shouldStillBlinkRightTurnSignal = 3; }
}
if (shouldTurnLeft || shouldStillBlinkLeftTurnSignal > 0 && shouldStillBlinkLeftTurnSignal-- > 0) {
currentBlink = (Blink)(currentBlink | TurnLeft);
}
if (shouldTurnRight || shouldStillBlinkRightTurnSignal > 0 && shouldStillBlinkRightTurnSignal-- > 0) {
currentBlink = (Blink)(currentBlink | TurnRight);
}
// if (!(currentState & DisableTurnSignalSound)) && (shouldTurnLeft || shouldTurnRight)) {
// digitalWrite(oLoudHorn, HIGH);
// delay(2);
// digitalWrite(oLoudHorn, LOW);
// delay(1);
// digitalWrite(oLoudHorn, HIGH);
// delay(1);
// digitalWrite(oLoudHorn, LOW);
// }
applyState();
prevTurnLeftState = shouldTurnLeft; prevTurnRightState = shouldTurnRight;
return true; // to repeat the action - false to stop
}
bool blinkOff(void *argument /* optional argument given to in/at/every */) {
if ((currentBlink & TurnLeft) > 0) {
currentBlink = (Blink)(currentBlink | PositionTurnLeftShouldBeOff);
}
if ((currentBlink & TurnRight) > 0) {
currentBlink = (Blink)(currentBlink | PositionTurnRightShouldBeOff);
}
currentBlink = (Blink)(currentBlink & ~TurnLeft & ~TurnRight);
bool shouldTurnLeft = analogRead(iTurnLeft) > 200, shouldTurnRight = analogRead(iTurnRight) > 200;
if (!(currentState & DisableTurnSignalSound) && (shouldTurnLeft || shouldTurnRight)) {
// digitalWrite(oLoudHorn, HIGH);
// delay(2);
// digitalWrite(oLoudHorn, LOW);
}
applyState();
return true; // to repeat the action - false to stop
}
void digitalWritePins(int state, int p0=0,int p1=0, int p2=0, int p3=0, int p4=0, int p5=0, int p6=0, int p7=0, int p8=0, int p9=0, int p10=0, int p11=0, int p12=0) {
int pins[] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12};
for(int i=ARRAY_SIZE(pins);--i>=0;) if (pins[i] != 0) { digitalWrite(pins[i], state); }
}
void digitalWriteStateToPins(int pins[]) { for(int i=0;i<ARRAY_SIZE(pins);i+=2) if (pins[i] != 0) { digitalWrite(pins[i], pins[i+1]); } }
void digitalWriteStateToPins(int p0=0, int v0=0, int p1=0, int v1=0, int p2=0, int v2=0, int p3=0, int v3=0, int p4=0, int v4=0,
int p5=0, int v5=0, int p6=0, int v6=0, int p7=0, int v7=0, int p8=0, int v8=0, int p9=0, int v9=0,
int p10=0, int v10=0, int p11=0, int v11=0, int p12=0, int v12=0) {
int pins[]={p0, v0, p1, v1, p2, v2, p3, v3, p4, v4, p5, v5, p6, v6, p7, v7, p8, v8, p9, v9, p10, v10, p11, v11, p12, v12};
for(int i=0;i<ARRAY_SIZE(pins);i+=2) if (pins[i] != 0) { digitalWrite(pins[i], pins[i+1]); }
}
bool blinkLoop(unsigned long now) {
switch(lastBlinkState%2) {
case 0://blink off
if (lastBlinkState == 0 && now >= lastBlink + blinkEveryMs
|| lastBlinkState > 0 && now >= lastBlink + blinkForMs) {
lastBlink = now;
lastBlinkState++;
currentBlink = (Blink)(currentBlink | BlinkFlash | BlinkFlashYRL);
applyState();
return true;
}
return false;
break;
case 1://blink on
if (now > lastBlink + blinkForMs)
{
lastBlink = now;
lastBlinkState++;
bool completed = lastBlinkState >= blinkXTimes * 2 - 1;
currentBlink = (Blink)(currentBlink & ~BlinkFlashYRL);
if (completed) {
currentBlink = (Blink)(currentBlink & ~BlinkFlash);
lastBlinkState = 0;
applyState();
return false;
} else {
applyState();
}
}
return true;
break;
}
}
void saveState(){
int state = (int)currentState, oldState = readSavedState();
if (oldState != state) {
EEPROM.write(stateEEPROMAddress, state % 256);
EEPROM.write(stateEEPROMAddress + 1, state / 256);
}
}
State readSavedState() {
State readState = (State)(EEPROM.read(stateEEPROMAddress) % 256 | (EEPROM.read(stateEEPROMAddress + 1) * 256));
return readState;
}
void restoreState() {
currentState = readSavedState();
if ((currentState & AutoRestore) != 0) { //auto-save & restore state
digitalWrite(oLowBeam, (currentState & LowBeam) != 0 ? HIGH : LOW);
digitalWrite(oHighBeam, (currentState & HighBeam) != 0 ? HIGH : LOW);
digitalWrite(oDRL, (currentState & DRL) != 0 ? HIGH : LOW);
digitalWrite(oYRL, (currentState & YRL) != 0 ? HIGH : LOW);
digitalWrite(oSecondaryLight, (currentState & SecondaryLight) != 0 ? HIGH : LOW);
headLightState = (currentState & (LowBeam | HighBeam | DRL | YRL)) != 0 ? AutoOn : AutoOff;
if ((currentState & Turn3x) != 0) {
shouldStillBlinkLeftTurnSignal = shouldStillBlinkRightTurnSignal = 2;
}
updateLedLight();
}
sinceLastCommand = millis();
}
bool checkAutoOff() {
unsigned long now = millis();
if (now >= sinceLastCommand + TurnOffAfterSecondsEvenWhenDisplayIsStillOn * 1000
&& (headLightState == ManualOn || headLightState == AutoOn)) {
saveState();
longTap();
lastBlinkState = 0;
headLightState = AutoOff; //auto off
return true;
}
if ((currentState & YRLFlash) > 0 && blinkLoop(now)) {
return true;
}
return false;
}
void updateLedLight(){
int turnLeft = digitalRead(oTurnLeft) == HIGH, turnRight = digitalRead(oTurnRight) == HIGH;
if (turnLeft) {
digitalWriteStateToPins(oLedRED, HIGH, oLedGREEN, LOW, oLedBLUE, LOW);
}
if (turnRight) {
digitalWriteStateToPins(oLedRED, HIGH, oLedGREEN, LOW, oLedBLUE, LOW);
}
if (turnLeft || turnRight) { }
else if (digitalRead(oLoudHorn) == HIGH) {
digitalWriteStateToPins(oLedRED, HIGH, oLedGREEN, LOW, oLedBLUE, LOW, oYRL, HIGH, oPositionLeft, HIGH, oPositionRight, HIGH);
} else if (digitalRead(oHighBeam) == HIGH) {
digitalWriteStateToPins(oLedRED, LOW, oLedGREEN, digitalRead(oSecondaryLight), oLedBLUE, HIGH);
}
else if (digitalRead(oLowBeam) == HIGH) {
digitalWriteStateToPins(oLedRED, digitalRead(oSecondaryLight), oLedGREEN, HIGH, oLedBLUE, LOW);
}
else if (digitalRead(oSecondaryLight) == HIGH) {
digitalWriteStateToPins(oLedRED, HIGH, oLedGREEN, HIGH, oLedBLUE, LOW);
}
else {
if ((currentState & WLED) != 0) {
digitalWriteStateToPins(oLedRED, HIGH, oLedGREEN, HIGH, oLedBLUE, HIGH);
} else {
digitalWriteStateToPins(oLedRED, LOW, oLedGREEN, LOW, oLedBLUE, LOW);
}
}
}
void applyState(){
bool isOn = (headLightState != AutoOff && headLightState != ManualOff);
bool turnLeft = (currentBlink & TurnLeft) > 0;
bool turnRight = (currentBlink & TurnRight) > 0;
if (!isOn && shouldStillBlinkLeftTurnSignal <= 0 && shouldStillBlinkRightTurnSignal <= 0 && (!(turnLeft || turnRight)) ) {
digitalWritePins(LOW, oHighBeam, oLowBeam, oYRL, oDRL, oSecondaryLight, oTurnLeft, oTurnRight, oPositionLeft, oPositionRight, oLoudHorn);
}
else {
bool positionTurnLeftShouldBeOff = (currentBlink & PositionTurnLeftShouldBeOff) > 0;
bool positionTurnRightShouldBeOff = (currentBlink & PositionTurnRightShouldBeOff) > 0;
bool positionShouldBeOn = isOn
&& ((currentState & DRL) != 0 || (currentState & YRL) != 0 || (currentState & LowBeam) != 0 || (currentState & HighBeam) != 0);
bool shouldBlink = (currentBlink & BlinkFlash) > 0;
bool shouldBlinkYRL = (currentBlink & BlinkFlashYRL) > 0;
if (turnLeft || turnRight || positionTurnLeftShouldBeOff || positionTurnRightShouldBeOff) {
digitalWriteStateToPins(oYRL, turnLeft || turnRight ? HIGH : LOW,
oDRL, (currentState & DRL) != 0 ? HIGH : LOW,
oLowBeam, (currentState & LowBeam) != 0 ? HIGH : LOW,
oSecondaryLight, (currentState & SecondaryLight) != 0 ? HIGH : LOW,
oHighBeam, (currentState & HighBeam) != 0 ? HIGH : LOW,
oPositionLeft, positionTurnLeftShouldBeOff ? LOW : (turnLeft || positionShouldBeOn ? HIGH : LOW),
oTurnLeft, turnLeft ? HIGH : LOW,
oPositionRight, positionTurnRightShouldBeOff ? LOW : (turnRight || positionShouldBeOn ? HIGH : LOW),
oTurnRight, turnRight ? HIGH : LOW
);
}
else if (shouldBlink) {// && positionShouldBeOn) {
//Serial.printf("isOn %d, currentBlink %d, shouldBlink %d, shouldBlinkYRL %d, turnLeft %d, turnRight %d, positionTurnLeftShouldBeOff %d, positionTurnRightShouldBeOff %d\n", isOn, currentBlink, shouldBlink, shouldBlinkYRL, turnLeft, turnRight, positionTurnLeftShouldBeOff, positionTurnRightShouldBeOff);
digitalWriteStateToPins(oYRL, shouldBlinkYRL ? HIGH : LOW,
oPositionLeft, shouldBlinkYRL ? HIGH : LOW,
oTurnLeft, LOW, oTurnRight, LOW,
oPositionRight, shouldBlinkYRL ? HIGH : LOW);
} else {
digitalWriteStateToPins(oDRL, (currentState & DRL) != 0 ? HIGH : LOW,
oLowBeam, (currentState & LowBeam) != 0 ? HIGH : LOW,
oHighBeam, (currentState & HighBeam) != 0 ? HIGH : LOW,
oYRL, (currentState & YRL) != 0 ? HIGH : LOW,
oSecondaryLight, (currentState & SecondaryLight) != 0 ? HIGH : LOW,
oPositionLeft, positionShouldBeOn ? HIGH : LOW,
oTurnLeft, LOW, oTurnRight, LOW,
oPositionRight, positionShouldBeOn ? HIGH : LOW);
}
}
updateLedLight();
}
void doubleTap() {
headLightState = ManualOn;
sinceLastCommand = millis();
int shouldTurnOnSecondaryLight = digitalRead(oSecondaryLight) != HIGH;
if (shouldTurnOnSecondaryLight) {
currentState = (State)(currentState | SecondaryLight);//turn on SecondaryLight flag
if ((currentState & Turn3x) != 0) {
shouldStillBlinkLeftTurnSignal = shouldStillBlinkRightTurnSignal = 2;
}
digitalWritePins(HIGH, oSecondaryLight);
}
else {
currentState = (State)(currentState & ~SecondaryLight);//turn off SecondaryLight flag
if ((currentState & Turn3x) != 0) {
shouldStillBlinkLeftTurnSignal = shouldStillBlinkRightTurnSignal = 1;
}
digitalWritePins(LOW, oSecondaryLight);
}
saveState();
updateLedLight();
}
void singleTap() {
headLightState = ManualOn;
sinceLastCommand = millis();
int shouldHighBeam = (currentState & LowBeam) != 0 && (currentState & HighBeam) == 0;
if (shouldHighBeam) {
currentState = (State)(currentState | HighBeam | LowBeam);//turn on HighBeam | LowBeam flag
if ((currentState & Turn3x) != 0) {
shouldStillBlinkLeftTurnSignal = shouldStillBlinkRightTurnSignal = 1;
}
}
else {
if ((currentState & LowBeam) == 0 && (currentState & HighBeam) == 0 && (currentState & DRL) == 0) {
if ((currentState & DRLFirstTap) != 0) {
currentState = (State)(currentState & ~YRL);//turn off YRL flag
currentState = (State)(currentState | DRL);//turn on DRL flag
}
else {
currentState = (State)(currentState & ~HighBeam);//turn off HighBeam flag
currentState = (State)(currentState | LowBeam);//turn on LowBeam flag
}
if ((currentState & Turn3x) != 0) {
//blink once the turn signals when you turn it on, for the first time
shouldStillBlinkLeftTurnSignal = 1;
shouldStillBlinkRightTurnSignal = 1;
}
}
else {
currentState = (State)(currentState & ~HighBeam);//turn off HighBeam flag
currentState = (State)(currentState | LowBeam);//turn on LowBeam flag
if ((currentState & Turn3x) != 0) {
shouldStillBlinkLeftTurnSignal = shouldStillBlinkRightTurnSignal = 2;
}
}
}
saveState();
applyState();
}
void multipleTap()
{
sinceLastCommand = millis();
int clicks = button.getNumberClicks();
if (clicks != 3) {
headLightState = ManualOn;
}
switch (clicks) {
case 3: {
///3* pressing three times on the button when the headlight is off, will turn on only Secondary Lights.
currentPolice = (Police)(currentPolice ^ PoliceLights);
digitalWrite(oPoliceLights, digitalRead(oPoliceLights) == HIGH ? LOW : HIGH);
//digitalWrite(oPoliceLights, HIGH);
}
break;
case 4: {
///4* Toggle the white circle ON/OFF. Toggles the yellow circle ON/OFF
///OFF -> WHITE -> YELLOW -> OFF
int shouldDRL = digitalRead(oDRL) == HIGH;
int shouldYRL = digitalRead(oYRL) == HIGH;
if (shouldYRL) {
currentState = (State)(currentState & ~(YRL|DRL));//turn off YRL and DRL flags
digitalWritePins(LOW, oDRL, oYRL);
shouldStillBlinkLeftTurnSignal = shouldStillBlinkRightTurnSignal = 1;
}
else if (shouldDRL) {
currentState = (State)(currentState & ~DRL);//turn off DRL flag
currentState = (State)(currentState | YRL);//turn on YRL flag
digitalWritePins(LOW, oDRL);
digitalWritePins(HIGH, oYRL);
shouldStillBlinkLeftTurnSignal = shouldStillBlinkRightTurnSignal = 3;
}
else {
currentState = (State)(currentState & ~YRL);//turn off YRL flag
currentState = (State)(currentState | DRL);//turn on DRL flag
digitalWritePins(HIGH, oDRL);
digitalWritePins(LOW, oYRL);
shouldStillBlinkLeftTurnSignal = shouldStillBlinkRightTurnSignal = 2;
}
updateLedLight();
saveState();
break;
}
case 5: {
///4* pressing once on the button when the headlight is off, will turn on only the DRL.
///This can be useful during the day when you don't want to consume power with the low beam.
///Disabling this feature will skip the DRL step and start the low beam directly
currentState = (State)(currentState ^ DRLFirstTap);
saveState();
shouldStillBlinkLeftTurnSignal = shouldStillBlinkRightTurnSignal = (currentState & DRLFirstTap) != 0 ? 2 : 1;
}
break;
case 6: {
///5* The One Touch button is always "ON" as long as the battery key is ON, especially useful
/// to see where the button is in the dark or e.g. when you suddenly enter a tunnel.
/// However, at times, this might not be useful, so you could turn this feature off
currentState = (State)(currentState ^ WLED);
saveState();
shouldStillBlinkLeftTurnSignal = shouldStillBlinkRightTurnSignal = (currentState & WLED) != 0 ? 2 : 1;
}
break;
case 7: {
///6* The headlamp flashes the yellow circle every 5 seconds as the car drivers mostly notice
///the changes in their landscape. This is a safety feature. However it could be considered
///annoying to some, and hence it can be easily toggled when needed.
currentState = (State)(currentState ^ YRLFlash);
saveState();
shouldStillBlinkLeftTurnSignal = shouldStillBlinkRightTurnSignal = (currentState & YRLFlash) != 0 ? 2 : 1;
}
break;
case 8: {
///8* the turn signals will have an extra 3 blinkers after you stop them. This can be very helpful
/// as you turn them on and quickly off but the other participants can still see your intention.
///Many cars have this feature, however, it can be turned off when not desired.
///Disabling this also disables the yellow circle/turn signals flash upon turning ON/OFF
currentState = (State)(currentState ^ Turn3x);
saveState();
shouldStillBlinkLeftTurnSignal = shouldStillBlinkRightTurnSignal = (currentState & Turn3x) != 0 ? 3 : 1;
}
break;
case 9: {
///9* when the AUTO ON/OFF is turned on, the headlight state is automatically saved/restored when
/// the display turns on/off. This includes all the states
///Turning off this feature means every time you need to manually turn on/off the headlight,
///except the 6-9 settings which are still saved
currentState = (State)(currentState ^ AutoRestore);
saveState();
shouldStillBlinkLeftTurnSignal = shouldStillBlinkRightTurnSignal = (currentState & AutoRestore) != 0 ? 4 : 1;
}
break;
case 10: {
currentState = (State)(currentState ^ DisableHorn);
saveState();
shouldStillBlinkLeftTurnSignal = shouldStillBlinkRightTurnSignal = (currentState & DisableHorn) == DisableHorn ? 2 : 1;
}
break;
case 11: {
currentState = (State)(currentState ^ DisableTurnSignalSound);
shouldStillBlinkLeftTurnSignal = shouldStillBlinkRightTurnSignal = (currentState & DisableTurnSignalSound) == DisableTurnSignalSound ? 2 : 1;
saveState();
}
break;
}
}
void longTap() {
saveState();
sinceLastCommand = millis() + 365 * 24 * 60 * 60 * 1000;
headLightState = ManualOff; //manual off
currentState = (State)(currentState & ~(LowBeam | HighBeam | DRL | YRL | SecondaryLight));//turn off LowBeam, HighBeam, DRL, YRL, SecondaryLight flag
if ((currentState & Turn3x) != 0) {
shouldStillBlinkLeftTurnSignal = 2;
shouldStillBlinkRightTurnSignal = 2;
}
//turn off everything//digitalWritePins(LOW, oHighBeam, oLowBeam, oYRL, oDRL, oSecondaryLight, oPoliceLights, oTurnLeft, oTurnRight, oPositionLeft, oPositionRight, oLoudHorn);
applyState();
}
bool autoTurnOff(void *argument /* optional argument given to in/at/every */) {
autoOffTimerAfterDisplay.cancel();
longTap();
headLightState = AutoOff; //manual off
}
void checkDisplayState(){
int displayOnState = analogRead(iDisplayOn) > 200;
if (displayOnState != lastDisplayOnState) {
lastDisplayOnState = displayOnState;
if (displayOnState){
restoreState();
autoOffTimerAfterDisplay.cancel();
}
else {
if (headLightState == ManualOn || headLightState == AutoOn) {
saveState();
}
if ((currentState & AutoRestore) != 0) {
autoOffTimerAfterDisplay.cancel();
autoOffTimerAfterDisplay.in(ShutDownAfterDisplayInSecondsAfterDisplayTurnsOff * 1000, autoTurnOff);
}
}
}
}
void setup() {
//Serial.begin(9600); // Input pins
pinMode(iSmartButton, INPUT); pinMode(iTurnLeft, INPUT); pinMode(iTurnRight, INPUT); pinMode(iHorn, INPUT); pinMode(iDisplayOn, INPUT);
// Output pins
pinMode(oHighBeam, OUTPUT); digitalWrite(oHighBeam, LOW);
pinMode(oLowBeam, OUTPUT); digitalWrite(oLowBeam, LOW);
pinMode(oYRL, OUTPUT); digitalWrite(oYRL, LOW);
pinMode(oDRL, OUTPUT); digitalWrite(oDRL, LOW);
pinMode(oLedRED, OUTPUT); digitalWrite(oLedRED, LOW);
pinMode(oLedGREEN, OUTPUT); digitalWrite(oLedGREEN, LOW);
pinMode(oLedBLUE, OUTPUT); digitalWrite(oLedBLUE, LOW);
pinMode(oSecondaryLight, OUTPUT); digitalWrite(oSecondaryLight, LOW);
pinMode(oPoliceLights, OUTPUT); digitalWrite(oPoliceLights, LOW);
pinMode(oTurnLeft, OUTPUT); digitalWrite(oTurnLeft, LOW);
pinMode(oTurnRight, OUTPUT); digitalWrite(oTurnRight, LOW);
pinMode(oPositionLeft, OUTPUT); digitalWrite(oPositionLeft, LOW);
pinMode(oPositionRight, OUTPUT); digitalWrite(oPositionRight, LOW);
pinMode(oLoudHorn, OUTPUT); digitalWrite(oLoudHorn, LOW);
updateLedLight();
button.attachDoubleClick(doubleTap); button.attachClick(singleTap);
button.attachLongPressStop(longTap); button.attachMultiClick(multipleTap);
sinceLastCommand = millis();
int previousSavedState = (int)readSavedState();
if (previousSavedState == 0 ) {
saveState();
} else {
currentState = (State)(previousSavedState & (WLED | DRLFirstTap | YRLFlash | Turn3x | AutoRestore));
applyState();
}
blinkTimer.every(turnSignalIntervalOn + turnSignalIntervalOff, blinkOff);
blinkTimer.every(policeLightsIntervalOn + policeLightsIntervalOff, blinkPoliceLightsOn);
delay(turnSignalIntervalOff);
blinkTimer.every(turnSignalIntervalOn + turnSignalIntervalOff, blinkOn);
//delay(policeLightsIntervalOff - turnSignalIntervalOff);
//blinkTimer.every(policeLightsIntervalOn + policeLightsIntervalOff, blinkPoliceLightsOff);
}
void checkHornState() {
if (analogRead(iHorn) > 200 && (currentState & DisableHorn) == 0) {
digitalWrite(oLoudHorn, HIGH);
}
else {
digitalWrite(oLoudHorn, LOW);
}
}
void checkOriginalLigthState(){
if (analogRead(iOriginalLightOn) > 200) {
if (!prevOriginalLightState) {
prevOriginalLightState = true;
prevOriginalLightLowBeamState = (currentState & LowBeam) > 0;
currentState = (State)(currentState | LowBeam);
applyState();
}
}
else {
if (prevOriginalLightState) {
prevOriginalLightState = false;
if (!prevOriginalLightLowBeamState) {
currentState = (State)(currentState && ~LowBeam);
}
applyState();
}
}
}
void loop() {
checkHornState();
//checkOriginalLigthState();
checkDisplayState();
button.tick();
blinkTimer.tick();
switch (headLightState)
{
case AutoOn:
case ManualOn:
if (checkAutoOff()) return;
break;
}
autoOffTimerAfterDisplay.tick();
}