1

Currently I am trying to get a Mindwave Mobile device to operate with an Arduino Uno. I'm following the guide here.

However the sample code given doesn't work very well. I have been trying to get a more consistent output but to no avail.

MindWave Code :

#include <SoftwareSerial.h>     // library for software serial
SoftwareSerial mySerial(5, 6);  // RX, TX
int BAUDRATE = 57600;

// checksum variables
byte payloadChecksum = 0;
byte CalculatedChecksum;
byte checksum = 0;              //data type byte stores an 8-bit unsigned number, from 0 to 255
int payloadLength = 0;
byte payloadData[64] = {0};
byte poorQuality = 0;
byte attention = 0;
byte meditation = 0;

// system variables
long lastReceivedPacket = 0;
boolean bigPacket = false;
boolean brainwave = false;

void setup() {
  Serial.begin(57600);       // Bluetooth
  delay(500);
  mySerial.begin(4800);      // software serialz
  delay(500);
  Serial.print("Communicating... ");
  mySerial.print("Communicating... ");
  mySerial.println();
}

byte ReadOneByte() {
  int ByteRead;
  // Wait until there is data
  while (!Serial.available());
  //Get the number of bytes (characters) available for reading from the serial port.
  //This is data that's already arrived and stored in the serial receive buffer (which holds 64 bytes)
  ByteRead = Serial.read();
  return ByteRead; // read incoming serial data
}

unsigned int delta_wave = 0;
unsigned int theta_wave = 0;
unsigned int low_alpha_wave = 0;
unsigned int high_alpha_wave = 0;
unsigned int low_beta_wave = 0;
unsigned int high_beta_wave = 0;
unsigned int low_gamma_wave = 0;
unsigned int mid_gamma_wave = 0;

void read_waves(int i) {
  delta_wave = read_3byte_int(i);
  i += 3;
  theta_wave = read_3byte_int(i);
  i += 3;
  low_alpha_wave = read_3byte_int(i);
  i += 3;
  high_alpha_wave = read_3byte_int(i);
  i += 3;
  low_beta_wave = read_3byte_int(i);
  i += 3;
  high_beta_wave = read_3byte_int(i);
  i += 3;
  low_gamma_wave = read_3byte_int(i);
  i += 3;
  mid_gamma_wave = read_3byte_int(i);
}

int read_3byte_int(int i) {
  return ((payloadData[i] << 16) + (payloadData[i + 1] << 8) + payloadData[i + 2]);
}

void loop() {
  // Look for sync bytes
  // Byte order: 0xAA, 0xAA, payloadLength, payloadData,
  // Checksum (sum all the bytes of payload, take lowest 8 bits, then bit inverse on lowest
  if (ReadOneByte() == 0xAA) {
    if (ReadOneByte() == 0xAA) {
      payloadLength = ReadOneByte();
      if (payloadLength > 169) //Payload length can not be greater than 169
        return;
      payloadChecksum = 0;
      for (int i = 0; i < payloadLength; i++) {     //loop until payload length is complete
        payloadData[i] = ReadOneByte();             //Read payload
        payloadChecksum += payloadData[i];
      }
      checksum = ReadOneByte();                     //Read checksum byte from stream
      payloadChecksum = 255 - payloadChecksum;      //Take one’s compliment of generated checksum
      if (checksum == payloadChecksum) {
        poorQuality = 200;
        attention = 0;
        meditation = 0;
      }
      brainwave = false;
      for (int i = 0; i < payloadLength; i++) { // Parse the payload
        switch (payloadData[i]) {
          case 02:
            i++;
            poorQuality = payloadData[i];
            bigPacket = true;
            break;
          case 04:
            i++;
            attention = payloadData[i];
            break;
          case 05:
            i++;
            meditation = payloadData[i];
            break;
          case 0x80:
            i = i + 3;
            break;
          case 0x83:                         // ASIC EEG POWER INT
            i++;
            brainwave = true;
            byte vlen = payloadData[i];
            //mySerial.print(vlen, DEC);
            //mySerial.println();
            read_waves(i + 1);
            i += vlen; // i = i + vlen
            break;
        }                                 // switch
      }                                   // for loop

/*
      if (bigPacket) {
        if (poorQuality == 0) {
        }
        else {                            // do nothing
        }
      }
*/
      if (brainwave && attention > 0 && attention < 100) {
        mySerial.print(attention, DEC);
        mySerial.println();

        Serial.print(attention, DEC);
        Serial.println();
      }
    }
  }
}

Often, the serial monitor stops display new values that stems from the Mindwave. Any suggestions on how I could modify the code to get it to run more smoothly will be greatly appreciated.

2 Answers 2

1

No use in reinventing the wheel!

Brain is an Arduino Library for parsing data from Neurosky-based EEG headsets. Tested units include the Star Wars Force Trainer and Mattel MindFlex.

It's designed to make it simple to send out an ASCII string of comma-separated values over serial, or to access processed brain wave information directly in your Arduino sketch. See the examples for code demonstrating each use case.

https://github.com/kitschpatrol/Brain

Install the library, and set it up like this:

#include <Brain.h>

// Set up the brain parser, pass it the hardware serial object you want to listen on.
Brain brain(Serial);

void setup() {
// Start the hardware serial.
Serial.begin(9600);
}

void loop() {
// Expect packets about once per second.
// The .readCSV() function returns a string (well, char*) listing the most recent brain data, in the following format:
// "signal strength, attention, meditation, delta, theta, low alpha, high alpha, low beta, high beta, low gamma, high gamma"    
    if (brain.update()) {
    Serial.println(brain.readErrors());
    Serial.println(brain.readCSV());
    }
}

Can't guarantee that it will work, but if you have the TX pin of the bluetooth hooked up to the RX pin of the Arduino I'm pretty sure it will work.

0

I have just spotted this question because I had a similar issue: the while condition reading Serial data is blocking.

I've rewritten the parsing part a little bit, which you can see here.

This makes it smoother to run things in parallel, but it takes about the same time to accumulate a big packet of brainwave data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.