MindWave hack with Arduino

so i recently purchased the MindWave headset from Neurosky (http://store.neurosky.com/products/mindwave-1).

The website gives great developer tools and instructions on how to interface with Arduino, which is basically what I did. here is the source code : (sorry to lazy to compress) ////////////////////////////////////////////////////////////////////////
// Arduino Bluetooth Interface with Mindwave
//
// This is example code provided by NeuroSky, Inc. and is provided
// license free.
////////////////////////////////////////////////////////////////////////

#define LED 13
#define BAUDRATE 115200
#define DEBUGOUTPUT 0
#define BLUESMIRFON 2

#define GREENLED1  3
#define GREENLED2  4
#define GREENLED3  5
#define YELLOWLED1 6
#define YELLOWLED2 7
#define YELLOWLED3 8
#define YELLOWLED4 9
#define REDLED1    10
#define REDLED2    11
#define REDLED3    12

// 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() {

pinMode(GREENLED1, OUTPUT);
pinMode(GREENLED2, OUTPUT);
pinMode(GREENLED3, OUTPUT);
pinMode(YELLOWLED1, OUTPUT);
pinMode(YELLOWLED2, OUTPUT);
pinMode(YELLOWLED3, OUTPUT);
pinMode(YELLOWLED4, OUTPUT);
pinMode(REDLED1, OUTPUT);
pinMode(REDLED2, OUTPUT);
pinMode(REDLED3, OUTPUT);

pinMode(LED, OUTPUT);
pinMode(BLUESMIRFON, OUTPUT);
digitalWrite(BLUESMIRFON, HIGH);
Serial.begin(BAUDRATE);           // USB

delay(3000) ;
Serial.print(194,BYTE) ;

}

////////////////////////////////
// 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 0×80:
i = i + 3;
break;
case 0×83:
i = i + 25;
break;
default:
break;
} // switch
} // for loop

#if !DEBUGOUTPUT

// *** Add your code here ***

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);
if(attention > 40)
digitalWrite(GREENLED1, HIGH);
else
digitalWrite(GREENLED1, LOW);

Serial.print(” Time since last packet: “);
Serial.print(millis() – lastReceivedPacket, DEC);
lastReceivedPacket = millis();
Serial.print(“\n”);

switch(attention / 10) {
case 0:
digitalWrite(GREENLED1, HIGH);
digitalWrite(GREENLED2, LOW);
digitalWrite(GREENLED3, LOW);
digitalWrite(YELLOWLED1, LOW);
digitalWrite(YELLOWLED2, LOW);
digitalWrite(YELLOWLED3, LOW);
digitalWrite(YELLOWLED4, LOW);
digitalWrite(REDLED1, LOW);
digitalWrite(REDLED2, LOW);
digitalWrite(REDLED3, LOW);
break;
case 1:
digitalWrite(GREENLED1, HIGH);
digitalWrite(GREENLED2, HIGH);
digitalWrite(GREENLED3, LOW);
digitalWrite(YELLOWLED1, LOW);
digitalWrite(YELLOWLED2, LOW);
digitalWrite(YELLOWLED3, LOW);
digitalWrite(YELLOWLED4, LOW);
digitalWrite(REDLED1, LOW);
digitalWrite(REDLED2, LOW);
digitalWrite(REDLED3, LOW);
break;
case 2:
digitalWrite(GREENLED1, HIGH);
digitalWrite(GREENLED2, HIGH);
digitalWrite(GREENLED3, HIGH);
digitalWrite(YELLOWLED1, LOW);
digitalWrite(YELLOWLED2, LOW);
digitalWrite(YELLOWLED3, LOW);
digitalWrite(YELLOWLED4, LOW);
digitalWrite(REDLED1, LOW);
digitalWrite(REDLED2, LOW);
digitalWrite(REDLED3, LOW);
break;
case 3:
digitalWrite(GREENLED1, HIGH);
digitalWrite(GREENLED2, HIGH);
digitalWrite(GREENLED3, HIGH);
digitalWrite(YELLOWLED1, HIGH);
digitalWrite(YELLOWLED2, LOW);
digitalWrite(YELLOWLED3, LOW);
digitalWrite(YELLOWLED4, LOW);
digitalWrite(REDLED1, LOW);
digitalWrite(REDLED2, LOW);
digitalWrite(REDLED3, LOW);
break;
case 4:
digitalWrite(GREENLED1, HIGH);
digitalWrite(GREENLED2, HIGH);
digitalWrite(GREENLED3, HIGH);
digitalWrite(YELLOWLED1, HIGH);
digitalWrite(YELLOWLED2, HIGH);
digitalWrite(YELLOWLED3, LOW);
digitalWrite(YELLOWLED4, LOW);
digitalWrite(REDLED1, LOW);
digitalWrite(REDLED2, LOW);
digitalWrite(REDLED3, LOW);
break;
case 5:
digitalWrite(GREENLED1, HIGH);
digitalWrite(GREENLED2, HIGH);
digitalWrite(GREENLED3, HIGH);
digitalWrite(YELLOWLED1, HIGH);
digitalWrite(YELLOWLED2, HIGH);
digitalWrite(YELLOWLED3, HIGH);
digitalWrite(YELLOWLED4, LOW);
digitalWrite(REDLED1, LOW);
digitalWrite(REDLED2, LOW);
digitalWrite(REDLED3, LOW);
break;
case 6:
digitalWrite(GREENLED1, HIGH);
digitalWrite(GREENLED2, HIGH);
digitalWrite(GREENLED3, HIGH);
digitalWrite(YELLOWLED1, HIGH);
digitalWrite(YELLOWLED2, HIGH);
digitalWrite(YELLOWLED3, HIGH);
digitalWrite(YELLOWLED4, HIGH);
digitalWrite(REDLED1, LOW);
digitalWrite(REDLED2, LOW);
digitalWrite(REDLED3, LOW);
break;
case 7:
digitalWrite(GREENLED1, HIGH);
digitalWrite(GREENLED2, HIGH);
digitalWrite(GREENLED3, HIGH);
digitalWrite(YELLOWLED1, HIGH);
digitalWrite(YELLOWLED2, HIGH);
digitalWrite(YELLOWLED3, HIGH);
digitalWrite(YELLOWLED4, HIGH);
digitalWrite(REDLED1, HIGH);
digitalWrite(REDLED2, LOW);
digitalWrite(REDLED3, LOW);
break;
case 8:
digitalWrite(GREENLED1, HIGH);
digitalWrite(GREENLED2, HIGH);
digitalWrite(GREENLED3, HIGH);
digitalWrite(YELLOWLED1, HIGH);
digitalWrite(YELLOWLED2, HIGH);
digitalWrite(YELLOWLED3, HIGH);
digitalWrite(YELLOWLED4, HIGH);
digitalWrite(REDLED1, HIGH);
digitalWrite(REDLED2, HIGH);
digitalWrite(REDLED3, LOW);
break;
case 9:
digitalWrite(GREENLED1, HIGH);
digitalWrite(GREENLED2, HIGH);
digitalWrite(GREENLED3, HIGH);
digitalWrite(YELLOWLED1, HIGH);
digitalWrite(YELLOWLED2, HIGH);
digitalWrite(YELLOWLED3, HIGH);
digitalWrite(YELLOWLED4, HIGH);
digitalWrite(REDLED1, HIGH);
digitalWrite(REDLED2, HIGH);
digitalWrite(REDLED3, HIGH);
break;
case 10:
digitalWrite(GREENLED1, HIGH);
digitalWrite(GREENLED2, HIGH);
digitalWrite(GREENLED3, HIGH);
digitalWrite(YELLOWLED1, HIGH);
digitalWrite(YELLOWLED2, HIGH);
digitalWrite(YELLOWLED3, HIGH);
digitalWrite(YELLOWLED4, HIGH);
digitalWrite(REDLED1, HIGH);
digitalWrite(REDLED2, HIGH);
digitalWrite(REDLED3, HIGH);
break;
}
}
#endif
bigPacket = false;
}
else {
// Checksum Error
}  // end if else for checksum
} // end if read 0xAA byte
} // end if read 0xAA byte
}

and here is the pdf tutorial link : http://developer.neurosky.com/docs/lib/exe/fetch.php?media=mindwave_arduino_and_leds.pdf

and some images of the result. thanks guys!

About these ads

23 Responses to MindWave hack with Arduino

  1. Pingback: MindWave is developer (read: hacker) friendly mind control - Hack a Day

  2. Pingback: MindWave is developer (read: hacker) friendly mind control - machine quotidienne

  3. Pingback: MindWave is developer (read: hacker) friendly mind control « Black Hat Security

  4. Pingback: MindWave Hack with Arduino « Medical and Health related projects with Arduino

  5. Great project Dan! Im kind of an arduino newbie and i bought a mindwave to mess around with. Ive followed neurosky’s tutorial and have successfully gotten my leds to light in correlation with the mindwave signal. I was wondering I you have posted the code you used to integrate the servo positioning! Thanks.

  6. Hello, i checked ur post and followed the mindwave guide, (http://developer.neurosky.com/docs/doku.php?id=arduino_tutorial) and i have cut the wire and did everything the instruction said, but for some reason i cant get the dongle and the headset to pair up. Any suggestion or advice would be highly appreciated as this is the last final puzzle for my DEMO Asia second audition.

    • Well it’s really hard to debug it from here especially if I can’t see it. With these things it could be any number of things. Could be hardware issue. Maybe you over/under soldered some joints ? Is arduino connected right ? Did you buy level converter ? Are wires in right spot? Troubleshoot troubleshoot

  7. Hi Daniel,

    It’s great to find your blog post on Mindwave hacking.

    I have came across a problem to pair the Mindwave headset to the dongle, it will pair if I plug it into the computer running mindwave app. If I unplug it from the computer and supply 3v to the dongle, it just doesn’t pair with the headset.

    In the Mindwave and Arduino application note, page 4 – “Running Mind_Wave.pde”, it mentioned “8. Hit “Reset” button to pair the MindWave to the dongle” Is this reset button referring to the one on Arduino board?

    Cheers,
    David

  8. Here’s how I pair:

    Turn off everything
    Turn on arduino first w/ sketch
    Then turn on mind wave headset.

    Wait till blue light on dongle and arduino appear. Let me know of this works

  9. Blue light on dongle and headset***

  10. Great Post Dan!!!! I’m very happy to see other people out there trying to do this Mindwave Hack. I started the same project after x-mas this year and I have made it very far, but I have ran into a wall. My goal is to use the Arduino’s “Serial Monitor” function to record the headset’s outputting data values and create a time-series .csv file. As of right now: I have the headset and dongle pairing successfully(blue lights), I have the Arduino talking to the computer through the serial monitor with characters streaming on the screen about every 1 second, but all the characters in the “Serial Monitor” show up as what appears to be jiberesh. I’ve double checked the wiring and feel confident with it, but I think the problem lies with where/how I cut the “Rx” & “Tx” wires coming into the micro controller located on the dongle. How did you do it? Is there a way I can test if I did it properly? Again thanks for any help.

    Best,
    CD

    • It’s very hard to say what might be wrong since it could be hardware or software. Are you using the default code? Is everything soldered correctly ? The serial data should be outputting the values as the code says. I know with anything requiring you to connect to tx and rx pins requires you to disconnect wires first, upload, connect and then reset arduino. Hope this helps

  11. Hi Dan,

    I was wondering if you’ve done anything using the Mindwave with MAX/MSP/Jitter? I currently have the Mindset controlling video parameters, running two 1280×720 videos thru a jit.gl.slab into vjit.gl.videoplane. Using MAX 5 and the np mindset object from nonpolyinomial lab. on a macbook pro 10.6.8 2.2ghz intel core i7. so the problem: the Mindset keeps crashing MAX and then freezing my computer. the CPU is also going up to 100% – 200% sometimes, though it usually stays between 30 – 60%.

    Anyway, it’s a long shot, but the google search results for this magic combination of keywords/problems are few and far. Any thoughts?

    Thanks!
    Emily

  12. Hi Daniel , i have some problems, i wantto have answer in the serial port when led is on . but i cant …. i will expalin better if you contact me please in my email radicalister@gmail.com, if you can help me thanks

  13. hi Dan are you still working on your mindwave projects?
    Thanks

  14. This proyect is awesome. Could anybody please tell me how many different actuators can you discriminate ??
    I mean, can I control a ccmotor, a servo and a LED conected in the same system with different frecuencies? How many of them ?

    Example. Conected a led and a servo, can i just power the servo with a frecuency and power the led with another frec ?

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s