-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFobScan.ino
317 lines (230 loc) · 7.76 KB
/
FobScan.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
#include <EnergySaving.h>
// PINS
const int scanDetectPin = 0; // detects access controller valid scan output
const int ignDetectPin = 1; // detects ignition state
const int fobEnablePin = 2; // powers up the fob when high
const int fobUnlockPin = 3; // pulse to "press" the unlock button
const int fobLockPin = 4; // pulse to "press" the lock button
const int fobButtonDelay = 100; // number of milliseconds to "hold" the fob button down for simulated presses
const int powerDelay = 200; // number of milliseconds to wait for the fob to power up or before power down.
// LEDs
#define LED_OFF HIGH // board LEDs in active low configuration
#define LED_ON LOW // here to avoid confusion
const int scanLED = 11; // blue towards the top - solid when access control signal is present
const int countdownLED = 12; // blue next to green LED - When solid, in countdown state
const int fobpowerLED = 13; // orange - On solid when ignition signal is present. Indicates locking/unlocking button presses when ignition is not on
// CONFIG OPTIONS
const bool optUnlock = true; // true will unlock when scanned
const bool optLockOnExitScan = true; // true will lock if you scan during shutdown timer
const bool optLockOnTimeout = true; // true will lock if the shutdown timer expires without an exit scan
const bool optDoubleLock = false; // true will press the lock button twice when locking
const unsigned long optTimeoutMillis = 30000; // Milliseconds until countdown timer expires
// STATES (MAIN)
#define STATE_IDLE 0
#define STATE_COUNTDOWN 1
#define STATE_IGNITION_ON 2
int state = STATE_IDLE; // stores current main state
// STATES (DEBOUNCE)
#define DB_IDLE 0
#define DB_DEBOUNCING 1
#define DB_DEBOUNCED 2
#define DB_WAIT 3
int dbState = DB_IDLE; // stores current debounce state
unsigned long dbStartTime; // store the millis when the debounce timer starts
const unsigned long dbTimeMillis = 100; // debounce for 100ms
bool scanState; // stores current scan status
unsigned long shudownStartTime; // store the millis when the countdown timer starts
EnergySaving lowPower; // Library for sleep mode.
void setup()
{
// PIN I/O SETUP
// INPUTS
pinMode(scanDetectPin, INPUT);
pinMode(ignDetectPin, INPUT);
// LEDS
pinMode(scanLED, OUTPUT);
pinMode(countdownLED, OUTPUT);
pinMode(fobpowerLED, OUTPUT);
// FOB CONTROL
pinMode(fobEnablePin, OUTPUT);
pinMode(fobLockPin, OUTPUT);
pinMode(fobUnlockPin, OUTPUT);
// DEFAULT PIN STATES
digitalWrite(scanLED, LED_OFF);
digitalWrite(countdownLED, LED_OFF);
digitalWrite(fobpowerLED, LED_OFF);
digitalWrite(fobEnablePin, LOW);
digitalWrite(fobLockPin, LOW);
digitalWrite(fobUnlockPin, LOW);
// SETUP SLEEP INTERRUPT
lowPower.begin(WAKE_EXT_INTERRUPT, scanDetectPin, dummy);
}
void loop()
{
scanState = debounceScan(); // check to see if we have a valid tag scan
switch (state)
{
case STATE_IDLE:
{
if (scanState)
{ // we are idle and we got a new scan!
fobEnable(true); // turn on the fob
delay(powerDelay); // wait before we do anything else
if (optUnlock)
fobUnlock(); // unlock if option enabled
// start countdown timer
shudownStartTime = millis();
state = STATE_COUNTDOWN;
}
else
{ // in IDLE state without a scan, power down
digitalWrite(countdownLED, LED_OFF);
fobEnable(false); // turn off the fob
// bedtime. Reduces current from 14ma waiting for a scan to 8ma.
// Of course the access cotnroller draws way more that that but every bit helps.
// sits here until an interrupt on the acccess controller input pin starts the main loop back up
lowPower.standby();
}
}
break;
case STATE_COUNTDOWN:
{ // waitng for ignition, a scan, or timer expire
digitalWrite(countdownLED, LED_ON);
if (millis() - shudownStartTime > optTimeoutMillis)
{ // optTimeoutMillis has elapsed, no one started the car
// and there was no scan, so lock the doors and shut back down
// lock the doors if option is set
if (optLockOnTimeout)
fobLock();
// go to idle
state = STATE_IDLE;
delay(powerDelay);
}
else
{ // still in countdown
if (digitalRead(ignDetectPin))
{ // ignition has been detected while in countdown, halt countdown and wait for ignition drop
state = STATE_IGNITION_ON;
digitalWrite(fobpowerLED, LED_ON);
}
else
{ // no ignition
if (scanState)
{ // we have a scan!
// a scan during a shutdown period is a signal to lock and shut the fob down
if (optLockOnExitScan)
fobLock();
// go to idle
state = STATE_IDLE;
delay(powerDelay);
}
}
}
}
break;
case STATE_IGNITION_ON:
{
// ignition is on
// wait for it to turn off.
digitalWrite(countdownLED, LED_OFF);
bool ign = digitalRead(ignDetectPin);
if (ign == false)
{ // ignition has been turned off
digitalWrite(fobpowerLED, LED_OFF);
// start shutdown timer
shudownStartTime = millis();
state = STATE_COUNTDOWN;
}
}
break;
}
}
void fobLock()
{
// press the fob's lock button
digitalWrite(fobpowerLED, LED_ON);
digitalWrite(fobLockPin, HIGH);
delay(fobButtonDelay);
digitalWrite(fobLockPin, LOW);
if(optDoubleLock)
{
delay(fobButtonDelay);
digitalWrite(fobLockPin, HIGH);
delay(fobButtonDelay);
digitalWrite(fobLockPin, LOW);
}
digitalWrite(fobpowerLED, LED_OFF);
}
void fobUnlock()
{
// press the fob's unlock button
digitalWrite(fobpowerLED, LED_ON);
digitalWrite(fobUnlockPin, HIGH);
delay(fobButtonDelay);
digitalWrite(fobUnlockPin, LOW);
digitalWrite(fobpowerLED, LED_OFF);
}
void fobEnable(bool p)
{
if (p)
{
// digitalWrite(fobpowerLED, LED_ON);
digitalWrite(fobEnablePin, HIGH);
}
else
{
digitalWrite(fobEnablePin, LOW);
// digitalWrite(fobpowerLED, LED_OFF);
}
}
bool debounceScan()
{ // debounces input from access controller and returns debounced pin state. Will not return true again until the input drops after a debounce.
if (digitalRead(scanDetectPin))
{ // input is high
digitalWrite(scanLED, LED_ON);
switch (dbState)
{
case DB_IDLE:
{ // high and idle, start debouncing timer
dbState = DB_DEBOUNCING;
dbStartTime = millis(); // record ticks
}
break;
case DB_DEBOUNCING:
{ // check the deboucning timer
if (millis() - dbStartTime > dbTimeMillis)
{ // dbTimeMillis has elapsed without a state change
dbState = DB_DEBOUNCED;
}
}
break;
case DB_DEBOUNCED:
{ // already debounced. notify the calling function (below)
}
break;
case DB_WAIT:
{ // already debounced and notified calling function, just waiting for the signal to drop.
// nothing to do.
}
break;
}
}
else
{ // input low. no longer debounced
dbState = DB_IDLE;
digitalWrite(scanLED, LED_OFF);
}
if (dbState == DB_DEBOUNCED)
{ // we have debounced the input, notify the calling function
// changing to DB_WAIT state mean there will only be exaclty one call to this function
// that returns true until the input drops and we go back to idle.
// So even if someone holds their tag on the reader, it will only count exactly 1 read.
dbState = DB_WAIT;
return true;
}
else
return false;
}
void dummy(void) //interrupt routine (isn't necessary to execute any tasks in this routine
{
}