digitalRead doesn't work on A6,A7 use analogRead
[Arduino] / Diseq_c / Diseq_c.ino
1 /*
2  */
3
4 // debug on serial
5 #define DEBUG 1
6
7 // serial commands for loop-back test
8 #define TEST 1
9
10 // pins, 2 recorders 4 inputs each 
11 int receiver[2][4] = {
12   { A0, A1, A2, A3 },
13   { A4, A5, A6, A7 },
14 };
15
16 #define REC_MASTER 2
17
18 #define LED 13
19
20 //                  rec1        rec2     master
21 int test_pins[] = { 12,11,10,9, 8,7,6,5, 4 };
22 int test_out[]  = {  0, 0, 0,0, 0,0,0,0, 1};
23 int nr_test_pins = sizeof(test_pins)/sizeof(int);
24
25 void setup(){
26   Serial.begin(9600); // FIXME check speed
27
28   pinMode(REC_MASTER, INPUT_PULLUP); // pullup for jumper/button
29
30   for(int nr=0; nr<2; nr++) {
31     for(int input=0; input<4; input++) {
32       pinMode(receiver[nr][input], INPUT);
33     }
34   }
35
36   pinMode(LED, OUTPUT);
37
38 #if DEBUG
39   Serial.print("boot master=");
40   Serial.println(digitalRead(REC_MASTER) == HIGH );
41 #endif
42
43 #if TEST
44   for(int i=0;i<nr_test_pins;i++) {
45     pinMode(test_pins[i], OUTPUT);
46     digitalWrite(test_pins[i], test_out[i]);
47   }
48 #endif
49 }
50
51 int receiver_selection( int nr ) {
52
53   #ifdef DEBUG
54   Serial.print("<");
55   #endif
56
57   int active = 0;
58   int selected = -1;
59   for(int i=0; i<4; i++) {
60     int a = analogRead( receiver[nr][i] );
61     int on_off = a > 512 ? 1 : 0;
62     active += on_off;
63     if ( on_off ) selected = i+1;
64
65     //Serial.print(a);
66     //Serial.print("~");
67     Serial.print(on_off);
68     //Serial.print(" ");
69   }
70
71   Serial.print(">");
72   Serial.print(selected);
73
74   if ( active == 0 ) { // no inputs active
75     return 0;
76   }
77
78   if ( active != 1 ) { // only one active at a time
79     return -1; // error
80   }
81
82   return selected;
83 }
84
85 #define LED_NO_ACTIVE_INPUTS 2000
86 #define LED_MULTIPLE_INPUTS  125
87 #define LED_SERIAL           200 // < 250
88 #define LED_OFF              0
89
90 int current_sat = 0;
91 int blink_interval = LED_NO_ACTIVE_INPUTS; // 2 sec on/off no receivers turned on
92 int last_changed_nr = -1;
93 int last_receiver_selection[2] = { -1, -1 };
94
95 void loop(){
96
97   int nr = 1;
98
99   while(nr && nr <= 2) {
100     int i = nr - 1;
101
102     int current_nr = nr;
103
104     int sat = receiver_selection(i);
105     int last_sat = last_receiver_selection[i];
106     last_receiver_selection[i] = sat;
107
108     #if DEBUG
109     Serial.print(" r");
110     Serial.print(nr);
111     Serial.print("=");
112     Serial.print(sat);
113     Serial.print(" last:");
114     Serial.print(last_sat);
115     Serial.print("|");
116     #endif
117
118     int prefer_master = 0;
119     if ( digitalRead(REC_MASTER) == HIGH ) {
120       prefer_master = 1;
121     } else {
122         if ( last_sat != sat ) {
123           Serial.print("C");
124           last_changed_nr = current_nr;
125           nr=0;
126         } else {
127           Serial.print("=");
128           nr++;
129           continue;
130         }
131
132         if ( last_changed_nr != current_nr ) {
133           sat = current_sat; // ignore, last not changed
134           Serial.print("I");
135         }
136
137
138     }
139  
140     if ( sat == 0 ) { // slow blink
141       blink_interval = LED_NO_ACTIVE_INPUTS;
142       nr++; // next
143     } else if ( sat < 0 ) { // error
144       blink_interval = LED_MULTIPLE_INPUTS;
145       nr = 0;
146     } else {
147       blink_interval = LED_OFF;
148   
149       if ( prefer_master ) {
150         nr = 0; // stop
151         #if DEBUG
152         Serial.print(" M ");
153         #endif
154       } else {
155         if ( last_sat != sat ) {
156           Serial.print(" C ");
157           last_changed_nr = current_nr;
158           nr=0;
159         } else {
160           Serial.print(" S ");
161           nr++;
162         }
163
164         if ( last_changed_nr != current_nr ) {
165           sat = current_sat; // ignore, last not changed
166           Serial.print(" I ");
167         }
168   
169       }
170
171       if ( current_sat != sat ) {
172
173   last_changed_nr = current_nr;
174         current_sat = sat;
175         nr = 0; // stop
176
177         for(int repeat=0; repeat<2; repeat++) {
178   
179           Serial.print("@L,");
180           Serial.println( char('A' + sat - 1) );
181   
182           for(int i=0; i<sat; i++) {
183             digitalWrite(LED, HIGH);
184             delay(LED_SERIAL);
185             digitalWrite(LED, LOW);
186             delay(LED_SERIAL);
187           }
188           delay( ( 5 - sat ) * LED_SERIAL * 2 ); // sleep up to 2s
189     //assert(4 * LED_SERIAL < 2000);
190         }
191
192       }
193
194     } // while
195
196   }
197
198
199   #if DEBUG
200   Serial.print(" sat=");
201   Serial.print(current_sat);
202   Serial.print(" from:");
203   Serial.print(last_changed_nr);
204   Serial.print(" blink=");
205   Serial.println(blink_interval);
206   #endif
207
208   // handle next led blink event in time
209   int m = millis() % ( blink_interval * 2 );
210   if ( blink_interval != 0 ) {
211     if ( m > blink_interval ) {
212       digitalWrite(LED, HIGH);
213     } else {
214       digitalWrite(LED, LOW);
215     }
216   } else {
217     digitalWrite(LED, LOW);
218   }
219
220   #if TEST
221   while ( Serial.available() ) {
222     int serial = Serial.read();
223     Serial.print(serial, HEX);
224     if ( serial >= '1' && serial <= ( '1' + nr_test_pins ) ) {
225       int nr = serial - '1';
226       Serial.print(" toggle pin ");
227       test_out[nr] = ! test_out[nr];
228       int pin = test_pins[nr];
229       Serial.print(pin);
230       Serial.print("=");
231       int out = test_out[nr];
232       Serial.print(out);
233       digitalWrite( pin, out );
234
235     }
236     Serial.print(" <");
237     for(int i=0;i<nr_test_pins;i++) {
238       if ( i == 4 || i == 8 ) Serial.print(" ");
239       Serial.print(test_out[i]);
240     }
241     Serial.println(">");
242   }
243   #endif
244 }
245
246