Project tutorial

Use the Force... Or your Brainwaves? © CERN-OHL

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

  • 20,170 views
  • 15 comments
  • 78 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)

Apps and online services

Brain Visualizer
It's needed only for experiments.

About this project

As a kid I always wanted to have a special ability, like moving objects with thoughts or flying. I tried to move objects with “the power of my mind” of course with no success. Well, yes, I was and I am a huge Star Wars fan, but I also like electronics and programming, so why not combine these two things to build an incredible project?

You can find all parts, codes and libraries with links at the Hardware and Software section.

The main concept is that using different brainwaves the user will be able to control a robot, the cursor on his PC or turn on/off lights in his home, while a microcontroller (Particle Photon) creates an online analyzing about the user’s brainwaves. This is a perfect method to detect diseases or stress and sleepy statement. This also can be used to help people live better their life, learn to control their emotions and how to be always happy. The human brain needs balance, to be healthly, we should sleep, think, move... Using the Particle Photon and the webserver created by the founders of the Particle we can help people to find their natural brain-balance. A childhood dream guided me to this project, and I'm very happy that I made it.

So yes, with this device you can actually use the Force...

But if we put Star Wars and the Force away for a bit, this device is not only for lazy people or for fans, it’s made to give back an ability for people who are fighting each day with their disability. The robot that I designed works the same way as a wheelchair, the home automation system would help for the user to control lights or TV-s even if he/she can’t move. The PC controller mode can be useful even for those who is perfectly healthly. I think all of us wants to play computer games or just surf on the internet using only thoughts.

Watch this short introduiction video about the story of the project:

Would you like to build your own? Here I’ll explain everything that you should know for this project. I share codes, schematics and this detailed instruction, so warm up your soldering iron…

The Science Behind the Project 

Electroencephalography (EEG) is an electrophysiological monitoring method to record electrical activity of the brain. It is typically noninvasive, with the electrodesplaced along the scalp, although invasive electrodes are sometimes used in specific applications. EEG measures voltage fluctuations resulting from ionic current within the neurons of the brain. Diagnostic applications generally focus on the spectral content of EEG, that is, the type of neural oscillations (popularly called "brain waves") that can be observed in EEG signals. (thank you Wikipedia) But we're going to use two very accurate values

  • Alpha is the frequency range from 7 Hz to 14 Hz. It emerges with closing of the eyes and with relaxation, and attenuates with eye opening or mental exertion.
  • Beta is the frequency range from 15 Hz to about 30 Hz. Low amplitude beta with multiple and varying frequencies is often associated with active, busy or anxious thinking and active concentration.

How does it work?

I used four microcontrollers to bring this project to life: an Arduino Mega, an Arduino Leonardo, an UNO and a Particle Photon. The Mega is the brain of the project that receives signals from the headset, analyzes then forwards commands to the other devices. Transmits every data forward to the Photon that creates the webserver. The Leonardo controls the mouse on a PC and the Uno is used to receive IR (infrared signals) and controls the robot. As I said the device is able to control three different things (or more if you want to program anything else). Now I’ll call these three different things channels. The device switches between these channels if you close your eyes:

  • Close your eyes for 1-2 seconds: switches to home automation featurette
  • Close your eyes for 2-4 seconds: switches to robot controller mode
  • Close your eyes for 1-6 seconds: switches to mouse controller mode

I used relays to make the home automation featurette you can connect anything to them: TV, light bulbs, wall outlets anything you want

The wireless headset:

I love hacking toys so I bought a brainwave sensing toy called Necomimi, that is able to move its ears depending on your brainwaves (attention, meditation). This is the most useless toy that I’ve ever saw, but there’s inside a small chip that reads brainwaves, filters noise and gives a very good signal. Works with UART (Serial) interface, so with some hacking we can use Arduinos to read brainwaves. The role of this headset is to transmit brainwaves wirelessly to the central server. Nobody wants cables on his head so I created this comfortable and user friendly headset.

Go down to see how to take apart and make a wireless Bluetooth headset out of it.

How to use it?

We have two measurable eSense values (NeuroSky values): meditation from 0 to 100 and attention from 0 to 100. The more focused you are, the higher the attention value becomes. The more relaxed you are, the higher the meditation level becomes.

The system is working with thoughts, nothing else is needed only learn how to be focused or relaxed. After a week of experimenting I was able to control my "attention" value very accurate. I can set my attention level consciously to about 15, or 39, 65 and 90. You have to find out how to control your values, we are all different. It worked very well with emotions. Thinking on love, friendship, anger or fear gives a very good contrast in the values.

To power up the system plug a micro USB cable in the Arduino Leonardo. This will also control your mouse (if you want) and ensures 5 volts and 500mA for the system.

Controlling the Home Automation System

If you are switched to home automation mode increase your attention level upper than 70 to turn on first relay, increase meditation level upper than 70 to turn on second relay, and increase both of them upper than 70 to turn on third relay. It's a bit tricky but isn't imposible after some mind-training. If is already turned on, use the same command to turn it off (so if is turned on reach again 70 to turn it off). When the system is done you can connect any high-voltage device (light, TV, PC, cooler fan, anything you want) to the relay module.

Controlling the Mouse

The cursor is controlled with emotions. If attention level is under 25 moves left, if is between 25 and 50 moves right, if is between 50 and 75 moves up and if is between 75 and 100 moves down. You can click by increasing meditation level. As I said before the attention level can be easily controlled with emotions (for me).

Controlling the Robot

The robot has only three different statements: stop, turn left, and move forward. Why only two movement options? Because these two directions are enough to move the robot anywhere you want. Use your meditation level to spin with the robot, and when you reached the direction where you want to move stop the robot then move forward with the attention level. It's a better method to control the robot and even beginners, who can't control their attention/meditation values so good, can play with this robot.

My Plan

This is an older picture about my plan, but succeeded so I’m very happy with the results. The headset transmits BT (Bluetooth) signals to the Arduino Mega that analyzes the incoming bytes and depending on the user’s thought controls the different features. It was very hard to find out the best ways to transmit this lot of data, but I choose Bluetooth instead of WiFi. At first time I wanted use the Particle Photon as a data transmitter, but that little guy got a better role in the making of the webserver. That was the biggest modification in the whole project. (On the picture you can see the actual plan). I used homemade Arduino modules, because I love design my own circuits. You can buy these modules online if you want or build yourself with me.

A half year of learning and experimenting

I worked a lot on this project and yes, I started almost half year ago, but with the Photon things accelerated. It was very easy to use their dashboard. By the way there are many tutorials on the internet how to hack EEG toys, that helped me a bit, but they didn't have any extra functions. So I decided that I'll hack this Necomimi toy and using my creativity I created this device that has much more features than blinking a LED. Hope you'll enjoy!

Let’s get started!

Hacking the Necomimi Toy

We want to modify this EEG toy to transmit data via Bluetooth so first take apart the case. The screws are under the sticker. Remove the sticker and the back of the device and you’ll find inside a small circuit. The one that is under the main circuit is the Neurosky TGAM chip. This is connected with four header pins to the main microcontroller board so take a soldering iron and remove this circuit carefully. Now solder three wires to the GND-pin to the VCC-pin and to the T-pin. The T-pin is the transmitter pin with 57600 baud rate, this sends data packets to our microcontroller, but I connected this directly to a  HC-06 slave BT module. The HC-06 is set to 9600 baud rate but don’t worry we’ll fix this problem. If you soldered the three wires to the you can build in your own rechargeable power source. I used a 500mAh Li-Ion battery, a USB charger circuit, a 5v step up circuit and two resistors (100 ohm and 200 ohm) to ensure a perfect 3.4 volt power supply for the chip and for the Bluetooth module. Follow the schematic to build the circuit that is needed in the headset. If the circuit is done configure the Bluetooth module.

About the chip:

The Neurosky ThinkGear AM is designed to filter, amplify and send data through serial interface. More info here.

The Schematic / Electronics

Follow the instructions and the pictures below and create your own wireless EEG headset.

You'll need a 500mAh Lithium-Ion battery, a 5v voltage step-up module to create a stable voltage then a 100 Ohm and 200 Ohm resistor to reduvóce voltage to stable 3.3 Volts. I soldered female header pins to power up and transmit data to the bluetooth module.

The most important part is to connect the the "T" pin with the "RX" on the Bluetooth module. This brings our project to life.

Configuring Bluetooth modules

HC-06: First load up the sketch named “HC_06_Bluetooth” to an Arduino UNO then connect the Bluetooth module as the schematic shows. I found this schematic on Artronix team's website and seemed useful.


void setup() { 
 
   // Start the hardware serial.
Serial.begin(9600);  // default HC-06 baud rate
delay(1000);
Serial.print("AT");
delay(1000);
Serial.print("AT+VERSION");
delay(1000);
Serial.print("AT+PIN"); // to remove password
delay(1000);
Serial.print("AT+BAUD7"); // Set baudrate to 576000 - eg Necomimi dafault
delay(1000);
Serial.begin(57600);    // 
delay(1000);
}
void loop() {
}

Open your Serial Monitor in the Arduino IDE and wait until the Arduino configures the BT module. Now your Bluetooth module is set to 57600 baud rate. You can try out a lot BCI (Brain Computer Interface) apps, because this hacked bluetooth Necomimi toy will be compatible with every Neurosky apps.


You can use these apps to learn to control your brainwaves and values, like attention or meditation. You can try a lot of cool games, but I recommend these apps (they are compatible with PC, iOS and Android): 

http://store.neurosky.com/products/brainwave-visualizer-android

http://store.neurosky.com/products/eeg-analyzer

http://store.neurosky.com/collections/android/products/blink-camera-android


Then use the "HC_05_Bluetooth" and load up to your Arduino the same way like before. Connect "EN" pin of the HC-05 to the 3v3 of the Arduino. You should write the adress of your HC-06 module in the code. Check the adress of the BT module with an Android smartphone, like me. Replace ":" (double dottles) with "," commas in the code.

And you should change the adress in the code:

20:15:09:15:17:82 ==> 2015,09,151782 This way the HC-05 module can recognise the adress. So just leave some ":", because they aren't neccesary.

// 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+LINK="copy here your adress"); //now the module should connnect automatically
  delay(1000);
  Serial.print("AT+UART=57600,1,0"); // Set baudrate to 576000
   Serial.begin(57600);    //
  delay(1000);
}
void loop() {
}

Finally the two modules should connect to each other. If you have problems click on this link to read more.

The Remote Controlled Robot

To make this inexpensive robot I used 38kHz IR technology, that is used in TV remote controllers. I bought from Amazon a chassis for my robot. The IR receiver is salvaged from an old TV and the Arduino is also from the Amazon. I added links to the parts (see down in the Hardware section).

The motor driver circuit

You’ll need these parts:

  • 2 Screw Terminals
  • L293D IC
  • 3 Header Pin
  • 9 Header Pin (90 degrees)
  • 1k Resistor
  • Red LED
  • Wires
  • A PCB Board

I used some copper wires and following the schematic connected the pins of the IC to the header pins. Doesn't matter that which pin goes to which header pin, just remember that were did you connected them. The LED is connected in series with the resistor and in paralell with the 5v VCC.

Assemble the chassis

This step was very easy, used a screwdriver and some crafting skills I built the frame in five minutes. After this is coming the harder part, we should build a motor driver circuit. To control my motors I choose the L293D IC that can drive two motors. Look at the schematic to build circuit.

Connect the parts to the Arduino

I used jumper wires to connect the sensor and the motor driver to the Arduino.

Arduino Pin ==> Motor Driver

                   4 ==> 15

                   5 ==> 10

                   6 ==> 7

                   7 ==> 2

               VIN ==> 8

                 5v ==> 1, 9, 16

             GND ==> 4, 5, 13, 12

So look at the schematic of the L293D module, then connect its pins to the Arduino UNO as I wrote here. The 5v pin of the Arduino should be connected to the 1, 9, 16 pins to enable the IC's motor driver function.  Then finally use the screwterminals to power up the motors.

Writing codes…

Using the IRremote library I created a code that reads 38kHz infrared signals, decodes them, then moves the robot. (Download the library at the Code section). 

I added explanations in the code, but the substance is that decodes IR signals coming from the main server, then depending on what the user wants, move the motors than will move the robot forward or turns left. Downloload the code: "Robot_Code". Load up this to your Arduino and and your robot will complete.



The Main Server (Arduino Mega, Leonardo, Photon)

The server reads incoming data packets from the wireless headset. We’ll use the configured BT module to ensure the connection between the headset and the server. The Arduino Mega is the brain of the circuit, everything is connected to this microcontroller: Bluetooth, infrared transmitter LED, webserver, and mouse controller. The code is a bit complicated, but I added explanations to understand.

The Case

It’s simple but looks great. I cut two 18x15 cm plates the smoothened the edges with sandpaper. Using some screws, I connected them to each other.

The Relay Circuit

You’ll need these parts:

  • 2n2222 transistor (3 PCS)
  • germanium diodes (3 PCS)
  • 330 ohm resistors (3 PCS)
  • 1k Ohm resistors (3 PCS)
  • 5v relays (3 PCS)
  • hook-up wires
  • header pins
  • PCB board

A picture worth more than a thousand words, so instead of trying to writing how should you connect the parts on the PCB look at the schematic If the “Signal” pin gets signal from the Arduino the relay will turn on. The transistor amplifies the signal to ensure enough power fot the relays. We will use 37-38-39 pins to control HIGH-LOW levels of each relay.

Install Parts in the Case

To install the parts on the plexy-glass case I used some double sided white tape. This loks noce and hold the circuits on the case pretty strong.

The Server-Circuitry

This server is software based so making the circuit isn’t so hard. (See schematic downer). You just need to power up the microcontrollers and make connection between them. The IR LED is connected to pin D3 and  the relays to 37-38-39. D16 of the Arduino Mega goes to RX of the Photon and the D18 to the RX of the Leonardo. 

Connection with the Robot


The IR Leds are connected to the digital pin D3 and with the IRremote library we send codes to the robot. It's pretty simple. The IR codes must be the same in the robot's code. If you think you're done you can test it with your camera. The infrared light looks purple on a photo, cameras can detect the IR light. This trick always works.

The Code

Use the "Arduino_Mega_Server" code in the Software part.

I suffered a lot while wrote the codes. I should program three different microcontrollers, so it was a big challenge. I used IRremote, Brain, SofwareSerial and Mouse libraries. (see download links at the Software part). But now the code is done and works so you just should upload to your microcontrollers. Download the .ino file or copy/paste the code in your IDE and use it. The code to the Particle Photon should be uploaded via the browser IDE. To make this register to the Particle Build. And connect your laptop to your microcontroller. I was really surprised that this happened almost automatically, I just added my Device ID number. 

Before loading up the codes be sure nothing is connected to the RX/TX pins. So disconnect your BLuetooth module from the Mega, and disconnect the Mega from the Leonardo and the Photon.

The Mouse Controller 

The Webserver

I wanted to add an IoT (Internet of Things) function in my project, so I made an online data logger using the Particle Photon. Depending on what you make with the device the Photon creates a personal server, and writes the data in the Cloud.

This may seem scary at first time, but imagine that you can detect if you are stressful (attention level increases and decreases quickly) or if you should sleep (meditation level is always higher than 80). This webserver may help you to live healthlier.

The Particle also has an online dashboard where you can publish any kind of data with the "Particle.publish();" syntax. We should have to say a big "thank you" for the developers of the dashboard. They saved a lot of time for us.

Writing codes in a browser?

It was intresting to write codes on a web page, but worked the same way like the normal IDE. The code was uploaded wirelessly. This microcontroller would be a very useful tool in everyone's toolbox.


The dashboard looks somehow like this if you worked fine. Shows Attention and Meditation, and you can check them anytime, at your dashboard URL. For more info about the dashboards click here.

A quick upgrade for those who want to control only a robot with mindwaves:

You don't want to build the build the whole project, only a mindwave controlled robot? Don't worry if you're a beginner I thought even on you. I made a code with explanations that needs an Arduino Mini Pro and an IR LED and of course the headset to control the robot. I know that many of my readers wants to make a simple and fun weekend project so here's the description and the code for you.

Use the "RobotControlllerHeadset" code inthe Software section. Connect an IR LED to the D3 pin on the Mini Pro (Or Arduino UNO) and connect the T pin of the NeuroSky chip to the RX pin of the Arduino. Power up with 3.3 or 5 volts and you're done. So build the robot, then use this code in your headset: 

#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);

 }

 } 

}

Becoming an IoT Jedi (Testing)

After making everything that I explained here, only one thing is left: try it out. Use the power of your mind, use the Force of the neurons in your brain, move your little robot, control your home, control everything with thoughts...

Now you become a real IoT Jedi!

The device has many features and it's very fun to play with. But as I said before it isn't only a toy, featuring biology and electronics we created a device that may help for people, who fight each day with their disabilities, to get back their natural capabilities.

The robot can be controlled easily, the relays aren't big challange too. You only should learn how to control your attention levels to control the mouse. On the picture I'm trying to exit an app with my brainwaves. More practice guarantees better experience.

Thank you for watching!

I really hope that you liked my project and please write your opinion in comments. I wanted to make a video about the device, but coding was hard so I haven't enough time to make a short movie, but I'll publish it very soon on my Youtube channel and I'll update this project presentation too.

Being the richest man in the cemetry doesn't matter to me... Going to bed at night saying we've done something wonderful... That matters to me - Steve Jobs

May the Force be with you, always!

Schematics

Server
Use this schematic to make the main server.
Dscn0732
Robot Circuit
Schematic for the DIY IR robot
Dscn0738
Relay Module Circuit
Schematic for the DIY 3 channel relay module. Used Arduino pins: D37, D38, D39.
Dscn0737
Schematic for the motor driver. Connect the pins to the Arduino.
F1e76t0hznyguw5.large
The server
Schematic for the main server's electronics.
Dscn0732
Schematic for the Arduino UNO robot
Dscn0730
Robot Circuit
Schematic for the IR robot
Dscn0738
Server Circuit
Use this schematic to make the main server.
Dscn0732
Headset Circuit
Schematic for the headset's electronics
Dscn0746

Code

Particle Photon CodeArduino
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 CodeArduino
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.inoArduino
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 CodeArduino
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_ServerArduino
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.inoArduino
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.

Comments

Similar projects you might like

Flying Drone Mjolnir With Hacked RC Transmitter

Project in progress by Allen Pan

  • 6,800 views
  • 0 comments
  • 28 respects

[App Inventor IoT ]LED fading (PWM)

Project tutorial by CAVEDU Education

  • 269 views
  • 0 comments
  • 5 respects

Remote System Monitor

Project tutorial by Stephen Harrison

  • 2,565 views
  • 0 comments
  • 5 respects

Integrate Node-Red with RAK811 LoRA node using the TTN

Project tutorial by Naresh krish

  • 200 views
  • 0 comments
  • 3 respects

Soil Control Grid: From The Ground To The Cloud

Project tutorial by Andrea De Gaetano

  • 130 views
  • 0 comments
  • 2 respects

Fox Advisor

Project tutorial by Celia Garrido Hidalgo

  • 1,108 views
  • 0 comments
  • 4 respects
Add projectSign up / Login