Hello, I am working on acquiring raw EEG signals using the MindWave Mobile 2 with Arduino. Although I have the code, I am not confident in the accurac

/*
Sabratha Faculty of Engineering
Electrical & Electronic Eng Department
1- Sokina Kenan
2- Abdulhamed Essed
9/October/2019
^_^
*/

const int buzzer = 6;
int BAUDRATE = 57600;

int counter = 0;

// ===== Totals =====
unsigned long total_attention = 0;
unsigned long total_meditation = 0;
unsigned long total_theta = 0;
unsigned long total_delta = 0;
unsigned long total_low_alpha = 0;
unsigned long total_high_alpha = 0;
unsigned long total_low_beta = 0;
unsigned long total_high_beta = 0;
unsigned long total_low_gamma = 0;
unsigned long total_mid_gamma = 0;
long total_raw_EEG = 0;

// ===== Averages =====
unsigned long ave_attention;
unsigned long ave_meditation;
unsigned long ave_theta;
unsigned long ave_delta;
unsigned long ave_low_alpha;
unsigned long ave_high_alpha;
unsigned long ave_low_beta;
unsigned long ave_high_beta;
unsigned long ave_low_gamma;
unsigned long ave_mid_gamma;
long ave_raw_EEG;

// ===== ThinkGear Variables =====
byte payloadChecksum = 0;
byte checksum = 0;
int payloadLength = 0;
byte payloadData[64] = {0};

byte poorQuality = 0;
byte attention = 0;
byte meditation = 0;

boolean bigPacket = false;
boolean brainwave = false;

// ===== EEG Bands =====
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;

// ===== RAW EEG =====
int raw_EEG = 0;

// ===============================
void setup() {
pinMode(buzzer, OUTPUT);
Serial.begin(57600);
delay(500);
Serial.println("Communicating...");
}

// ===============================
byte ReadOneByte() {
while (!Serial.available());
return Serial.read();
}

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

// ===============================
int read_2byte_signed(int i) {
int value = ((int)payloadData[i] << 8) | payloadData[i + 1];
if (value > 32767) value -= 65536;
return value;
}

// ===============================
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);
}

// ===============================
void loop() {

if (ReadOneByte() == 0xAA) {
if (ReadOneByte() == 0xAA) {

  payloadLength = ReadOneByte();
  if (payloadLength > 169) return;

  payloadChecksum = 0;
  for (int i = 0; i < payloadLength; i++) {
    payloadData[i] = ReadOneByte();
    payloadChecksum += payloadData[i];
  }

  checksum = ReadOneByte();
  payloadChecksum = 255 - payloadChecksum;
  if (checksum != payloadChecksum) return;

  brainwave = false;

  for (int i = 0; i < payloadLength; i++) {
    switch (payloadData[i]) {

      case 0x02:
        poorQuality = payloadData[++i];
        bigPacket = true;
        break;

      case 0x04:
        attention = payloadData[++i];
        break;

      case 0x05:
        meditation = payloadData[++i];
        break;

      case 0x80:          // ===== RAW EEG =====
        i++;
        raw_EEG = read_2byte_signed(i);
        i++;
        break;

      case 0x83:
        i++;
        brainwave = true;
        read_waves(i + 1);
        i += payloadData[i];
        break;
    }
  }

  if (brainwave && attention > 0 && attention < 100) {

    counter++;

    total_attention += attention;
    total_meditation += meditation;
    total_theta += theta_wave;
    total_delta += delta_wave;
    total_low_alpha += low_alpha_wave;
    total_high_alpha += high_alpha_wave;
    total_low_beta += low_beta_wave;
    total_high_beta += high_beta_wave;
    total_low_gamma += low_gamma_wave;
    total_mid_gamma += mid_gamma_wave;
    total_raw_EEG += raw_EEG;

    ave_attention = total_attention / counter;
    ave_meditation = total_meditation / counter;
    ave_theta = total_theta / counter;
    ave_delta = total_delta / counter;
    ave_low_alpha = total_low_alpha / counter;
    ave_high_alpha = total_high_alpha / counter;
    ave_low_beta = total_low_beta / counter;
    ave_high_beta = total_high_beta / counter;
    ave_low_gamma = total_low_gamma / counter;
    ave_mid_gamma = total_mid_gamma / counter;
    ave_raw_EEG = total_raw_EEG / counter;

    Serial.println("=================================");
    Serial.print("RAW EEG: "); Serial.println(raw_EEG);
    Serial.print("Attention: "); Serial.println(attention);
    Serial.print("Meditation: "); Serial.println(meditation);
    Serial.print("Theta: "); Serial.println(theta_wave);
    Serial.print("Delta: "); Serial.println(delta_wave);
    Serial.println("=================================");
  }

  if (theta_wave < 20000)
    digitalWrite(buzzer, HIGH);
  else
    digitalWrite(buzzer, LOW);
}

}
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post full error messages in code tags as it makes it easier to scroll through them and copy them for examination

To post the error message, click the "COPY ERROR MESSAGES" button in the IDE, paste the copied messages in a reply here, select all of it and click the < CODE > icon above the reply editor to add the code tags and then save the reply

waveforms typically need to be read using an analog-to-digital converter and to do frequency analysis, need to be read periodically. Looks like your just reading a serial packet of data with an 0xAA 0xAA prefix and checksum.

the format of the pack is not obvious, but it looks like the packet may contain various types of data that is prefixed with IDs (e.g. 0x02, 0x04, 0x05, 0x80, 0x83) and the 0x80 ID indicates a 2-byte raw EEG value or is ID 0x83 indicating a waveform.

hard to provide any help without knowing the instruments being used and the format of the data packet.

typically easier to read a message header, determine the amount of data and read and process a complete message

Besides doing what @UKHeliBob requested, you need to state your problem or question.

Put your code in a code block...


It should look like this...

/*
  Sabratha Faculty of Engineering
  Electrical & Electronic Eng Department
  1- Sokina Kenan
  2- Abdulhamed Essed
  9/October/2019
  ^_^
*/

const int buzzer = 6;
int BAUDRATE = 57600;

int counter = 0;

// ===== Totals =====
unsigned long total_attention = 0;
unsigned long total_meditation = 0;
unsigned long total_theta = 0;
unsigned long total_delta = 0;
unsigned long total_low_alpha = 0;
unsigned long total_high_alpha = 0;
unsigned long total_low_beta = 0;
unsigned long total_high_beta = 0;
unsigned long total_low_gamma = 0;
unsigned long total_mid_gamma = 0;
long total_raw_EEG = 0;

// ===== Averages =====
unsigned long ave_attention;
unsigned long ave_meditation;
unsigned long ave_theta;
unsigned long ave_delta;
unsigned long ave_low_alpha;
unsigned long ave_high_alpha;
unsigned long ave_low_beta;
unsigned long ave_high_beta;
unsigned long ave_low_gamma;
unsigned long ave_mid_gamma;
long ave_raw_EEG;

// ===== ThinkGear Variables =====
byte payloadChecksum = 0;
byte checksum = 0;
int payloadLength = 0;
byte payloadData[64] = {0};

byte poorQuality = 0;
byte attention = 0;
byte meditation = 0;

boolean bigPacket = false;
boolean brainwave = false;

// ===== EEG Bands =====
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;

// ===== RAW EEG =====
int raw_EEG = 0;

// ===============================
void setup() {
  pinMode(buzzer, OUTPUT);
  Serial.begin(57600);
  delay(500);
  Serial.println("Communicating...");
}

// ===============================
byte ReadOneByte() {
  while (!Serial.available());
  return Serial.read();
}

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

// ===============================
int read_2byte_signed(int i) {
  int value = ((int)payloadData[i] << 8) | payloadData[i + 1];
  if (value > 32767) value -= 65536;
  return value;
}

// ===============================
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);
}

// ===============================
void loop() {

  if (ReadOneByte() == 0xAA) {
    if (ReadOneByte() == 0xAA) {

      payloadLength = ReadOneByte();
      if (payloadLength > 169) return;

      payloadChecksum = 0;
      for (int i = 0; i < payloadLength; i++) {
        payloadData[i] = ReadOneByte();
        payloadChecksum += payloadData[i];
      }

      checksum = ReadOneByte();
      payloadChecksum = 255 - payloadChecksum;
      if (checksum != payloadChecksum) return;

      brainwave = false;

      for (int i = 0; i < payloadLength; i++) {
        switch (payloadData[i]) {

          case 0x02:
            poorQuality = payloadData[++i];
            bigPacket = true;
            break;

          case 0x04:
            attention = payloadData[++i];
            break;

          case 0x05:
            meditation = payloadData[++i];
            break;

          case 0x80:          // ===== RAW EEG =====
            i++;
            raw_EEG = read_2byte_signed(i);
            i++;
            break;

          case 0x83:
            i++;
            brainwave = true;
            read_waves(i + 1);
            i += payloadData[i];
            break;
        }
      }

      if (brainwave && attention > 0 && attention < 100) {

        counter++;

        total_attention += attention;
        total_meditation += meditation;
        total_theta += theta_wave;
        total_delta += delta_wave;
        total_low_alpha += low_alpha_wave;
        total_high_alpha += high_alpha_wave;
        total_low_beta += low_beta_wave;
        total_high_beta += high_beta_wave;
        total_low_gamma += low_gamma_wave;
        total_mid_gamma += mid_gamma_wave;
        total_raw_EEG += raw_EEG;

        ave_attention = total_attention / counter;
        ave_meditation = total_meditation / counter;
        ave_theta = total_theta / counter;
        ave_delta = total_delta / counter;
        ave_low_alpha = total_low_alpha / counter;
        ave_high_alpha = total_high_alpha / counter;
        ave_low_beta = total_low_beta / counter;
        ave_high_beta = total_high_beta / counter;
        ave_low_gamma = total_low_gamma / counter;
        ave_mid_gamma = total_mid_gamma / counter;
        ave_raw_EEG = total_raw_EEG / counter;

        Serial.println("=================================");
        Serial.print("RAW EEG: "); Serial.println(raw_EEG);
        Serial.print("Attention: "); Serial.println(attention);
        Serial.print("Meditation: "); Serial.println(meditation);
        Serial.print("Theta: "); Serial.println(theta_wave);
        Serial.print("Delta: "); Serial.println(delta_wave);
        Serial.println("=================================");
      }

      if (theta_wave < 20000)
        digitalWrite(buzzer, HIGH);
      else
        digitalWrite(buzzer, LOW);
    }
  }
}

"... I have the code, I am not confident in the accuracy..."

@sokina_kenan - How do you determine if the code does or does not have accuracy?

/*
Sabratha Faculty of Engineering
Electrical & Electronic Eng Department
1- Sokina Kenan
2- Abdulhamed Essed
9/October/2019
^_^
*/

const int buzzer = 6;
int BAUDRATE = 57600;

int counter = 0;

// ===== Totals =====
unsigned long total_attention = 0;
unsigned long total_meditation = 0;
unsigned long total_theta = 0;
unsigned long total_delta = 0;
unsigned long total_low_alpha = 0;
unsigned long total_high_alpha = 0;
unsigned long total_low_beta = 0;
unsigned long total_high_beta = 0;
unsigned long total_low_gamma = 0;
unsigned long total_mid_gamma = 0;
long total_raw_EEG = 0;

// ===== Averages =====
unsigned long ave_attention;
unsigned long ave_meditation;
unsigned long ave_theta;
unsigned long ave_delta;
unsigned long ave_low_alpha;
unsigned long ave_high_alpha;
unsigned long ave_low_beta;
unsigned long ave_high_beta;
unsigned long ave_low_gamma;
unsigned long ave_mid_gamma;
long ave_raw_EEG;

// ===== ThinkGear Variables =====
byte payloadChecksum = 0;
byte checksum = 0;
int payloadLength = 0;
byte payloadData[64] = {0};

byte poorQuality = 0;
byte attention = 0;
byte meditation = 0;

boolean bigPacket = false;
boolean brainwave = false;

// ===== EEG Bands =====
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;

// ===== RAW EEG =====
int raw_EEG = 0;

// ===============================
void setup() {
pinMode(buzzer, OUTPUT);
Serial.begin(57600);
delay(500);
Serial.println("Communicating...");
}

// ===============================
byte ReadOneByte() {
while (!Serial.available());
return Serial.read();
}

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

// ===============================
int read_2byte_signed(int i) {
int value = ((int)payloadData[i] << 8) | payloadData[i + 1];
if (value > 32767) value -= 65536;
return value;
}

// ===============================
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);
}

// ===============================
void loop() {

if (ReadOneByte() == 0xAA) {
if (ReadOneByte() == 0xAA) {

  payloadLength = ReadOneByte();
  if (payloadLength > 169) return;

  payloadChecksum = 0;
  for (int i = 0; i < payloadLength; i++) {
    payloadData[i] = ReadOneByte();
    payloadChecksum += payloadData[i];
  }

  checksum = ReadOneByte();
  payloadChecksum = 255 - payloadChecksum;
  if (checksum != payloadChecksum) return;

  brainwave = false;

  for (int i = 0; i < payloadLength; i++) {
    switch (payloadData[i]) {

      case 0x02:
        poorQuality = payloadData[++i];
        bigPacket = true;
        break;

      case 0x04:
        attention = payloadData[++i];
        break;

      case 0x05:
        meditation = payloadData[++i];
        break;

      case 0x80:          // ===== RAW EEG =====
        i++;
        raw_EEG = read_2byte_signed(i);
        i++;
        break;

      case 0x83:
        i++;
        brainwave = true;
        read_waves(i + 1);
        i += payloadData[i];
        break;
    }
  }

  if (brainwave && attention > 0 && attention < 100) {

    counter++;

    total_attention += attention;
    total_meditation += meditation;
    total_theta += theta_wave;
    total_delta += delta_wave;
    total_low_alpha += low_alpha_wave;
    total_high_alpha += high_alpha_wave;
    total_low_beta += low_beta_wave;
    total_high_beta += high_beta_wave;
    total_low_gamma += low_gamma_wave;
    total_mid_gamma += mid_gamma_wave;
    total_raw_EEG += raw_EEG;

    ave_attention = total_attention / counter;
    ave_meditation = total_meditation / counter;
    ave_theta = total_theta / counter;
    ave_delta = total_delta / counter;
    ave_low_alpha = total_low_alpha / counter;
    ave_high_alpha = total_high_alpha / counter;
    ave_low_beta = total_low_beta / counter;
    ave_high_beta = total_high_beta / counter;
    ave_low_gamma = total_low_gamma / counter;
    ave_mid_gamma = total_mid_gamma / counter;
    ave_raw_EEG = total_raw_EEG / counter;

    Serial.println("=================================");
    Serial.print("RAW EEG: "); Serial.println(raw_EEG);
    Serial.print("Attention: "); Serial.println(attention);
    Serial.print("Meditation: "); Serial.println(meditation);
    Serial.print("Theta: "); Serial.println(theta_wave);
    Serial.print("Delta: "); Serial.println(delta_wave);
    Serial.println("=================================");
  }

  if (theta_wave < 20000)
    digitalWrite(buzzer, HIGH);
  else
    digitalWrite(buzzer, LOW);
}

}
}
The

raw EEG wave values are always 512 or 725.

@sokina_kenan Did you miss the request to post your code using code tags ?

what instrument are you using to generate the samples you are receiving over the serial interface?

MindWave Mobile 2 with Arduino

Please edit your posts to add code tags, a link to the sensor, and a wiring diagram or photo of the setup.

the protocol description, pg 12, describes the RAW Wave Values as

0 0x80 2 RAW Wave Value: a single big-endian
           16-bit two's-compliment signed value
           (high-order byte followed by
           low-order byte) (-32768 to 32767)

which says that ffollowing the RAW Wave Value code (0x80) is a 2 byte length followed by that many bytes of data

but it looks like your code is only reading 2 bytes

      case 0x80:          // ===== RAW EEG =====
        i++;
        raw_EEG = read_2byte_signed(i);
        i++;
        break;

your code should read the next 2 bytes, the # of data bytes and then read that many bytes into your buffer

since the data is big-endian and the AT328p is little-endian, that data could be directly copied into a int16_t buffer, but the bytes of each int16_t need to be swapped