-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mainlights.ino
202 lines (175 loc) · 5.52 KB
/
Mainlights.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
#include <Arduino.h>
#include "avr/sleep.h"
#define DIPLIGHT 8 //output dip lights
#define PARKLIGHT 9 //output park lights
#define FOGLIGHT 11 //output fog lights
#define REARLIGHT 12 //output rear lights
#define MAINLIGHT 13 //output main/beam
#define BTN_FOGLIGHT 4 //button fog lights
#define BTN_MAINLIGHT 5 //button main/beam
#define BTN_DIPLIGHT 6 //button dip lights
#define BTN_PARKLIGHT 7 //input park lights (switched by key)
#define ENGINE_ON 3 //interrupt signal pin
#define DELAY 100 //delay time [ms]
#define MODULO 5 //corresponds to delay and loop. e.g. flash every 500 ms (100 * 5)
#define SLEEP_TIME 120000 //delay before Atmega goes to sleep [2 minutes]
int currentState = 0;
int fogState = 0;
long time = 0;
long powerOffTime = 0;
int enableSleep = 0;
unsigned int count = 0;
void handleLightState(int state) {
switch(state) {
case 1://lights on, fog off, main off
digitalWrite(FOGLIGHT, LOW);
digitalWrite(REARLIGHT, HIGH);
digitalWrite(DIPLIGHT, HIGH);
digitalWrite(MAINLIGHT, LOW);
break;
case 2://lights on, fog off, main on
digitalWrite(FOGLIGHT, LOW);
digitalWrite(REARLIGHT, HIGH);
digitalWrite(DIPLIGHT, LOW);
digitalWrite(MAINLIGHT, HIGH);
break;
case 3://lights with fog on
digitalWrite(FOGLIGHT, HIGH);
digitalWrite(REARLIGHT, HIGH);
digitalWrite(DIPLIGHT, HIGH);
digitalWrite(MAINLIGHT, LOW);
break;
case 4://main lights with fog on
digitalWrite(FOGLIGHT, HIGH);
digitalWrite(REARLIGHT, HIGH);
digitalWrite(DIPLIGHT, LOW);
digitalWrite(MAINLIGHT, HIGH);
break;
case 5: //only main lights high
digitalWrite(FOGLIGHT, LOW);
digitalWrite(REARLIGHT, LOW);
digitalWrite(DIPLIGHT, LOW);
digitalWrite(MAINLIGHT, HIGH);
break;
case 6: //park lights on
digitalWrite(FOGLIGHT, LOW);
digitalWrite(REARLIGHT, LOW);
digitalWrite(DIPLIGHT, LOW);
digitalWrite(MAINLIGHT, LOW);
digitalWrite(PARKLIGHT, HIGH);
break;
default://all off
digitalWrite(FOGLIGHT, LOW);
digitalWrite(REARLIGHT, LOW);
digitalWrite(DIPLIGHT, LOW);
digitalWrite(MAINLIGHT, LOW);
digitalWrite(PARKLIGHT, LOW);
break;
}
}
/**
* interrupt wake up routine
*/
void wakeUp() {
enableSleep = 0;
handleLightState(0);
}
/**
* power down device
*/
void gotoSleep() {
handleLightState(0);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
attachInterrupt(1, wakeUp, HIGH);
sleep_mode();
sleep_disable();
detachInterrupt(1);
}
/**
* setup device
*/
void setup() {
//setup output pins
pinMode(MAINLIGHT, OUTPUT);
pinMode(DIPLIGHT, OUTPUT);
pinMode(FOGLIGHT, OUTPUT);
pinMode(REARLIGHT, OUTPUT);
pinMode(PARKLIGHT, OUTPUT);
//setup button pins
pinMode(BTN_MAINLIGHT, INPUT);
digitalWrite(BTN_MAINLIGHT, HIGH); //enable internal pullup
pinMode(BTN_DIPLIGHT, INPUT);
digitalWrite(BTN_DIPLIGHT, HIGH); //enable internal pullup
pinMode(BTN_FOGLIGHT, INPUT);
digitalWrite(BTN_FOGLIGHT, HIGH); //enable internal pullup
pinMode(BTN_PARKLIGHT, INPUT);
pinMode(ENGINE_ON, INPUT);
handleLightState(0); //turn all off on start
}
/**
* working loop
*/
void loop() {
time = millis();
int readBtnMainLight = digitalRead(BTN_MAINLIGHT);
int readBtnDipLight = digitalRead(BTN_DIPLIGHT);
int readBtnFogLight = digitalRead(BTN_FOGLIGHT);
int readParkLight = digitalRead(BTN_PARKLIGHT);
int readPowerOn = digitalRead(ENGINE_ON);
//check if we will suspend
if (enableSleep == 1) {
if (readPowerOn == LOW &&
readParkLight == LOW &&
(time > (powerOffTime + SLEEP_TIME))) {
gotoSleep();
} else if(readPowerOn == HIGH) {
enableSleep = 0;
}
}
//handle buttons
if (readBtnDipLight == LOW) { //lights on
//test if fog light button pressed and toggle it
if (readBtnFogLight == LOW && count % MODULO == 0) {
fogState = !fogState;
}
if (fogState == 1) {
//fog on, dip on, main off
if (readBtnMainLight == HIGH) {
currentState = 3;
}
//fog light on, dip off, main on
if (readBtnMainLight == LOW) {
currentState = 4;
}
} else {
//fog off, dip on, main off
if (readBtnMainLight == HIGH) {
currentState = 1;
}
//fog off, dip off, main on
if (readBtnMainLight == LOW) {
currentState = 2;
}
}
} else if (readBtnDipLight == HIGH && readBtnMainLight == LOW) {
//using main light on daytime
currentState = 5; // only main lights on
} else if (readPowerOn == LOW && readParkLight == HIGH) {
enableSleep = 0;
// swith on park lights
currentState = 6;
} else {
//turn all off
currentState = 0;
fogState = 0;
}
//check
if (readPowerOn == LOW && readParkLight == LOW && enableSleep == 0) {
powerOffTime = time;
enableSleep = 1;
}
handleLightState(currentState);
count++;
delay(DELAY);
}