/
aux.c
740 lines (616 loc) · 23.7 KB
/
aux.c
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
#include "elecanisms.h"
#include "peripheral_core.h"
#include "i2c_address_space.h"
#include "ajuart.h"
// picocom -b 115200 /dev/ttyUSB0
char buffer[128];
#define MODULE_LED_RED D0
#define MODULE_LED_GREEN D1
#define resistor_0 0x3b4
#define resistor_1 0x2ed
#define resistor_2 0x276
#define resistor_3 0x1f1
#define resistor_4 0x162
#define resistor_5 0xc6
#define cutoff_01 850
#define cutoff_12 690
#define cutoff_23 565
#define cutoff_34 430
#define cutoff_45 300
typedef void (*STATE_HANDLER_T)(void); // frame of game states
void setup(void);// forward declaration of module modes
void run(void);
void solved(void);
void end_win(void);
void end_fail(void);
STATE_HANDLER_T state, last_state;
typedef void (*GAME_STATE)(void); // the states the games goes through
void sixwires(void);
void fivewires(void);
void fourwires(void);
void threewires(void);
void twowires(void);
void onewires(void);
void zerowires(void);
GAME_STATE gamestate;
uint8_t done_striked;
uint16_t a0, a1, a2, a3, a4, a5;
uint8_t wires_out, wires_in; // counts the number of empty ports
uint16_t aux_cables[6];
uint16_t initial_aux_cables[6];
uint8_t initial_color_vals[7];
uint8_t color_vals[7];
uint16_t winarray[7];
void ledoff(void); // define function
void set_gamestate(void);
void update_analogvals(void);
void initial_aux_values(void);
void update_color_values(void);
void initial_color_values(void);
void update_aux_values(void);
void update_wires_out(void);
void checkwin_cordlocation(void);
void checkwin_cordcolor(void);
// uint8_t check_num_plugged(void){delay_by_nop(1); }
int16_t main(void) {
init_elecanisms();
i2c2_init(157);
init_ajuart(); // Initializes I2C on I2C2
I2C2ADD = MODULE_AUXCABLE_ADDR >>1; // Set the device address (7-bit register)
I2C2MSK = 0; // Set mask to 0 (only this address matters)
_SI2C2IE = 1; // Enable i2c slave interrupt
D0_DIR = OUT;
D1_DIR = OUT;
state = setup;
delay_by_nop(300);
while (1) {
state();
sprintf(buffer, "INITIAL ||| AuxVal (0)%x (1)%x (2)%x (3)%x (4)%x (5)%x || ColorVal (1)%x (2)%x (3)%x (4)%x (5)%x (6)%x ",
initial_aux_cables[0], initial_aux_cables[1], initial_aux_cables[2], initial_aux_cables[3], initial_aux_cables[4], initial_aux_cables[5],
initial_color_vals[1], initial_color_vals[2], initial_color_vals[3], initial_color_vals[4], initial_color_vals[5], initial_color_vals[6]
);
U1_putc('\r'); U1_putc('\n');
U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
delay_by_nop(100);
sprintf(buffer, "CURRENT ||| AuxVal (0)%x (1)%x (2)%x (3)%x (4)%x (5)%x || ColorVal (1)%x (2)%x (3)%x (4)%x (5)%x (6)%x ",
aux_cables[0], aux_cables[1], aux_cables[2], aux_cables[3], aux_cables[4], aux_cables[5],
color_vals[1], color_vals[2], color_vals[3], color_vals[4], color_vals[5], color_vals[6]
);
U1_putc('\r'); U1_putc('\n');
U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
delay_by_nop(100);
sprintf(buffer, "FLAGS ||| EPS %x | FLUX %x | RTC %x ", led_eps, led_flux, led_rtc );
U1_putc('\r'); U1_putc('\n');
U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
delay_by_nop(100);
sprintf(buffer, "-----------------------------------------------");
U1_putc('\r'); U1_putc('\n');
U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
delay_by_nop(600000);
}
} // end of main
//GAMEPLAY STATE MACHINE FUNCTIONS ////////////////////////////
///////////////////////////////////////////////////////////////
// note that the circle of ports are labeled
// clockwise starting at the top from zero to 5
// this corresponds to the analog read pin associated
void sixwires(void){
update_analogvals();
update_aux_values();
uint8_t twoclockwisewire;
twoclockwisewire = initial_aux_cables[2];
winarray[0] = twoclockwisewire;
winarray[1] = initial_aux_cables[1];
winarray[2] = 0;
winarray[3] = initial_aux_cables[3];
winarray[4] = initial_aux_cables[4];
winarray[5] = initial_aux_cables[5];
checkwin_cordlocation();
// sprintf(buffer, "ACTUAL A0: %x %x | A1: %x %x | A2: %x %x | A3: %x %x | A4: %x %x | A5: %x %x ",
// aux_cables[0],a0, aux_cables[1],a1, aux_cables[2], a2, aux_cables[3], a3, aux_cables[4], a4, aux_cables[5], a5 );
// U1_putc('\r'); U1_putc('\n');
// U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
// delay_by_nop(100);
//
// sprintf(buffer, "GOALS A0: %x %x | A1: %x %x | A2: %x %x | A3: %x %x | A4: %x %x | A5: %x %x ",
// winarray[0],a0, winarray[1],a1, winarray[2], a2, winarray[3], a3, winarray[4], a4, winarray[5], a5 );
// U1_putc('\r'); U1_putc('\n');
// U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
// delay_by_nop(2000000);
// sprintf(buffer, "-----------------------------------------------");
// U1_putc('\r'); U1_putc('\n');
// U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
// delay_by_nop(100);
sprintf(buffer, "sixwires ---------------------------------------");
U1_putc('\r'); U1_putc('\n');
U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
delay_by_nop(60000);
}
void fivewires(void) {
// list order is Purple Blue Yellow Red Orange Black
// unplug all wires that are after the one that is already missing
// purple state, unplug all
update_color_values();
if(initial_color_vals[2] == 0){
winarray[1] = 0; // black
winarray[2] = 0; // purple
winarray[3] = 0; // blue
winarray[4] = 0; // yellow
winarray[5] = 0; // orange
winarray[6] = 0; // red
// sprintf(buffer, "purple state");
// U1_putc('\r'); U1_putc('\n');
// U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
// delay_by_nop(60000);
}
// Blue state, remove all but purple
if(initial_color_vals[3] == 0){
winarray[1] = 0; // black
winarray[2] = 1; // purple
winarray[3] = 0; // blue
winarray[4] = 0; // yellow
winarray[5] = 0; // orange
winarray[6] = 0; // red
// sprintf(buffer, "blue state");
// U1_putc('\r'); U1_putc('\n');
// U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
// delay_by_nop(60000);
}
// Yellow state, remove all but purple, blue
if(initial_color_vals[4] == 0){
winarray[1] = 0; // black
winarray[2] = 1; // purple
winarray[3] = 1; // blue
winarray[4] = 0; // yellow
winarray[5] = 0; // orange
winarray[6] = 0; // red
// sprintf(buffer, "yellow state");
// U1_putc('\r'); U1_putc('\n');
// U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
// delay_by_nop(60000);
}
// Red state, remove all but purple, blue, yellow
if(initial_color_vals[6] == 0){
winarray[1] = 0; // black
winarray[2] = 1; // purple
winarray[3] = 1; // blue
winarray[4] = 1; // yellow
winarray[5] = 0; // orange
winarray[6] = 0; // red
// sprintf(buffer, "red state");
// U1_putc('\r'); U1_putc('\n');
// U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
// delay_by_nop(60000);
}
// orange state, remove all but purple, blue, yello, red
if(initial_color_vals[5] == 0){
winarray[1] = 0; // black
winarray[2] = 1; // purple
winarray[3] = 1; // blue
winarray[4] = 1; // yellow
winarray[5] = 0; // orange
winarray[6] = 1; // red
// sprintf(buffer, "orange state");
// U1_putc('\r'); U1_putc('\n');
// U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
// delay_by_nop(60000);
}
// black state, remove all wires
if(initial_color_vals[1] == 0){
winarray[1] = 0; // black
winarray[2] = 0; // purple
winarray[3] = 0; // blue
winarray[4] = 0; // yellow
winarray[5] = 0; // orange
winarray[6] = 0; // red
// sprintf(buffer, "black state");
// U1_putc('\r'); U1_putc('\n');
// U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
// delay_by_nop(60000);
}
checkwin_cordcolor();
sprintf(buffer, "fivewires ---------------------------------------");
U1_putc('\r'); U1_putc('\n');
U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
delay_by_nop(60000);
}
void fourwires(void){
// → If both power and ground are plugged in, remove all of the other wires. (2)
// → If only power is plugged in, attach ground. If only ground is plugged in, attach power. (5)
// → If neither are plugged in and the EPS alarm is active, plug in power. If EPS is not active, plug in ground. (3)
update_color_values();
if(initial_color_vals[6] == 1 && initial_color_vals[1] == 1){
winarray[1] = 1; // black
winarray[2] = 0; // purple
winarray[3] = 0; // blue
winarray[4] = 0; // yellow
winarray[5] = 0; // orange
winarray[6] = 1; // red
}
if((initial_color_vals[6] == 1 && initial_color_vals[1] == 0) ||
(initial_color_vals[6] == 0 && initial_color_vals[1] == 1)){
winarray[1] = 1; // black
winarray[2] = color_vals[2]; // purple
winarray[3] = color_vals[3]; // blue
winarray[4] = color_vals[4]; // yellow
winarray[5] = color_vals[5]; // orange
winarray[6] = 1; // red
}
if(initial_color_vals[6] == 0 && initial_color_vals[1] == 0){
winarray[1] = color_vals[1]; // black
winarray[2] = color_vals[2]; // purple
winarray[3] = color_vals[3]; // blue
winarray[4] = color_vals[4]; // yellow
winarray[5] = color_vals[5]; // orange
winarray[6] = color_vals[6]; // red
if(led_eps == 0){winarray[1] = 1; }
if(led_eps == 1){winarray[1] = 0; }
}
checkwin_cordcolor();
sprintf(buffer, "four wires ---------------------------------------");
U1_putc('\r'); U1_putc('\n');
U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
delay_by_nop(60000);
}
void threewires(void) {
// Check the status of EPS, FLUX, and RTC.
// If they all have the same status, unplug all of the wires. (0)
// If only one is on, unplug any two of the wires. (1)
// If two are active, unplug any one of the wires. (2)
uint8_t status_sum;
status_sum = led_eps + led_flux + led_rtc;
if(status_sum == 0 || status_sum == 3) {
LED1 = 1;
update_wires_out();
if(wires_out == 6){state = solved; }
}
if(status_sum == 1) {
update_wires_out();
if(wires_in == 1){state = solved; }
}
if(status_sum == 2) {
update_wires_out();
if(wires_in == 2){state = solved; }
}
sprintf(buffer, "three wires ---------------------------------------");
U1_putc('\r'); U1_putc('\n');
U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
delay_by_nop(60000);
}
void twowires(void) {
// If the two wires are next to each other, plug in all of the wires. (6)
// If they are directly across from one another, unplug them both. (0)
// If neither of those apply, remove one of the wires. (1)
if(initial_aux_cables[0] != 0 && initial_aux_cables[1] != 0 ||
initial_aux_cables[1] != 0 && initial_aux_cables[2] != 0 ||
initial_aux_cables[2] != 0 && initial_aux_cables[3] != 0 ||
initial_aux_cables[3] != 0 && initial_aux_cables[4] != 0 ||
initial_aux_cables[4] != 0 && initial_aux_cables[5] != 0 ||
initial_aux_cables[5] != 0 && initial_aux_cables[0] != 0 ){
winarray[1] = 1; winarray[2] = 1;
winarray[3] = 1; winarray[4] = 1;
winarray[5] = 1; winarray[6] = 1;
checkwin_cordcolor();
}
if(initial_aux_cables[0] != 0 && initial_aux_cables[3] != 0 ||
initial_aux_cables[1] != 0 && initial_aux_cables[4] != 0 ||
initial_aux_cables[2] != 0 && initial_aux_cables[5] != 0 ){
winarray[1] = 0; winarray[2] = 0;
winarray[3] = 0; winarray[4] = 0;
winarray[5] = 0; winarray[6] = 0;
checkwin_cordcolor();
}
else{
update_wires_out();
if(wires_in == 1) {state = solved; }
}
sprintf(buffer, "two wires ---------------------------------------");
U1_putc('\r'); U1_putc('\n');
U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
delay_by_nop(60000);
}
void onewires(void) {
// If the wire is red, plug in all wires except one. (5)
// If the wire is yellow, plug in all wires except one. (5)
// If the wire is orange, plug in all the wires (6)
// If the wire is blue, plug in all the wires (6)
// If the wire is purple, plug in all the wires except two (4)
// If the wire is black, pull out the wire. (0)
update_wires_out();
update_color_values();
// red or yellow state
if(initial_color_vals[6] == 1 || initial_color_vals[4] == 1){
if(wires_in == 5) {state = solved; }
}
// orange or blue state
if(initial_color_vals[5] == 1 || initial_color_vals[3] == 1){
if(wires_out == 0) {state = solved; }
}
// purple state
if(initial_color_vals[2] == 1 ){
if(wires_out == 2) {state = solved; }
}
// black state
if(initial_color_vals[1] == 1){
if(wires_out == 6) {state = solved; }
}
sprintf(buffer, "one wires ---------------------------------------");
U1_putc('\r'); U1_putc('\n');
U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
delay_by_nop(60000);
}
void zerowires(void) {
uint8_t num_to_add;
num_to_add = 0;
if(led_eps == 1) {num_to_add +=1; }
if(led_rtc == 1) {num_to_add +=2; }
if(led_flux == 1){num_to_add +=3; }
if(num_to_add == 0){num_to_add = 6; }
update_wires_out();
if(wires_in == num_to_add){state = solved; }
sprintf(buffer, "zero wires ---------------------------------------");
U1_putc('\r'); U1_putc('\n');
U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
delay_by_nop(60000);
}
// OVERALL GAME STATE MACHINE FUNCTIONS ///////////////////////
///////////////////////////////////////////////////////////////
void setup(void) { // Waits for master module to start the game
// State Setup
if (state != last_state) {
last_state = state;
MODULE_LED_GREEN = ON; delay_by_nop(1);
MODULE_LED_RED = ON;
complete_flag = 0;
num_strikes = 0;
error_code = 0;
}
// State tasks
initial_aux_values();
initial_color_values();
set_gamestate();
//Check for state transitions
if (start_flag == 1) { state = run;}
// State Cleanup
if (state != last_state) {
MODULE_LED_RED = OFF; delay_by_nop(1);
MODULE_LED_GREEN = OFF;
}
}
void run(void) { // Plays the game
// State Setup
if (state != last_state) {
last_state = state;
MODULE_LED_RED = ON;
done_striked = 0;
}
// Perform state tasks
gamestate();
// Check for state transitions
if (win_flag == 1) {state = end_win;}
else if (lose_flag == 1) {state = end_fail;}
// State Cleanup
if (state != last_state) {MODULE_LED_RED = OFF;}
}
void solved(void) { // The puzzle on this module is finished
// State Setup
if (state != last_state) {
last_state = state;
complete_flag = 1;
MODULE_LED_GREEN = ON;
}
// Perform state tasks
sprintf(buffer, "---------------You Won!!--------------------");
U1_putc('\r'); U1_putc('\n');
U1_puts((uint8_t*)buffer); U1_flush_tx_buffer();
delay_by_nop(1000);
// Check for state transitions
if (win_flag == 1) {
state = end_win;
} else if (lose_flag == 1) {
state = end_fail;
}
// State Cleanup
if (state != last_state) {
complete_flag = 0;
MODULE_LED_GREEN = OFF;
}
}
void end_win(void) { // The master module said the game was won
// State Setup
if (state != last_state) {
last_state = state;
MODULE_LED_GREEN = ON;
T1CON = 0x0030; // set Timer1 period to 0.5s
PR1 = 0x7A11;
TMR1 = 0; // set Timer1 count to 0
IFS0bits.T1IF = 0; // lower Timer1 interrupt flag
T1CONbits.TON = 1; // turn on Timer1
}
// Perform state tasks
if (IFS0bits.T1IF == 1) {
IFS0bits.T1IF = 0; // lower Timer1 interrupt flag
MODULE_LED_GREEN = !MODULE_LED_GREEN; // toggle LED
}
// State Cleanup
if (state != last_state) {
MODULE_LED_GREEN = OFF;
}
}
void end_fail(void) { // The master module said the game was lost
// State Setup
if (state != last_state) {
last_state = state;
MODULE_LED_RED = ON;
T1CON = 0x0030; // set Timer1 period to 0.5s
PR1 = 0x7A11;
TMR1 = 0; // set Timer1 count to 0
IFS0bits.T1IF = 0; // lower Timer1 interrupt flag
T1CONbits.TON = 1; // turn on Timer1
}
// Perform state tasks
if (IFS0bits.T1IF == 1) {
IFS0bits.T1IF = 0; // lower Timer1 interrupt flag
MODULE_LED_RED = !MODULE_LED_RED; // toggle LED
}
// State Cleanup
if (state != last_state) {
MODULE_LED_RED = OFF;
T1CONbits.TON = 0; // turn off Timer1
}
}
///////////////////////////////////////////////////////////
//////////////////// Functions //////////////////////////
///////////////////////////////////////////////////////////
// win check helper functions//////////////////////////////////
///////////////////////////////////////////////////////////////
void checkwin_cordlocation(void){
update_aux_values();
if( winarray[0] == aux_cables[0] &&
winarray[1] == aux_cables[1] &&
winarray[2] == aux_cables[2] &&
winarray[3] == aux_cables[3] &&
winarray[4] == aux_cables[4] &&
winarray[5] == aux_cables[5] ) {state = solved; }
}
void checkwin_cordcolor(void){
update_color_values();
if( winarray[1] == color_vals[1] &&
winarray[2] == color_vals[2] &&
winarray[3] == color_vals[3] &&
winarray[4] == color_vals[4] &&
winarray[5] == color_vals[5] &&
winarray[6] == color_vals[6] ) {state = solved; }
}
void set_gamestate(void){
update_wires_out(); // checks how many wires out //sets the game mode
if(wires_out == 0){gamestate = sixwires; }
if(wires_out == 1){gamestate = fivewires; }
if(wires_out == 2){gamestate = fourwires; }
if(wires_out == 3){gamestate = threewires; }
if(wires_out == 4){gamestate = twowires; }
if(wires_out == 5){gamestate = onewires; }
if(wires_out == 6){gamestate = zerowires; }
}
void update_wires_out(void){
update_aux_values();
wires_out = 0;
if(aux_cables[0] == 0) {wires_out += 1;}
if(aux_cables[1] == 0) {wires_out += 1;}
if(aux_cables[2] == 0) {wires_out += 1;}
if(aux_cables[3] == 0) {wires_out += 1;}
if(aux_cables[4] == 0) {wires_out += 1;}
if(aux_cables[5] == 0) {wires_out += 1;}
wires_in = 6 - wires_out;
}
// aux values helper functions/////////////////////////////////
///////////////////////////////////////////////////////////////
void update_analogvals(void){
a0 = read_analog(A0_AN);
a1 = read_analog(A1_AN);
a2 = read_analog(A2_AN);
a3 = read_analog(A3_AN);
a4 = read_analog(A4_AN);
a5 = read_analog(A5_AN);
}
void initial_aux_values(void){
update_aux_values();
initial_aux_cables[0] = aux_cables[0];
initial_aux_cables[1] = aux_cables[1];
initial_aux_cables[2] = aux_cables[2];
initial_aux_cables[3] = aux_cables[3];
initial_aux_cables[4] = aux_cables[4];
initial_aux_cables[5] = aux_cables[5];
}
void update_aux_values(void){
update_analogvals();
if(a0 > cutoff_01) {aux_cables[0] = 1; }
if(a0 < cutoff_01 && a0 > cutoff_12) {aux_cables[0] = 2; }
if(a0 < cutoff_12 && a0 > cutoff_23) {aux_cables[0] = 3; }
if(a0 < cutoff_23 && a0 > cutoff_34) {aux_cables[0] = 4; }
if(a0 < cutoff_34 && a0 > cutoff_45) {aux_cables[0] = 5; }
if(a0 < cutoff_45 && a0 > 100) {aux_cables[0] = 6; }
if(a0 < 100){aux_cables[0] = 0; }
if(a1 > cutoff_01) {aux_cables[1] = 1; }
if(a1 < cutoff_01 && a1 > cutoff_12) {aux_cables[1] = 2; }
if(a1 < cutoff_12 && a1 > cutoff_23) {aux_cables[1] = 3; }
if(a1 < cutoff_23 && a1 > cutoff_34) {aux_cables[1] = 4; }
if(a1 < cutoff_34 && a1 > cutoff_45) {aux_cables[1] = 5; }
if(a1 < cutoff_45 && a1 > 100) {aux_cables[1] = 6; }
if(a1 < 100){aux_cables[1] = 0; }
if(a2 > cutoff_01) {aux_cables[2] = 1; }
if(a2 < cutoff_01 && a2 > cutoff_12) {aux_cables[2] = 2; }
if(a2 < cutoff_12 && a2 > cutoff_23) {aux_cables[2] = 3; }
if(a2 < cutoff_23 && a2 > cutoff_34) {aux_cables[2] = 4; }
if(a2 < cutoff_34 && a2 > cutoff_45) {aux_cables[2] = 5; }
if(a2 < cutoff_45 && a2 > 100) {aux_cables[2] = 6; }
if(a2 < 100){aux_cables[2] = 0; }
if(a3 > cutoff_01) {aux_cables[3] = 1; }
if(a3 < cutoff_01 && a3 > cutoff_12) {aux_cables[3] = 2; }
if(a3 < cutoff_12 && a3 > cutoff_23) {aux_cables[3] = 3; }
if(a3 < cutoff_23 && a3 > cutoff_34) {aux_cables[3] = 4; }
if(a3 < cutoff_34 && a3 > cutoff_45) {aux_cables[3] = 5; }
if(a3 < cutoff_45 && a3 > 100) {aux_cables[3] = 6; }
if(a3 < 100){aux_cables[3] = 0; }
if(a4 > cutoff_01) {aux_cables[4] = 1; }
if(a4 < cutoff_01 && a4 > cutoff_12) {aux_cables[4] = 2; }
if(a4 < cutoff_12 && a4 > cutoff_23) {aux_cables[4] = 3; }
if(a4 < cutoff_23 && a4 > cutoff_34) {aux_cables[4] = 4; }
if(a4 < cutoff_34 && a4 > cutoff_45) {aux_cables[4] = 5; }
if(a4 < cutoff_45 && a4 > 100) {aux_cables[4] = 6; }
if(a4 < 100){aux_cables[4] = 0; }
if(a5 > cutoff_01) {aux_cables[5] = 1; }
if(a5 < cutoff_01 && a5 > cutoff_12) {aux_cables[5] = 2; }
if(a5 < cutoff_12 && a5 > cutoff_23) {aux_cables[5] = 3; }
if(a5 < cutoff_23 && a5 > cutoff_34) {aux_cables[5] = 4; }
if(a5 < cutoff_34 && a5 > cutoff_45) {aux_cables[5] = 5; }
if(a5 < cutoff_45 && a5 > 100) {aux_cables[5] = 6; }
if(a5 < 100){aux_cables[5] = 0; }
}
// Cord color helper functions ////////////////////////////////
///////////////////////////////////////////////////////////////
void initial_color_values(void){
update_color_values();
// initial_color_vals[0] = color_vals[0];
initial_color_vals[1] = color_vals[1];
initial_color_vals[2] = color_vals[2];
initial_color_vals[3] = color_vals[3];
initial_color_vals[4] = color_vals[4];
initial_color_vals[5] = color_vals[5];
initial_color_vals[6] = color_vals[6];
}
void update_color_values(void){
update_aux_values();
if(aux_cables[0]==1||aux_cables[1]==1||aux_cables[2]==1||
aux_cables[3]==1||aux_cables[4]==1||aux_cables[5]==1){
color_vals[1] = 1; }
else { color_vals[1] = 0;}
if(aux_cables[0]==2||aux_cables[1]==2||aux_cables[2]==2||
aux_cables[3]==2||aux_cables[4]==2||aux_cables[5]==2){
color_vals[2] = 1; }
else { color_vals[2] = 0;}
if(aux_cables[0]==3||aux_cables[1]==3||aux_cables[2]==3||
aux_cables[3]==3||aux_cables[4]==3||aux_cables[5]==3){
color_vals[3] = 1; }
else { color_vals[3] = 0;}
if(aux_cables[0]==4||aux_cables[1]==4||aux_cables[2]==4||
aux_cables[3]==4||aux_cables[4]==4||aux_cables[5]==4){
color_vals[4] = 1; }
else { color_vals[4] = 0;}
if(aux_cables[0]==5||aux_cables[1]==5||aux_cables[2]==5||
aux_cables[3]==5||aux_cables[4]==5||aux_cables[5]==5){
color_vals[5] = 1; }
else { color_vals[5] = 0;}
if(aux_cables[0]==6||aux_cables[1]==6||aux_cables[2]==6||
aux_cables[3]==6||aux_cables[4]==6||aux_cables[5]==6){
color_vals[6] = 1; }
else { color_vals[6] = 0;}
}
// 0ther helpful helper functions ///////////////////////
///////////////////////////////////////////////////////////////
void ledoff(void) {
LED1 = 0; delay_by_nop(1);
LED2 = 0; delay_by_nop(1);
LED3 = 0; delay_by_nop(1);
D0 = OFF;
}