
PROCESSING, ARDUINO AND EEG PART 2
FROM EEG TO ARDUINO
In this sketch, we are going to make the connection between Arduino and EEG Headset through Processing. Most of the code that we need is already written and explained in the previous posts.
To explain the process a bit more, the EEG Headset will send the brainwave data package to ThinkGear Connector which will share this information with Processing. The attention values will be analyzed in Processing and will be displayed in the display window. If the value is higher than 50, Processing will command Arduino to activate Pin 13 and deactivate it if the value is lower than 50.
As mentioned above, there will be two outputs; the display window and the LED connected to the Arduino Pin 13.
Here is the final code:
/* Developed by Alis Design Nov, 2013 Revisited Nov 2017 www.alis.design based on the library developed by Andreas Borg http://crea.tion.to/ */ //================================= neurosky import neurosky.*; import org.json.*; ThinkGearSocket neuroSocket; int attention, meditation, delta, theta, low_alpha, high_alpha, low_beta, high_beta, low_gamma, mid_gamma, sig, blinkStrength; //================================= arduino import processing.serial.*; import cc.arduino.*; Arduino arduino; int ledPin = 13; //================================= font PFont f; //================================= init void setup() { size(500, 500); // initialize the font object f = createFont("Arial", 16, true); // Arial, 16 point, anti-aliasing on arduino = new Arduino(this, Arduino.list()[2], 57600); // ledPin as Output arduino.pinMode(ledPin, Arduino.OUTPUT); // initialize the headset ThinkGearSocket neuroSocket = new ThinkGearSocket(this); try { neuroSocket.start(); } catch (Exception e) { println("Is ThinkGear running??"); } } void clearBackground() { background(40); } //================================= frame loop void draw() { clearBackground(); textFont(f, 80); // Specify font to be used fill(255); // Specify font color text(attention, width/2, 100); // Display Text textAlign(CENTER); // if the attention level is above 50, the LED will be turned on which is connected to pin 13 if (attention > 50) { arduino.digitalWrite(ledPin, Arduino.HIGH); } else { arduino.digitalWrite(ledPin, Arduino.LOW); } } //================================= neurosky functions void poorSignalEvent(int sigLevel) { sig=sigLevel; } void attentionEvent(int attentionLevel) { attention = attentionLevel; } void meditationEvent(int meditationLevel) { meditation = meditationLevel; } void blinkEvent(int blinkStrengthLevel) { blinkStrength = blinkStrengthLevel; } void eegEvent(int deltaLevel, int thetaLevel, int low_alphaLevel, int high_alphaLevel, int low_betaLevel, int high_betaLevel, int low_gammaLevel, int mid_gammaLevel) { delta = deltaLevel; theta = thetaLevel; low_alpha = low_alphaLevel; high_alpha = high_alphaLevel; low_beta = low_betaLevel; high_beta = high_betaLevel; low_gamma = low_gammaLevel; mid_gamma = mid_gammaLevel; } void rawEvent(int[] rawArray) { } void stop() { neuroSocket.stop(); super.stop(); }
and here is the sketch folder.
In the next sketch, we are going to control a DC motor with Arduino and EEG Headset.