-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArduinoTask.ino
More file actions
387 lines (323 loc) · 8.13 KB
/
ArduinoTask.ino
File metadata and controls
387 lines (323 loc) · 8.13 KB
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
/*
ArduinoTask Compatible with v1.02
Digital Pins 2 through 7 are used for Buttons A through F
Uppercase letter sent when the button is depressed
Lowercase letter sent when the button is released
Digital Pins 8, 9, and 10 will control reward.
Digital Pin 11 and Analog Pins 4 and 5 are used for Arduino-Arduino communication
Master/Slave determiniation made upon initialization
*/
#include <Wire.h>
const char *arduinoIdentifier = "ARDUINO";
//restart function
void(* resetFunc) (void) = 0;
//Use this to ensure that mutually incompatible MatLab code and Arduino software aren't used together
const char versionIdentifier = 'C';
//The following variables are used to set up the Properties of the session.
////The cReflectiveInd character indicates the Arduino should bounce received messages back.
////Great for testing.
const char cReflectiveInd = 'R';
////The cPairedInd character indicates the Arduino is paired with a second one.
const char cPairedInd = 'P';
////The cMasterInd character indicates the Arduino will behave as the master in the pair.
const char cMasterInd = 'M';
////The cSlaveInd character indicates the Arduino will behave as the slave in the pair.
const char cSlaveInd = 'S';
////The cFinishedInd character indicates you are Finished setting properties.
const char cFinishedInd = 'F';
////The cRestartChar causes the Arduino to restart
const char cRestartChar = '~';
////The cAck character is used as a reply to acknowledge a property.
const char cAck = 'A';
//Global flags to keep track of the above properties.
boolean bReflective = false;
boolean bPaired = false;
boolean bMaster = true;
const long signed millisRewardTimeout = 2000;
const int pins[] = {2, 3, 4, 5, 6, 7};
const int rewardPins[] = {8, 9, 10};
const int PINNUMBER = sizeof(pins) / sizeof(int);
const int REWARDNUMBER = sizeof (rewardPins) / sizeof(int);
const char rewardOnSignals[] = {'1','2','3'};
const char rewardOffSignals[] = {'4','5','6'};
byte rewardTable[256];
long signed timeRewardLeft[REWARDNUMBER];
const char downStates[] = {'A', 'B', 'C', 'D', 'E', 'F'};
const char upStates[] = {'a', 'b', 'c', 'd', 'e', 'f'};
const long signed DEBTIME = 5;
const int MESSAGEPIN = 11;
int states[PINNUMBER];
long signed debounce[PINNUMBER];
long unsigned millisThisFrame;
long unsigned millisLastFrame;
void setup()
{
Serial.begin(115200);
millisLastFrame=0;
establishConnection();
identifyMyself();
communicateProperties();
if ( bPaired )
{
pairedSetup();
}
else
{
bMaster = true;
}
if ( bMaster)
{
masterSetup();
}
}
void loop()
{
millisThisFrame=millis();
HandleSerialComm();
if ( bMaster )
{
CheckWireComm();
CheckButtons();
CheckRewardTimeout();
}
millisLastFrame=millisThisFrame;
}
void establishConnection()
{
bool bFinished = false;
while ( !bFinished )
{
while ( Serial.available() <= 0 )
{
delay ( 200 );
Serial.print ( versionIdentifier );
delay ( 300 );
}
//Flush the buffer of the initialization character
char tmpChar = Serial.read();
if ( tmpChar == cRestartChar )
{
resetFunc();
}
else
{
bFinished = true;
}
}
}
void identifyMyself()
{
delay ( 100 );
Serial.print ( arduinoIdentifier );
delay ( 100 );
}
void communicateProperties()
{
boolean bFinished = false;
boolean bAck = false;
while ( !bFinished )
{
if ( Serial.available() )
{
char inChar = Serial.read();
switch (inChar)
{
case versionIdentifier:
bAck = false;
break;
case cReflectiveInd:
bAck = true;
bReflective = true;
break;
case cPairedInd:
bAck = true;
bPaired = true;
break;
case cMasterInd:
bAck = true;
bMaster = true;
break;
case cSlaveInd:
bAck = true;
bMaster = false;
break;
case cFinishedInd:
bAck = true;
bFinished = true;
break;
case cRestartChar:
resetFunc();
break;
default:
bAck = false;
Serial.print('!');
break;
}
if ( bAck )
{
bAck = false;
Serial.print(cAck);
}
}
}
}
void pairedSetup()
{
if ( bMaster )
{
Wire.begin();
pinMode ( MESSAGEPIN, INPUT );
}
else
{
Wire.begin(2);
//Function to call when receiving Wire data from the Master
Wire.onReceive ( receiveEvent );
//Function to call when Wire data is Requested by the Master
Wire.onRequest ( requestEvent );
pinMode ( MESSAGEPIN, OUTPUT );
}
}
void masterSetup()
{
for ( int iPin = 0; iPin < PINNUMBER; iPin++ )
{
pinMode ( pins[iPin], INPUT );
states[iPin] = 0;
debounce[iPin] = 0;
}
for ( int iRewardChan = 0; iRewardChan < REWARDNUMBER; iRewardChan++ )
{
pinMode ( rewardPins[iRewardChan], OUTPUT );
timeRewardLeft[iRewardChan] = 0;
rewardTable[byte(rewardOnSignals[iRewardChan])] = (iRewardChan | B11000000 );
rewardTable[byte(rewardOffSignals[iRewardChan])] = (iRewardChan | B10100000 );
}
}
void HandleSerialComm()
{
if (bMaster)
{
while ( Serial.available() > 0 )
{
HandleSerialCommMaster();
}
}
else
{
HandleSerialCommSlave();
}
}
void HandleSerialCommMaster()
{
char inSerial = Serial.read();
if ( inSerial == cRestartChar )
{
resetFunc();
}
if ( bReflective )
{
Serial.print(inSerial);
}
byte entry = rewardTable[byte(inSerial)];
if ( entry )
{
byte rewardChan = ( entry & B11111 );
if ( entry & B1000000 )
{
timeRewardLeft[rewardChan] = millisRewardTimeout;
digitalWrite ( rewardPins[rewardChan], HIGH );
}
else
{
timeRewardLeft[rewardChan] = 0;
digitalWrite ( rewardPins[rewardChan], LOW );
}
}
else if ( bPaired )
{
Wire.beginTransmission(2);
Wire.write ( inSerial );
Wire.endTransmission();
}
}
void HandleSerialCommSlave()
{
digitalWrite ( MESSAGEPIN, HIGH );
}
void CheckWireComm()
{
if ( bPaired && digitalRead ( MESSAGEPIN ) == HIGH )
{
Wire.requestFrom ( 2, 1 );
while ( Wire.available() )
{
char c = Wire.read();
if ( c != -1 && c != '\r' && c != '\n' )
{
Serial.print ( c );
}
}
}
}
void CheckButtons()
{
for ( int iPin = 0; iPin < PINNUMBER; iPin++ )
{
if ( debounce[iPin] <= 0 )
{
int tmpState = digitalRead(pins[iPin]);
if ( tmpState != states[iPin] )
{
states[iPin] = tmpState;
if ( states[iPin] == HIGH )
{
Serial.print ( downStates[iPin] );
}
else
{
Serial.print ( upStates[iPin] );
}
debounce[iPin] = DEBTIME;
}
}
else
{
debounce[iPin] -= (millisThisFrame - millisLastFrame);
}
}
}
void CheckRewardTimeout()
{
for ( int iRewardChan = 0; iRewardChan < REWARDNUMBER; iRewardChan++ )
{
if ( timeRewardLeft[iRewardChan] > 0 )
{
timeRewardLeft[iRewardChan] -= (millisThisFrame - millisLastFrame);
if ( timeRewardLeft[iRewardChan] <= 0 )
{
digitalWrite ( rewardPins[iRewardChan], LOW );
}
}
}
}
void receiveEvent ( int incomingBytes )
{
while ( Wire.available() )
{
char incChar = Wire.read();
Serial.print ( incChar );
}
}
void requestEvent()
{
char commChar = Serial.read();
if ( commChar != '\n' && commChar != '\r' && commChar != '\0' )
{
Wire.write ( commChar );
}
if ( !Serial.available() )
{
digitalWrite ( MESSAGEPIN, LOW );
}
}