forked from alexsonablaza/Team11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEeg2Micro2LED2Servo
271 lines (215 loc) · 7.14 KB
/
Eeg2Micro2LED2Servo
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
#include <Servo.h> //Servo library
Servo servo_left; //initialize a servo object for the connected servo
Servo servo_right;
//turn on eeg > arduino > bluetooth
int angle = 0;
//GPIO13 CONNECTION LED STATUS
#define LED 13
#define BAUDRATE 57600
#define DEBUGOUTPUT 0
//GPIO 9 OUTPUT
#define LIGHTON 9
#define powercontrol 10
// checksum variables
byte generatedChecksum = 0;
byte checksum = 0;
int payloadLength = 0;
byte payloadData[64] = {
0};
byte poorQuality = 0;
byte attention = 0;
byte meditation = 0;
// system variables
long lastReceivedPacket = 0;
boolean bigPacket = false;
//////////////////////////
// Microprocessor Setup //
//////////////////////////
void setup() {
servo_left.attach(7);
servo_right.attach(8);
pinMode(LIGHTON, OUTPUT);
pinMode(LED, OUTPUT);
Serial.begin(BAUDRATE); // USB
}
////////////////////////////////
// Read data from Serial UART //
////////////////////////////////
byte ReadOneByte() {
int ByteRead;
while(!Serial.available());
ByteRead = Serial.read();
#if DEBUGOUTPUT
Serial.print((char)ByteRead); // echo the same byte out the USB serial (for debug purposes)
#endif
return ByteRead;
}
/////////////
//MAIN LOOP//
/////////////
void loop() {
// Look for sync bytes
if(ReadOneByte() == 170) {
if(ReadOneByte() == 170) {
payloadLength = ReadOneByte();
if(payloadLength > 169) //Payload length can not be greater than 169
return;
generatedChecksum = 0;
for(int i = 0; i < payloadLength; i++) {
payloadData[i] = ReadOneByte(); //Read payload into memory
generatedChecksum += payloadData[i];
}
checksum = ReadOneByte(); //Read checksum byte from stream
generatedChecksum = 255 - generatedChecksum; //Take one's compliment of generated checksum
if(checksum == generatedChecksum) {
poorQuality = 200;
attention = 0;
meditation = 0;
for(int i = 0; i < payloadLength; i++) { // Parse the payload
switch (payloadData[i]) {
case 2:
i++;
poorQuality = payloadData[i];
bigPacket = true;
break;
case 4:
i++;
attention = payloadData[i];
break;
case 5:
i++;
meditation = payloadData[i];
break;
case 0x80:
i = i + 4;
break;
case 0x83:
i = i + 25;
break;
default:
break;
} // switch
} // for loop
#if !DEBUGOUTPUT
if(attention > 0;attention <25) // no to low leve signal
{
servo_left.write(0); //reset to initial state.
servo_right.write(0);
Serial.print("PoorQuality: ");
Serial.print(poorQuality, DEC);
Serial.print(" Attention: ");
Serial.print(attention, DEC);
Serial.print(" Time since last packet: ");
Serial.print(millis() - lastReceivedPacket, DEC);
lastReceivedPacket = millis();
Serial.print("\n");
delay(100);
}
delay(1000);
if(attention > 25; attention < 50) // upcoming signal
{
servo_left.write(45); //move to 45 degrees
servo_right.write(45);
Serial.print("PoorQuality: ");
Serial.print(poorQuality, DEC);
Serial.print(" Attention: ");
Serial.print(attention, DEC);
Serial.print(" Time since last packet: ");
Serial.print(millis() - lastReceivedPacket, DEC);
lastReceivedPacket = millis();
Serial.print("\n");
delay(100);
}
delay(1000);
if(attention <70; attention > 60) // command to move from 90 degrees between 60- 70 attention level
{
servo_left.write(90);
servo_right.write(90);
Serial.print("PoorQuality: ");
Serial.print(poorQuality, DEC);
Serial.print(" Attention: ");
Serial.print(attention, DEC);
Serial.print(" Time since last packet: ");
Serial.print(millis() - lastReceivedPacket, DEC);
lastReceivedPacket = millis();
Serial.print("\n");
delay(100);
}
delay(1000);
if(attention > 70; attention <60 ) //attention level is above our threshold
{
servo_left.write(0); //reset to 0
servo_right.write(0);
Serial.print("PoorQuality: ");
Serial.print(poorQuality, DEC);
Serial.print(" Attention: ");
Serial.print(attention, DEC);
Serial.print(" Time since last packet: ");
Serial.print(millis() - lastReceivedPacket, DEC);
lastReceivedPacket = millis();
Serial.print("\n");
delay(100);
}
if(bigPacket) {
if(poorQuality == 0)
digitalWrite(LED, HIGH);
else
digitalWrite(LED, LOW);
Serial.print("PoorQuality: ");
Serial.print(poorQuality, DEC);
Serial.print(" Attention: ");
Serial.print(attention, DEC);
Serial.print(" Time since last packet: ");
Serial.print(millis() - lastReceivedPacket, DEC);
lastReceivedPacket = millis();
Serial.print("\n");
/*
switch(attention / 13) {
case 0:
digitalWrite(LIGHTON, LOW);
break;
//No connection or low connection light is off
case 1:
digitalWrite(LIGHTON, LOW);
break;
//Poor quality is high then light is off
case 2:
digitalWrite(LIGHTON, LOW);
break;
case 3:
digitalWrite(LIGHTON, LOW);
break;
case 4:
digitalWrite(LIGHTON, LOW);
break;
//If I am not active then low
case 5:
digitalWrite(LIGHTON, LOW);
//For all cases where attention is greater than 60
case 6:
digitalWrite(LIGHTON, HIGH);
break;
case 7:
digitalWrite(LIGHTON, HIGH);
break;
case 8:
digitalWrite(LIGHTON, HIGH);
break;
case 9:
digitalWrite(LIGHTON, HIGH);
break;
case 10:
digitalWrite(LIGHTON, HIGH);
break;
}
*/
}
#endif
bigPacket = false;
}
else {
// Checksum Error
} // end if else for checksum
} // end if read 0xAA byte
} // end if read 0xAA byte
}