Hackster is hosting Hackster Holidays, Ep. 1: Welcome & Giveaway Drawing. Watch now!Tune in to Hackster Holidays, Ep. 1 now!
Tamas Imets
Published © CERN-OHL

Use the Force... Or your Brainwaves?

Thought controlled system with personal webserver and 3 working functions: robot controller, home automation and PC mouse controller.

AdvancedFull instructions provided7 days62,530
Use the Force... Or your Brainwaves?

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
Used for the robot.
×1
Arduino Mega 2560
Arduino Mega 2560
Tha brain of the project...
×1
Arduino Leonardo
Arduino Leonardo
Mouse Controller
×1
Photon
Particle Photon
Webserver Data Logger
×1
Necomimi Brainwave Cat Ears
EEG Sensor
×1
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
Buy this if you dont want to build your own motor driver.
×1
Relay Module
Purchase this module if you don't want to solder circuits.
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×1
HC-06 Bluetooth Module
×1
LED (generic)
LED (generic)
×3
Resistor 330 ohm
Resistor 330 ohm
×3
General Purpose Transistor NPN
General Purpose Transistor NPN
×3
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1

Software apps and online services

Brain Visualizer
It's needed only for experiments.

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Server

Use this schematic to make the main server.

Robot Circuit

Schematic for the DIY IR robot

Relay Module Circuit

Schematic for the DIY 3 channel relay module. Used Arduino pins: D37, D38, D39.

L293D

Schematic for the motor driver. Connect the pins to the Arduino.

The server

Schematic for the main server's electronics.

Robot

Schematic for the Arduino UNO robot

Robot Circuit

Schematic for the IR robot

Server Circuit

Use this schematic to make the main server.

Headset Circuit

Schematic for the headset's electronics

Code

Particle Photon Code

Arduino
Copy this in your browser IDE then upload to the Particle Photon. Open the dashboard to see the results in real time.
//this is the code for the webserver for a "Use the Force... Or your Brainwaves?" project
//creates a statistics for the user about his/her brainwaves meditation and attention level
//thanked to the fully functioning dashboard of the Particle the code is very simple
int att;
int med;
int function;

void setup() {
    Serial.begin(9600);
    
}
void loop() {
    
    // Publish data
    Spark.publish("Attention", (att) + "%");
    delay(2000);
    Spark.publish("Meditation", (med) + "%");
    delay(2000);
    Spark.publish("Last Function", (function) + "");
    delay(2000);
    
    if (Serial.available > 0) {
        char c = Serial.read();
    }
    
    delay(10);
    
    switch(c) {
        case'a':
        att = 10;
        break;
        case'b':
        att = 20;
        break;
        case'c':
        att = 30;
        break;
        case'd':
        att = 40;
        break;
        case'e':
        att = 50;
        break;
        case'f':
        att = 60;
        break;
        case'g':
        att = 70;
        break;
        case'h':
        att = 80;
        break;
        case'i':
        att = 90;
        break;
        case'j':
        att = 100;
        break;
        
        case'k':
        med = 10;
        break;
        case'l':
        med = 20;
        break;
        case'm':
        med = 30;
        break;
        case'n':
        med = 40;
        break;
        case'o':
        med = 50;
        break;
        case'p':
        med = 60;
        break;
        case'q':
        med = 70;
        break;
        case'r':
        med = 80;
        break;
        case's':
        med = 90;
        break;
        case't':
        med = 100;
        break;
        
        case'x':
        function = "Home Automation"
        break;
        
        case'y':
        function = "Robot Controller Mode"
        break;
        
        case'z':
        function = "Mouse Controller"
        break;
    }
}

Robot Controller Code

Arduino
This code controls the robot. Upload this to the Arduino UNO. You can change the IR codes if you want.
//Used for "Use the Force or your Brainwaves?" project


#include <IRremote.h>
int IRpin = 3;
IRrecv irrecv(IRpin);
decode_results results;
int motor_pin1 = 4;
int motor_pin2 = 5;
int motor_pin3 = 6;
int motor_pin4 = 7;

void setup ()
{
  irrecv.enableIRIn();
  pinMode(motor_pin1,OUTPUT);
  pinMode(motor_pin2,OUTPUT);
  pinMode(motor_pin3,OUTPUT);
  pinMode(motor_pin4,OUTPUT);
  Serial.begin(9600);
}

void loop() {
   if (irrecv.decode(&results)) 
    {
         irrecv.resume();   // Receive the next value
    }
  
   if (results.value == 0xFF18E7)  //if gets this code goes forward
     {
       digitalWrite(motor_pin1,LOW);
       digitalWrite(motor_pin2,HIGH);
       digitalWrite(motor_pin3,LOW);
       digitalWrite(motor_pin4,HIGH);
    {
          Serial.println("Go Forward!");
      }
    }
 
   if (irrecv.decode(&results))
{
         irrecv.resume();   // Receive the next value
    }
    if (results.value == 0xFF4AB5)  //if gets this code goes backward
     {
       digitalWrite(motor_pin1,HIGH);
       digitalWrite(motor_pin2,LOW);
       digitalWrite(motor_pin3,HIGH);
       digitalWrite(motor_pin4,LOW);
       {
          Serial.println("Go Backward!");
      }
    }

if (irrecv.decode(&results))
{
         irrecv.resume();   // Receive the next value
    }
    if (results.value == 0xFF10EF)  //if gets this code turns left
     {
        digitalWrite(motor_pin1,HIGH);      
        digitalWrite(motor_pin2,LOW);      
        digitalWrite(motor_pin3,LOW);
        digitalWrite(motor_pin4,HIGH);
        {
          Serial.println("Turn Left!");
      }
    }

if (irrecv.decode(&results))
{
         irrecv.resume();   // Receive the next value
    }
    if (results.value == 0xFF5AA5)  //if gets this code turns right
     {
         digitalWrite(motor_pin1,LOW);       
         digitalWrite(motor_pin2,HIGH);    
         digitalWrite(motor_pin3,HIGH);
         digitalWrite(motor_pin4,LOW);
         {
          Serial.println("Turn Right!");
      }
    }

if (irrecv.decode(&results))
{
         irrecv.resume();   // Receive the next value
    }
    if (results.value == 0xFF38C7)  //if gets this code stops
     {
         digitalWrite(motor_pin1,LOW);       
         digitalWrite(motor_pin2,LOW);    
         digitalWrite(motor_pin3,LOW);
         digitalWrite(motor_pin4,LOW);
         {
          Serial.println("Stop!");
      }
    }
}

HC_05_Bluetooth.ino

Arduino
Configures HC-05 module.
// HC-05 configuration

void setup() {
  // Start the hardware serial.
  Serial.begin(9600);  // default HC-05 baud rate
  delay(1000);
  Serial.print("AT");
  delay(1000);
  Serial.print("AT+VERSION");
  delay(1000);
  Serial.println("AT+ROLE=1"); //set the HC-05 to master mode
  delay(1000);
  Serial.println(" AT+PAIR=copy here your adress,9"); //now the module should connnect automatically
  delay(1000);
  Serial.print("AT+BAUD7"); // Set baudrate to 576000 - eg Necomimi dafault
   Serial.begin(57600);    //
  delay(1000);

}

void loop() {

}

Mouse Controller Code

Arduino
Upload this to the Arduino Leonardo. Controls the cursor on your computer, depending on the signal that gets from the Arduino Mega.
//Code by Imets Tamas for the Use the Force... Or your Brainwaves? project
//Moves the mouse cursor through serial signals

#include <Mouse.h>

void setup() {
  Serial.begin(9600);
  // initialize mouse control:
  Mouse.begin();
}

void loop() {
  // use serial input to control the mouse:
  if (Serial.available() > 0) {
    char inByte = Serial.read();

    switch (inByte) {
      case 'u':
        // move mouse up
        Mouse.move(0, -8);
        break;
      case 'd':
        // move mouse down
        Mouse.move(0, 8);
        break;
      case 'l':
        // move mouse left
        Mouse.move(-8, 0);
        break;
      case 'r':
        // move mouse right
        Mouse.move(8, 0);
        break;
      case 'c':
        // perform mouse left click
        Mouse.click(MOUSE_LEFT);
        break;
    }
  }
}

Arduino_Mega_Server

Arduino
Analzyzing brainwaves, controls everything...
/*    Arduino Mega Code for "A Tool for the Mind" project by Imets Tams, 2nd of February, 2016
  This code analyzes and organizes then sends forward incoming bluetooth serial signals from the brainwave sensor
  Feel free to edit, but don't forget to credit,  For the variable values
  I used the characters of family's name, you can change them if you want: I. M. E.  T. S.
  See more details at http://www.instructables.com/member/Imetomi/ or https://www.hackster.io/Imetomi */


#include <VirtualWire.h> // include the library 
#include <SoftwareSerial.h> // include the library 
#include <Brain.h> // include the library 
#include <LiquidCrystal.h> // include the library 
#include <IRremote.h>

int led = 13;
int medled = 50; //the LED to shows the meditation level
int attled = 51; //the LED to shows the attention level
int blinkled = 52;
char robot;

IRsend irsend; //IR led to pin 3

SoftwareSerial mouse(18, 19); //RX, TX this goes to the Leaonardo Board
SoftwareSerial webserver(16, 15); //goes to the Particle Photon to create a web server
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
Brain brain(Serial); //the Brainwaves are read with the help of the bluetooth module through hardware serial

int closedeye = (brain.readLowAlpha()); //to detect blink
int attention = (brain.readAttention()); //to read attention
int brainsignal = (brain.readSignalQuality()); //to read signal  quality
int meditation = (brain.readMeditation()); //to detect meditation
int alpha = (brain.readHighAlpha()); //to detect closed eye
int lightatt;
int lightmed;
int blinklength;
int prevsignal = 0;


void setup() {
  //here we're going to set the main settings:

  mouse.begin(9600); //set the baud rate
  webserver.begin(9600); //set the baud rate
  Serial.begin(57600); //set the baud rate (57600 is the Necomimi's bit/seconds rate)
  lcd.begin(16, 2); //start the LCD screen

  pinMode(led, OUTPUT); //set pinmodes for the leds and relays
  pinMode(attled, OUTPUT); //set pinmodes for the leds and relays
  pinMode(medled, OUTPUT); //set pinmodes for the leds and relays
  pinMode(blinkled, OUTPUT); //set pinmodes for the leds and relays
  pinMode(37, OUTPUT); //set pinmodes for the leds and relays
  pinMode(38, OUTPUT); //set pinmodes for the leds and relays
  pinMode(39, OUTPUT); //set pinmodes for the leds and relays

  lightatt = map(attention, 0, 100, 0, 255); //map the attention level for analogWrite the LED
  lightmed = map(meditation, 0, 100, 0, 255); //map the attention level for analogWrite the LED

}

void robotmode() {
  if (brain.update()) {
    if ((blinklength > 0) && (blinklength < 2000)) { //if you close eyour eyes for 2 seconds
      loop(); //you'll exit the robot controller mode
    }
    if (attention > 70) {
      irsend.sendNEC(0xFF18E7, 32); //go forward
      webserver.println("g");
      delay(40);    
    }

    if (meditation > 70) {
      irsend.sendNEC(0xFF10EF, 32); //spin
      webserver.println("q");
      delay(40);  
    }
  }
}

void loop() {
 if (brain.update()){
    analogWrite(attled, lightatt); //analog write the attention led to show the actual attention level
    //the more focused you are, the stronger thee LED lights
    analogWrite(medled, lightmed); //analog write the attention led to show the actual attention level
    //the higher meditation level will light up the LED

    if ((closedeye > 30000) && (alpha > 30000)) { //we use 2 values to be sure the user's eye is closed
      digitalWrite(blinkled, HIGH); //in english: if eyes are closed the LED will light up
      blinklength = millis(); // count milliseconds if eye closed
    }
    else if ((closedeye < 30000) && (alpha < 30000)) {
      digitalWrite(blinkled, LOW); //in english: if eyes are opened the LED will turn off
      blinklength = 0;
    }

    if ((blinklength > 0) && (blinklength < 2000)) { //if you close eyour eyes for 2 seconds
      webserver.println("a");
      delay(40);
      robotmode(); //you'll be able to control the robots
      
    }

    if ((blinklength > 2001) && (blinklength < 4000)) { //if you close eyour eyes for 3-4 seconds
      webserver.println("a");
      delay(40);
      relay(); //you'll be able to control the the home automation system
    }

    if ((blinklength > 4001) && (blinklength < 6000)) { //if you close eyour eyes for 4-6 seconds
      webserver.println("a");
      delay(40);
      mousecontrol(); //you'll be able to control the mouse
    }
   
   Serial.println(brain.readCSV()); //print the data on serial monitor, not necessary, it's only for testing
  }
}

void relay() {
  webserver.println("");
  if (brain.update()) {
   if ((blinklength > 0) && (blinklength < 2000)) { //if you close eyour eyes for 2 seconds
      loop(); //you'll exit the relay controller mode
    }

   if(attention > 70) { //turn on relay1
    digitalWrite(37, HIGH);
    webserver.println("g");
    prevsignal = 1;
   }

   if(meditation > 70) { //turn on relay2
    digitalWrite(37, HIGH);
    webserver.println("q");
    prevsignal = 1;
   }

   if(1000 > blinklength > 0) { //turn on relay3
    digitalWrite(37, HIGH);
    webserver.println("qj");
    prevsignal = 1;
   }

   if((attention > 70) && (prevsignal == 1)) {
    digitalWrite(37, LOW);
    webserver.println("q");
    prevsignal = 1; //check if is already turned on, then turn off
   }

   if((meditation > 70) && (prevsignal == 1)) {
    digitalWrite(37, LOW);
    webserver.println("qj");
    prevsignal = 1; //check if is already turned on, then turn off
   }

   if((1000 > blinklength > 0) && (prevsignal == 1)) {
    digitalWrite(37, LOW);
    webserver.println("a");
    prevsignal = 1; //check if is already turned on, then turn off
   }
   
   }
}

void mousecontrol() { //you'll need a few hours of practice to control the cursor perfectly
  webserver.println("z"); //forward data to the webserver
 if(brain.update()) {
  
   if (0 < attention < 25) {
    mouse.println('l'); //left
    webserver.println("a");
   }

   if (26 < attention < 50) {
    mouse.println('r'); //right
    webserver.println("c");
   }

   if (51 < attention < 75) {
    mouse.println('u'); //up
    webserver.println("f");
   }
   
   if (76 < attention < 100) {
    mouse.println('d'); //down
    webserver.println("h");
   }

     if (meditation > 70) {
    mouse.println('c'); //click
    webserver.println("t");
   }
 }
}

RobotControllerHeadset.ino

Arduino
For those who want to build an IR robot.
#include <IRremote.h>
#include <IRremoteInt.h>
#include <Brain.h>
IRsend irsend;
Brain brain(Serial);
const int ledPin = 3; 
long interval = 500; 
long previousMillis = 0;
int ledState = LOW; 
int medValue;
void setup() {
 // Set up the LED pin.
 pinMode(ledPin, OUTPUT);
 
 // Start the hardware serial.
 Serial.begin(57600);
}
void loop() {
 // Expect packets about once per second.
 if (brain.update()) {
 Serial.println(brain.readCSV());
 
 // Attention runs from 0 to 100.
 medValue = brain.readMeditation();
 }
 
 // Make sure we have a signal.
 if(brain.readSignalQuality() == 0) {
 
 // Send a signal to the LED.
 if (medValue > 65) {
irsend.sendNEC(0xFF10EF, 32); 
 delay(40);
 }
 if (brain.readAttention() > 65) {
irsend.sendNEC(0xFF18E7, 32); 
 delay(40);
 }
 } 
 
}

Brain Library

Used to measure attention and meditation level.

SoftwareSerial Library

Used to ensure multiple communication pins.

IRremote Library

Decoding and sending IR signals.

Credits

Tamas Imets

Tamas Imets

5 projects • 72 followers
My name is Tamas and I am 20 years old guy who likes to build AI related projects.

Comments