ArduinoThinkGearProcessing: Mind_Wave.pde

File Mind_Wave.pde, 9.3 KB (added by sc, 7 years ago)

Mind_Wave.pde Arduino Code

Line 
1////////////////////////////////////////////////////////////////////////
2// Arduino Bluetooth Interface with Mindwave
3//
4// This is example code provided by NeuroSky, Inc. and is provided
5// license free.
6////////////////////////////////////////////////////////////////////////
7
8#define LED 13
9#define BAUDRATE 115200
10#define DEBUGOUTPUT 0
11#define BLUESMIRFON 2
12
13#define GREENLED1  3
14#define GREENLED2  4
15#define GREENLED3  5
16#define YELLOWLED1 6
17#define YELLOWLED2 7
18#define YELLOWLED3 8
19#define YELLOWLED4 9
20#define REDLED1    10
21#define REDLED2    11
22#define REDLED3    12
23
24// checksum variables
25byte generatedChecksum = 0;
26byte checksum = 0;
27int payloadLength = 0;
28byte payloadData[64] = {
29  0};
30byte poorQuality = 0;
31byte attention = 0;
32byte meditation = 0;
33
34// system variables
35long lastReceivedPacket = 0;
36boolean bigPacket = false;
37
38//////////////////////////
39// Microprocessor Setup //
40//////////////////////////
41void setup() {
42
43  pinMode(GREENLED1, OUTPUT);
44  pinMode(GREENLED2, OUTPUT);
45  pinMode(GREENLED3, OUTPUT);
46  pinMode(YELLOWLED1, OUTPUT);
47  pinMode(YELLOWLED2, OUTPUT);
48  pinMode(YELLOWLED3, OUTPUT);
49  pinMode(YELLOWLED4, OUTPUT);
50  pinMode(REDLED1, OUTPUT);
51  pinMode(REDLED2, OUTPUT);
52  pinMode(REDLED3, OUTPUT);
53
54  pinMode(LED, OUTPUT);
55  pinMode(BLUESMIRFON, OUTPUT);
56  digitalWrite(BLUESMIRFON, HIGH);
57  Serial.begin(BAUDRATE);           // USB
58
59  delay(3000) ;
60  Serial.print(194,BYTE) ;
61
62
63
64}
65
66////////////////////////////////
67// Read data from Serial UART //
68////////////////////////////////
69byte ReadOneByte() {
70  int ByteRead;
71
72  while(!Serial.available());
73  ByteRead = Serial.read();
74
75#if DEBUGOUTPUT 
76  Serial.print((char)ByteRead);   // echo the same byte out the USB serial (for debug purposes)
77#endif
78
79  return ByteRead;
80}
81
82/////////////
83//MAIN LOOP//
84/////////////
85void loop() {
86
87
88  // Look for sync bytes
89  if(ReadOneByte() == 170) {
90    if(ReadOneByte() == 170) {
91
92      payloadLength = ReadOneByte();
93      if(payloadLength > 169)                      //Payload length can not be greater than 169
94          return;
95
96      generatedChecksum = 0;       
97      for(int i = 0; i < payloadLength; i++) { 
98        payloadData[i] = ReadOneByte();            //Read payload into memory
99        generatedChecksum += payloadData[i];
100      }   
101
102      checksum = ReadOneByte();                      //Read checksum byte from stream     
103      generatedChecksum = 255 - generatedChecksum;   //Take one's compliment of generated checksum
104
105        if(checksum == generatedChecksum) {   
106
107        poorQuality = 200;
108        attention = 0;
109        meditation = 0;
110
111        for(int i = 0; i < payloadLength; i++) {    // Parse the payload
112          switch (payloadData[i]) {
113          case 2:
114            i++;           
115            poorQuality = payloadData[i];
116            bigPacket = true;           
117            break;
118          case 4:
119            i++;
120            attention = payloadData[i];                       
121            break;
122          case 5:
123            i++;
124            meditation = payloadData[i];
125            break;
126          case 0x80:
127            i = i + 3;
128            break;
129          case 0x83:
130            i = i + 25;     
131            break;
132          default:
133            break;
134          } // switch
135        } // for loop
136
137#if !DEBUGOUTPUT
138
139        // *** Add your code here ***
140
141        if(bigPacket) {
142          if(poorQuality == 0)
143            digitalWrite(LED, HIGH);
144          else
145            digitalWrite(LED, LOW);
146          Serial.print("PoorQuality: ");
147          Serial.print(poorQuality, DEC);
148          Serial.print(" Attention: ");
149          Serial.print(attention, DEC);
150          if(attention > 40)
151            digitalWrite(GREENLED1, HIGH);
152          else
153            digitalWrite(GREENLED1, LOW);
154
155          Serial.print(" Time since last packet: ");
156          Serial.print(millis() - lastReceivedPacket, DEC);
157          lastReceivedPacket = millis();
158          Serial.print("\n");
159
160          switch(attention / 10) {
161          case 0:
162            digitalWrite(GREENLED1, HIGH);
163            digitalWrite(GREENLED2, LOW);
164            digitalWrite(GREENLED3, LOW);
165            digitalWrite(YELLOWLED1, LOW);
166            digitalWrite(YELLOWLED2, LOW);
167            digitalWrite(YELLOWLED3, LOW);
168            digitalWrite(YELLOWLED4, LOW);
169            digitalWrite(REDLED1, LOW);
170            digitalWrite(REDLED2, LOW);
171            digitalWrite(REDLED3, LOW);           
172            break;
173          case 1:
174            digitalWrite(GREENLED1, HIGH);
175            digitalWrite(GREENLED2, HIGH);
176            digitalWrite(GREENLED3, LOW);
177            digitalWrite(YELLOWLED1, LOW);
178            digitalWrite(YELLOWLED2, LOW);
179            digitalWrite(YELLOWLED3, LOW);
180            digitalWrite(YELLOWLED4, LOW);
181            digitalWrite(REDLED1, LOW);
182            digitalWrite(REDLED2, LOW);
183            digitalWrite(REDLED3, LOW);
184            break;
185          case 2:
186            digitalWrite(GREENLED1, HIGH);
187            digitalWrite(GREENLED2, HIGH);
188            digitalWrite(GREENLED3, HIGH);
189            digitalWrite(YELLOWLED1, LOW);
190            digitalWrite(YELLOWLED2, LOW);
191            digitalWrite(YELLOWLED3, LOW);
192            digitalWrite(YELLOWLED4, LOW);
193            digitalWrite(REDLED1, LOW);
194            digitalWrite(REDLED2, LOW);
195            digitalWrite(REDLED3, LOW);
196            break;
197          case 3:             
198            digitalWrite(GREENLED1, HIGH);
199            digitalWrite(GREENLED2, HIGH);
200            digitalWrite(GREENLED3, HIGH);             
201            digitalWrite(YELLOWLED1, HIGH);
202            digitalWrite(YELLOWLED2, LOW);
203            digitalWrite(YELLOWLED3, LOW);
204            digitalWrite(YELLOWLED4, LOW);
205            digitalWrite(REDLED1, LOW);
206            digitalWrite(REDLED2, LOW);
207            digitalWrite(REDLED3, LOW);             
208            break;
209          case 4:
210            digitalWrite(GREENLED1, HIGH);
211            digitalWrite(GREENLED2, HIGH);
212            digitalWrite(GREENLED3, HIGH);             
213            digitalWrite(YELLOWLED1, HIGH);
214            digitalWrite(YELLOWLED2, HIGH);
215            digitalWrite(YELLOWLED3, LOW);
216            digitalWrite(YELLOWLED4, LOW);
217            digitalWrite(REDLED1, LOW);
218            digitalWrite(REDLED2, LOW);
219            digitalWrite(REDLED3, LOW);             
220            break;
221          case 5:
222            digitalWrite(GREENLED1, HIGH);
223            digitalWrite(GREENLED2, HIGH);
224            digitalWrite(GREENLED3, HIGH);             
225            digitalWrite(YELLOWLED1, HIGH);
226            digitalWrite(YELLOWLED2, HIGH);
227            digitalWrite(YELLOWLED3, HIGH);
228            digitalWrite(YELLOWLED4, LOW);
229            digitalWrite(REDLED1, LOW);
230            digitalWrite(REDLED2, LOW);
231            digitalWrite(REDLED3, LOW);               
232            break;
233          case 6:             
234            digitalWrite(GREENLED1, HIGH);
235            digitalWrite(GREENLED2, HIGH);
236            digitalWrite(GREENLED3, HIGH);             
237            digitalWrite(YELLOWLED1, HIGH);
238            digitalWrite(YELLOWLED2, HIGH);
239            digitalWrite(YELLOWLED3, HIGH);
240            digitalWrite(YELLOWLED4, HIGH);
241            digitalWrite(REDLED1, LOW);
242            digitalWrite(REDLED2, LOW);
243            digitalWrite(REDLED3, LOW);             
244            break;
245          case 7:
246            digitalWrite(GREENLED1, HIGH);
247            digitalWrite(GREENLED2, HIGH);
248            digitalWrite(GREENLED3, HIGH);             
249            digitalWrite(YELLOWLED1, HIGH);
250            digitalWrite(YELLOWLED2, HIGH);
251            digitalWrite(YELLOWLED3, HIGH);
252            digitalWrite(YELLOWLED4, HIGH);
253            digitalWrite(REDLED1, HIGH);
254            digitalWrite(REDLED2, LOW);
255            digitalWrite(REDLED3, LOW);             
256            break;   
257          case 8:
258            digitalWrite(GREENLED1, HIGH);
259            digitalWrite(GREENLED2, HIGH);
260            digitalWrite(GREENLED3, HIGH);             
261            digitalWrite(YELLOWLED1, HIGH);
262            digitalWrite(YELLOWLED2, HIGH);
263            digitalWrite(YELLOWLED3, HIGH);
264            digitalWrite(YELLOWLED4, HIGH);
265            digitalWrite(REDLED1, HIGH);
266            digitalWrite(REDLED2, HIGH);
267            digitalWrite(REDLED3, LOW);
268            break;
269          case 9:
270            digitalWrite(GREENLED1, HIGH);
271            digitalWrite(GREENLED2, HIGH);
272            digitalWrite(GREENLED3, HIGH);             
273            digitalWrite(YELLOWLED1, HIGH);
274            digitalWrite(YELLOWLED2, HIGH);
275            digitalWrite(YELLOWLED3, HIGH);
276            digitalWrite(YELLOWLED4, HIGH);
277            digitalWrite(REDLED1, HIGH);
278            digitalWrite(REDLED2, HIGH);
279            digitalWrite(REDLED3, HIGH);
280            break;
281          case 10:
282            digitalWrite(GREENLED1, HIGH);
283            digitalWrite(GREENLED2, HIGH);
284            digitalWrite(GREENLED3, HIGH);             
285            digitalWrite(YELLOWLED1, HIGH);
286            digitalWrite(YELLOWLED2, HIGH);
287            digitalWrite(YELLOWLED3, HIGH);
288            digitalWrite(YELLOWLED4, HIGH);
289            digitalWrite(REDLED1, HIGH);
290            digitalWrite(REDLED2, HIGH);
291            digitalWrite(REDLED3, HIGH);
292            break;           
293          }                     
294        }
295#endif       
296        bigPacket = false;       
297      }
298      else {
299        // Checksum Error
300      }  // end if else for checksum
301    } // end if read 0xAA byte
302  } // end if read 0xAA byte
303}