-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarm.ino
376 lines (311 loc) · 8.7 KB
/
alarm.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
// IR settings
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
// IR Constants
const long BUTTON_1 = 0xFF6897;
const long BUTTON_2 = 0xFF9867;
const long BUTTON_3 = 0xFFB04F;
const long BUTTON_4 = 0xFF30CF;
const long BUTTON_5 = 0xFF18E7;
const long BUTTON_6 = 0xFF7A85;
const long BUTTON_7 = 0xFF10EF;
const long BUTTON_8 = 0xFF38C7;
const long BUTTON_9 = 0xFF5AA5;
const long BUTTON_0 = 0xFF4AB5;
const long BUTTON_OK = 0xFF02FD;
const long BUTTON_DOWN = 0xFFA857;
const long BUTTON_UP = 0xFF629D;
const long BUTTON_LEFT = 0xFF22DD;
const long BUTTON_RIGHT = 0xFFC23D;
const long BUTTON_STAR = 0xFF42BD;
const long BUTTON_HEX = 0xFF52AD;
// Variables
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
int firstNumber = 1;
int secondNumber = 1;
boolean isFirstNumber = true;
// CONSTANTS
const int BUTTON_PIN = 3;
const int LED_NUM1_PIN = 4;
const int LED_NUM2_PIN = 5;
const String RESET_PROGRAM_STRING = "r";
const String PLAYBACK_STRING = "p";
const String RESET_ALARM_STRING = "i";
const int RESET_ALARM_BUTTONS_COUNT = 5;
const int RESET_ALARM_BUTTONS[RESET_ALARM_BUTTONS_COUNT] = {8, 7, 6, 10}; // TODO
const int SETUP_ALARM_BUTTONS_COUNT = 5;
const int SETUP_ALARM_BUTTONS[SETUP_ALARM_BUTTONS_COUNT]= {8, 7, 6, 1}; // TODO
const int LONG_WAITING_INDEX = 2;
const int LONG_WAITING_TIME = 1500;
const int NORMAL_WAITING_TIME = 400;
const int BUTTON_PUSH_TIME = 100;
const int BETWEEN_BUTTONS_TIME = 200;
void reInit(){
Serial.println("# Re-Init...");
Serial.println(" ");
Serial.println('\n');
Serial.println("*********************************");
stringComplete = false;
inputString = "";
firstNumber = 0;
secondNumber = 0;
isFirstNumber = true;
initLeds();
Serial.println("=== Button Pusher ====");
Serial.println(" - Reset Program: ");
Serial.print(" PC: ");
Serial.println(RESET_PROGRAM_STRING);
Serial.println(" Remote Control: *");
Serial.println(" - Reset the Alarm: ");
Serial.print(" PC: ");
Serial.println(RESET_ALARM_STRING);
Serial.println(" Remote Control: #");
Serial.println(" - Playback buttons: ");
Serial.print(" PC: ");
Serial.println(PLAYBACK_STRING);
Serial.println(" Remote Control: OK");
}
void initLeds(){
digitalWrite(LED_NUM1_PIN, LOW);
digitalWrite(LED_NUM2_PIN, LOW);
}
void onLedNum1(){
digitalWrite(LED_NUM1_PIN, HIGH);
}
void onLedNum2(){
digitalWrite(LED_NUM2_PIN, HIGH);
}
void imitateButtonPush(){
digitalWrite(BUTTON_PIN, HIGH);
delay(BUTTON_PUSH_TIME);
digitalWrite(BUTTON_PIN, LOW);
}
void imitateMultipleButtonPush(int pushCount){
Serial.print(" Push: ");
Serial.println(pushCount);
int i;
for(i = 0; i < pushCount; i = i + 1){
imitateButtonPush();
delay(BETWEEN_BUTTONS_TIME);
}
}
void pushSetupButtons(){
int i;
Serial.println(" Go to the corresponding menu...");
// Get in the corresponding menu item
for (i = 0; i < SETUP_ALARM_BUTTONS_COUNT-1; i = i + 1) {
imitateMultipleButtonPush(SETUP_ALARM_BUTTONS[i]);
// Long wainting
if(i == LONG_WAITING_INDEX){
Serial.println(" Long Wainting...");
delay(LONG_WAITING_TIME);
}else{
delay(NORMAL_WAITING_TIME);
}
}
// Playback car specific numbers
Serial.println(" Playback car specific numbers...");
imitateMultipleButtonPush(firstNumber);
delay(NORMAL_WAITING_TIME);
imitateMultipleButtonPush(secondNumber);
initLeds();
}
void pushResetAlarmButtons(){
Serial.println("# Reset Alarm: ");
int i;
Serial.println(" Go to the reset menu...");
// Get in the corresponding menu item
for (i = 0; i < RESET_ALARM_BUTTONS_COUNT-1; i = i + 1) {
imitateMultipleButtonPush(RESET_ALARM_BUTTONS[i]);
// Long wainting
if(i == LONG_WAITING_INDEX){
Serial.println(" Long Wainting...");
delay(LONG_WAITING_TIME);
}else{
delay(NORMAL_WAITING_TIME);
}
}
Serial.println("### DONE Reset Alarm ###");
Serial.println(" ");
}
boolean isValidNumber(String str)
{
boolean isNum=false;
if(!(str.charAt(0) == '+' || str.charAt(0) == '-' || isDigit(str.charAt(0)))) return false;
for(byte i=1;i<str.length();i++)
{
if(!(isDigit(str.charAt(i)) || str.charAt(i) == '.')){
Serial.println("@ Not a Char");
return false;
}
}
return true;
}
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
pinMode(BUTTON_PIN, OUTPUT);
pinMode(LED_NUM1_PIN, OUTPUT);
pinMode(LED_NUM2_PIN, OUTPUT);
// Start the IR receiver
irrecv.enableIRIn();
reInit();
}
void loop() {
// print the string when a newline arrives:
irReceive();
if (stringComplete) {
// Write
Serial.print("= \"");
Serial.print(inputString);
Serial.println("\"");
// Process Input from Serial
if(inputString.equals(RESET_PROGRAM_STRING)){
reInit();
}else if(inputString.equals(PLAYBACK_STRING)){
Serial.print("# Playback: ");
Serial.print(firstNumber);
Serial.print(", ");
Serial.print(secondNumber);
Serial.println("...");
// Playback buttons
pushSetupButtons();
Serial.println("### DONE Playback ###");
Serial.println(" ");
}else if(inputString.equals(RESET_ALARM_STRING)){
pushResetAlarmButtons();
}else if(isValidNumber(inputString)){
// Possible numbers
if(isFirstNumber){
firstNumber = inputString.toInt();
Serial.print("# First Number is set to: ");
Serial.println(firstNumber);
onLedNum1();
}else{
secondNumber = inputString.toInt();
Serial.print("# Second Number is set to: ");
Serial.println(secondNumber);
onLedNum2();
}
isFirstNumber = !isFirstNumber;
Serial.println("### DONE Number Settings ###");
Serial.println(" ");
} // End of input handling if
// clear the input string
inputString = "";
// clear the string complete
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
if (inChar == '\n') {
stringComplete = true;
}else{
inputString += inChar;
}
}
}
int firstIrDigit = 0;
int secondIrDigit = 0;
int firstIrNumber = 0;
int secondIrNumber = 0;
boolean isfirstIrDigit = true;
boolean isfirstIrNumber = true;
int getIrNumber(){
switch (results.value) {
case BUTTON_1:
return 1;
break;
case BUTTON_2:
return 2;
break;
case BUTTON_3:
return 3;
break;
case BUTTON_4:
return 4;
break;
case BUTTON_5:
return 5;
break;
case BUTTON_6:
return 6;
break;
case BUTTON_7:
return 7;
break;
case BUTTON_8:
return 8;
break;
case BUTTON_9:
return 9;
break;
case BUTTON_0:
return 0;
break;
}
}
void irReceive(){
if (irrecv.decode(&results)) {
// DECODE
switch (results.value) {
case BUTTON_1:
case BUTTON_2:
case BUTTON_3:
case BUTTON_4:
case BUTTON_5:
case BUTTON_6:
case BUTTON_7:
case BUTTON_8:
case BUTTON_9:
case BUTTON_0:
if(isfirstIrDigit){
firstIrDigit = 10 * getIrNumber();
}else{
secondIrDigit = getIrNumber();
int num = firstIrDigit + secondIrDigit;
if(isFirstNumber){
firstNumber = num;
Serial.print("First IrNumber set to: ");
Serial.println(num);
onLedNum1();
}else{
secondNumber = num;
Serial.print("Second IrNumber set to: ");
Serial.println(num);
onLedNum2();
}
isFirstNumber = !isFirstNumber;
}
isfirstIrDigit = !isfirstIrDigit;
break;
case BUTTON_STAR:
reInit();
break;
case BUTTON_HEX:
pushResetAlarmButtons();
break;
case BUTTON_OK:
pushSetupButtons();
break;
default:
break;
}
irrecv.resume(); // Receive the next value
}
}