-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadv_arduino_IR_remote.ino
650 lines (567 loc) · 16.7 KB
/
adv_arduino_IR_remote.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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
#include <LiquidCrystal.h>
#include <IRremote.h>
#include <EEPROM.h>
#include "Buzz.h" // includes buzz() function: arg 0 - 5 or other for default
#include "WriteDefaults.h"
//IRremote initializations and variables.
const char irPin = 2;
IRrecv irrecv(irPin);
decode_results results;
IRsend irsend;
//LCD initialization
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// number of banks
#define bankNo 10
// max bank name length
#define bankNameLen 14
// number of signals from basic buttons
#define basicButtonNo 10
// number of signals from additional buttons
#define additionalButtonNo 5
// number of signals in sequence
#define sequenceLen 4
// number of bytes ir signal takes
#define signalLen 4 // size of long
// menu constants
const char maxColumns = 14;
const char menuMain[][maxColumns] = {"Forward IR", "Send IR", "Receive IR", "Connect PC", "Settings"};
const char menuSettings[][maxColumns] = {"Cal. basic", "Cal. addit.", "Test Buzzer"};
// bankNames, menu can be conf by user
char menuSend[bankNo][bankNameLen];
// variables for handling Menu() in loop()
char choice = -1;
char subchoice = -1;
//constants for EEPROM addressing
#define startupModeAddr 0
#define basicButtonsSignalsAddr 1
#define additionalButtonsSignalsAddr ( basicButtonsSignalsAddr + (signalLen * basicButtonNo) )
#define banksNamesAddr ( additionalButtonsSignalsAddr + (signalLen * additionalButtonNo) )
#define sequenceBanksAddr ( banksNamesAddr + (bankNameLen * bankNo) )
// #define otherSignals ( banksAddr + (4 * 10) ) currently unused
// default buzzPin in Buzz.h is 15 (A1)
//arrays for button names
const char basicButtons[basicButtonNo][maxColumns] = {"POWER", "VOLUME UP", "VOLUME DOWN", "Przycisk 1", "Przycisk 2", "Przycisk 3", "Przycisk 4", "Przycisk 5", "Przycisk 6", "Przycisk 7"};
const char additionalButtons[additionalButtonNo][maxColumns] = {"RIGHT", "UP", "DOWN", "LEFT", "OK"};
//arrays for buttons signals
long basicButtonsSignals[basicButtonNo];
long additionalButtonsSignals[additionalButtonNo];
//array for signal sequences
long sequences[bankNo][sequenceLen];
// functions -------------------------------------------------------------------
// ---- load data from eeprom ----
void loadSignals() {
/*
Reads signal values from EEPROM and saves them to basicButtonsSignals array.
Previously named assignButtons.
*/
EEPROM.get(basicButtonsSignalsAddr, basicButtonsSignals);
EEPROM.get(additionalButtonsSignalsAddr, additionalButtonsSignals);
}
void loadBankNames() {
/*
Reads bank names from eeprom and loads to menuSend array.
*/
EEPROM.get(banksNamesAddr, menuSend);
}
void loadSequences() {
/*
Loads sequences of 4 signals from EEPROM.
If sequence element value = 255, writes 0 to sequences array.
*/
int addr = sequenceBanksAddr;
unsigned char read = 0;
for (int j = 0; j < bankNo; j++) {
for (int i = 0; i < sequenceLen; i++) {
read = EEPROM.read(addr++);
if (read == 255) {
sequences[j][i] = 0;
}
else if (read < 10) {
sequences[j][i] = basicButtonsSignals[read];
}
else if (read >= 10 && read < 15) {
sequences[j][i] = additionalButtonsSignals[read];
}
else {
sequences[j][i] = 0; // wrongly programmed sequence
}
// Serial.println(read); // DEBUGGING
}
}
}
// ---- Menu, iu and keypad shield button handling ----
char ButtonRead(int buttonVal) {
/*
Returns lowercase first character of button name
given buttonVal - usually analogRead(A0).
*/
if (buttonVal >= 950) { // nothing pressed
return 0;
}
else if (buttonVal <= 50) { // RIGHT
return 'r';
}
else if (buttonVal > 50 && buttonVal <= 200) { // UP
return 'u';
}
else if (buttonVal > 200 && buttonVal <= 350) { // DOWN
return 'd';
}
else if (buttonVal > 350 && buttonVal <= 600) { // LEFT
return 'l';
}
else if (buttonVal > 600) { // SELECT
return 's';
}
}
char Menu(const char rows, const char list[][maxColumns]) {
/*
Returns index of chosen element of list[] - menu option
returns -1 when going back (left) was chosen.
Displays simple scrollable menu with
arrow (char 127) indicating current choice.
up / down - navigation
right / select - select option (return)
left - go back (return -1)
*/
char button;
bool buttonReleased = true;
bool selectedUpperRow = true;
char currTopOption = 0;
while (true) {
lcd.clear();
if (selectedUpperRow) {
lcd.print(String(list[currTopOption]) + " " + char(127));
lcd.setCursor(0, 1);
lcd.print(String(list[currTopOption + 1]));
}
else {
lcd.print(String(list[currTopOption]));
lcd.setCursor(0, 1);
lcd.print(String(list[currTopOption + 1]) + " " + char(127));
}
do {
button = ButtonRead(analogRead(A0));
delay(2);
} while (button == ButtonRead(analogRead(A0)));
if (button) {
if (button == 's' || button == 'r') { // SELECT or RIGHT
lcd.clear();
return currTopOption + byte(!selectedUpperRow);
}
else if (button == 'l') { // LEFT
lcd.clear();
return -1;
}
else if (button == 'u') { // UP
if (selectedUpperRow) {
if (currTopOption != 0) {
currTopOption--;
}
}
else {
selectedUpperRow = !selectedUpperRow;
}
}
else if (button == 'd') { // DOWN
if (!selectedUpperRow) {
if (currTopOption != rows - 2) {
currTopOption++;
}
}
else {
selectedUpperRow = !selectedUpperRow;
}
}
}
}
}
// ---- IR handling ----
long receiveSignal(const long skip = 0xFFFFFFFF) {
/*
Awaits a signal and returns it. You can also specify
a 'skip' signal which the function will ignore and wait for next one.
*/
irrecv.enableIRIn();
while (true) {
if (ButtonRead(analogRead(A0)) == 'l') {
return 123;
}
if (irrecv.decode(&results)) {
if (results.value == skip) {
irrecv.resume();
continue;
}
// Serial.println(results.value); // DEBUGGING
irrecv.resume();
return results.value;
}
}
}
void forwardIR() {
//sends sequences of signals depending on received signal
long x;
while (true) {
if (ButtonRead(analogRead(A0)) == 'l') {
return;
}
x = receiveSignal();
if (x == 123) {
return;
}
for (int i = 0; i < 10; i++) {
if (x == basicButtonsSignals[i]) {
sendIR(sequences[i]);
}
}
}
}
void sendIR(long signalSequence[sequenceLen]) {
/*
Sends given sequence of IR signals using most common protocols.
Interval between signals is 1 second.
Default length of sequence is 4 signals
*/
for (int i = 0; i < sequenceLen; i++) {
if (signalSequence[i] != 0) {
// Serial.println(signalSequence[i]); // DEBUGGING
irsend.sendSony(signalSequence[i], 32); delay(200);
irsend.sendLG(signalSequence[i], 32); delay(200);
irsend.sendSAMSUNG(signalSequence[i], 32); delay(200);
irsend.sendPanasonic(signalSequence[i], 32);
delay(1000);
}
}
irrecv.enableIRIn();
}
void receiveIR() {
/*
This function receives signals and handles them,
executing specific code depending on what singal has been received.
*/
while (true) {
if (ButtonRead(analogRead(A0)) == 'l') {
return;
}
if (irrecv.decode(&results)) {
// Serial.println(results.value); // DEBUGGING
if (results.value == basicButtonsSignals[0]) {
buzz(2);
}
else if (results.value == basicButtonsSignals[1]) {
buzz(1);
lcd.clear();
lcd.print("VOLUME UP!");
delay(2000);
}
else if (results.value == basicButtonsSignals[2]) {
buzz(0);
lcd.clear();
lcd.print("VOLUME DOWN!");
delay(2000);
}
else if (results.value == basicButtonsSignals[3]) {
buzz(2);
lcd.clear();
lcd.print("WYBRANO P1!");
delay(2000);
}
else if (results.value == basicButtonsSignals[4]) {
buzz(2);
lcd.clear();
lcd.print("WYBRANO P2!");
delay(2000);
}
else {
buzz(2);
}
lcd.clear();
lcd.print("Receive IR");
irrecv.resume();
}
}
}
// ---- PC connection ----
void PCsendButtonSignals() {
// Sends all button signals (basic + additional) through Serial.
for (int i = 0; i < basicButtonNo; i++) {
Serial.println(basicButtonsSignals[i]);
}
for (int i = 0; i < additionalButtonNo; i++) {
Serial.println(additionalButtonsSignals[i]);
}
}
void PCsendBankNames() {
// Sends all bank names by line through Serial.
for (int i = 0; i < bankNo; i++) {
Serial.println(menuSend[i]);
}
}
void PCsendSequences() {
/*
Reads from EEPROM and sends all sequences byte by byte through Serial.
Although sends only 1 byte at a time uses println (delete trailing \r\n).
Testing with print() instead of println
Remember to add linger sleep() due to slow EEPROM reading time.
*/
int addr = sequenceBanksAddr;
unsigned char read = 0;
for (int j = 0; j < bankNo; j++) {
for (int i = 0; i < sequenceLen; i++) {
read = EEPROM.read(addr++);
// if (read < 15 || read == 255) {
Serial.println(read);
// }
// else {
// buzz(); buzz(); buzz(); // error - wrongly programmed sequence
// }
}
}
}
void PCreceiveUpdateEEPROM() {
/*
Receives bytes from Serial and stores them in EEPROM.
Allows for PC - edited banknames and sequences
*/
unsigned char read = 0;
char readChr[14]; // 11 is max no of chars that written long can take
String readStr = "";
int addr = basicButtonsSignalsAddr;
long signal;
Serial.write(1); // write 1 byte to start
// buttons' signals
lcd.clear();
lcd.print("button");
lcd.setCursor(0, 1);
lcd.print("transfer");
for (int i = 0; i < basicButtonNo + additionalButtonNo; i++) {
for (int j = 0; j < 11; j++) {
readChr[j] = ' ';
}
readStr = Serial.readStringUntil('\n');
readStr.toCharArray(readChr, 11);
signal = atol(readChr);
EEPROM.put(addr, signal);
addr = addr + 4;
Serial.write(1);
}
// bank names
lcd.clear();
lcd.print("bankname");
lcd.setCursor(0, 1);
lcd.print("transfer");
for (int i = 0; i < bankNo; i++) {
readStr = Serial.readStringUntil('\n');
readStr.toCharArray(readChr, bankNameLen);
EEPROM.put(addr, readChr);
addr = addr + bankNameLen;
Serial.write(1);
}
// sequences
lcd.clear();
lcd.print("sequence");
lcd.setCursor(0, 1);
lcd.print("transfer");
for (int i = addr; i < ( addr + (4 * 10) ); i++) {
for (int j = 0; j < 4; j++) {
readChr[j] = ' ';
}
readStr = Serial.readStringUntil('\n');
readStr.toCharArray(readChr, 3);
read = atoi(readChr);
EEPROM.update(i, read);
Serial.write(1);
}
lcd.clear();
lcd.print("done");
}
// ---- button calibration ----
void calibrateButtons(const char words[][maxColumns], int addr, const int len) {
/*
Used to calibrate basic remote buttons listed in words[].
For it to work properly you have to click each button 3 times
and all of the signals received have to be the same to prevent misscalibration.
*/
//Start, shows messages on lcd
lcd.clear();
lcd.print("Press each");
lcd.setCursor(0, 1);
lcd.print("button 3 times");
delay(2000);
lcd.clear();
//function on array containing buttons to click
for (int i = 0; i < len; i++) {
long x = 0, y = 0, z = 0, test;
while (true) {
lcd.clear();
lcd.print(words[i]);
x = receiveSignal(); // first signal
//this shows progress of callibration of specific button on screen
lcd.setCursor(0, 1);
lcd.print("3");
y = receiveSignal(); // second signal
if (x != y) { // checking if first and second are the same
lcd.clear();
lcd.print("ERROR!");
lcd.setCursor(0, 1);
lcd.print("Try once again");
delay(1000);
continue;
}
//further progress
lcd.print("2");
z = receiveSignal(); // third signal
if (y != z) { // checking if second and third are the same
lcd.clear();
lcd.print("ERROR!");
lcd.setCursor(0, 1);
lcd.print("Try once again");
delay(1000);
continue;
}
//if all 3 signals are the same callibration of this button is succesful
lcd.print("1");
delay(300);
lcd.clear();
lcd.print("Button");
lcd.setCursor(0, 1);
lcd.print("calibrated");
delay(1000);
lcd.clear();
//saving the button signal to EEPROM
EEPROM.put(addr, x);
break;
}
addr = addr + 4;
}
//renew the buttons arrays after calibration
loadSignals();
loadSequences();
lcd.clear();
buzz(2);
lcd.print("Succesfully");
lcd.setCursor(0, 1);
lcd.print("callibrated");
delay(2000);
}
// reset program function
void (*resetFunc) (void) = 0; //declare reset function at address 0
// setup() and loop() ----------------------------------------------------------
void setup() {
// Serial.begin(9600); //DEBUGGING
pinMode(buzzPin, OUTPUT); // buzzPin defined in Buzz.h
buzz(1);
lcd.begin(16, 2);
irrecv.enableIRIn();
// load first byte from EEPROM
unsigned char startupMode = EEPROM.read(startupModeAddr);
// Set all EEPROM bytes to 0s and set addr 0 to 255
if (startupMode == 1) {
lcd.print("CLEARING EEPROM");
buzz(4);
for (int i = 0 ; i < EEPROM.length() ; i++) {
EEPROM.update(i, 0);
}
EEPROM.update(startupModeAddr, 255);
// restart
resetFunc();
}
// Write default banknames and sequences to EEPROM (WriteDefaults.h)
if (startupMode == 255) {
lcd.print("UPDATING EEPROM");
buzz(4);
writeDefaultBanknames(banksNamesAddr, sequenceBanksAddr);
writeDefaultSequences(sequenceBanksAddr);
EEPROM.update(startupModeAddr, 0);
}
// Load data from EEPROM
loadSignals();
loadBankNames();
loadSequences();
}
void loop() {
choice = Menu((sizeof(menuMain) / sizeof(menuMain[0])), menuMain);
switch (choice) {
// "Forward IR"
case 0:
lcd.print("Forward IR");
forwardIR();
break;
// "Send IR"
case 1:
subchoice = Menu((sizeof(menuSend) / sizeof(menuSend[0])), menuSend);
if (subchoice == -1) {
break;
}
else {
// Serial.println(sequences[subchoice][0]); // DEBUGGING
sendIR(sequences[subchoice]);
}
break;
// "Receive IR"
case 2:
lcd.print("Receive IR");
receiveIR();
break;
// "Connect PC"
case 3:
Serial.begin(9600);
lcd.print("Connecting");
buzz(5);
delay(500);
lcd.setCursor(0, 1);
lcd.print("Sending data");
delay(500);
PCsendButtonSignals();
PCsendBankNames();
PCsendSequences();
lcd.clear();
lcd.print("Data sent");
delay(500);
lcd.clear();
lcd.print("Waiting for res-");
lcd.setCursor(0, 1);
lcd.print("ponse PC signal");
// waiting for byte
while (!Serial.available()) {}
// possible error
if (Serial.available() > 1) {
buzz();
}
// waiting for last byte
// while (!Serial.available()) {}
// if PC sends back 1 - start receiving
if (Serial.read() == 1) {
lcd.clear();
lcd.print("Writing...");
PCreceiveUpdateEEPROM();
// // load received data from EEPROM to memory
// loadSignals();
// loadBankNames();
// loadSequences();
// restart
resetFunc();
}
Serial.end();
buzz(0);
break;
// "Settings"
case 4:
subchoice = Menu((sizeof(menuSettings) / sizeof(menuSettings[0])), menuSettings);
if (ButtonRead(analogRead(A0)) == 'l') {
break;
}
switch (subchoice) {
case 0:
calibrateButtons(basicButtons, basicButtonsSignalsAddr, sizeof(basicButtons) / sizeof(basicButtons[0]));
break;
case 1:
calibrateButtons(additionalButtons, additionalButtonsSignalsAddr, sizeof(additionalButtons) / sizeof(additionalButtons[0]));
break;
case 2:
buzz(2);
break;
}
break;
}
}