Before I start this article, there's something I have to admit. I'm a junky.
Like many geeks who came of age in the early 90s, I got roped into doing cyberpunk. It started out as an occasional thing, maybe a Gibson novel every now and then, but before I knew it I was in deep with no hope of surfacing. Shadowrun, Deus Ex, Neil Effing Stephenson... I did it all, man. I even watched Mick Jagger scowl his way through Freejack a couple of times.
With my imagination held captive by this constant techno-high, I started cultivating a perhaps unhealthy obsession with cybernetics. You know, human augmentation, driving machines with your mind, implanting nanites in your body, piping the internet directly into your brain... basically, all the stuff your mother told you would bring about the downfall of humanity.
So of course when I heard that there is now a commodity-level, non-invasive brain-computer interface, well... you know where I'm going with this.
The NeuroSky Mind[Wave|Set]
First, let's clear up a couple of misconceptions. When many people hear the words "brain-computer interface," those that don't start grabbing their guns and preparing for the inevitable robot invasion will often say "wow, cool, so the computer can read my thoughts?"
No. That's wrong. You're thinking of a psychic.
So what are some of the differences between a head-mounted EEG device and a self-proclaimed psychic? Let's review:
|Reads several brainwave frequencies corresponding to very general neural activity||Reads your face, tea leaves, stars, horoscopes, and pretty much everything except your brainwaves|
|Sits comfortably on your head with a sensor against the forehead and ground contacts on your ear||Not very comfortable when sitting on your head|
|Feeds you useful information about your mental state, including a reliability measure||If these guys gave you a reliability measure, they'd be out of business|
|Includes a convenient API for programmability and hacking||Not a platform conducive to opening up and hacking (warning: trying to do so may lead to criminal charges)|
The NeuroSky equipment in particular can read most of the things that an amateur psychologist would be interested in: alpha waves (high and low), beta waves (high and low), delta waves, gamma waves (high and low), and theta waves, as well as a couple of derived measures: eye blinks, attention, and meditation. These last two are aggregate values calculated on the chip in the device.
For our purposes we're really only interested in the attention value, which corresponds very roughly to how much you concentrate. Interestingly, it's not exclusive of the meditiation value, so you can have both a high attention and a high meditation value. This is what some of the NeuroSky people described to me as "being in the zone."
Notice that there's only one forehead-mounted sensor, however, which does extremely poorly at differential analysis and reading signals from some of the more interesting parts of the head like the motor cortex. No, this can't understand what you're thinking; it can only measure activity in one part of your brain. You could be imagining strawberries, giant robots, or a dancing tyrannosaurus for all that it can tell. The intensity of the brain activity is what we'll use in our setup today.
One more note: I used the more expensive MindSet for this project because it comes with Bluetooth built-in. The MindWave is about half the price, but it uses proprietary RF communications rather than Bluetooth, which makes it fine for desktop computers but a little painful to use in hardware projects. If you feel like saving three Grants, though, there are instructions located all over the internet for hacking the MindWave into Arduino projects.
Hardware setup is pretty easy for this--it's nothing compared to what you've just done for the Nerf gun. Grab your breadboard, plug in your Bluetooth module (assuming you've soldered some headers onto the six-pin interface), and connect some wires according to the following diagram:
Hook the Arduino up to your computer of choice, download the associated program, and open it up in the Arduino IDE. If this is the first time you've programmed your Bluetooth module, you'll have to set the line
to read 115200 instead of 57600. The factory settings on the BlueSMiRF module are set to 115,200 bps by default, but we'll be much happier running at 57,600bps. Therefore we have to tell the Arduino that initial communication with the BlueSMiRF will be at the higher speed; after the Arduino has programmed into the BlueSMiRF once, all future communications will take place at the new rate (even if you turn it off). If you ever use the SF,1 command to restore factory settings, however, the BlueSMiRF will be set back to 115,200bps.
You'll also want to change the following line so it contains your MindSet's MAC address instead of a random one:
Finally, set the program bit to 1:
This will program the Bluetooth module so that every time it powers up, it will automatically go into master mode and start scanning for the MAC address you gave it. If your MindSet is turned on, the BlueSMiRF should find it in a matter of seconds and connect. You should only need to do this once.
So load your program onto the Arduino, and if all has gone well then you'll see the status light on the BlueSMiRF blink rapidly a few times. It will continue to flash rapidly until it either drops out of command mode ("---" if you have terminal access) or until it gets turned off and back on.
Now remember that PROGRAM parameter you changed earlier? We'll have to change that back to load the actual software on-board:
This will set the program to begin collecting data through the BlueSMiRF and analyzing the attention value. If the attention value exceeds the FIRE_THRESHHOLD value (on a scale of 1-100) then the Arduino will send a fire command to the gun.
There are other things the included software can do (explained on the software page), as well as a variety of easy hacks you can do to make it more useful or interesting, but I'll leave that as an exercise to you, the reader.
Otherwise, that's it! You can now terrorize Mick Jagger with your very own hacked-together smartgun.
While I can't actually offer much in the way of troubleshooting help, allow me to describe the tortuous and torturous process of getting this to work. Almost everything was an issue with the BlueSMiRF module, so if you're banging your head against something I suggest you begin investigating there.
I started out with the sample programs included in the Arduino developers' section of the NeuroSky developer site. The first one to program the BlueSMiRF didn't seem to work at first, no matter whether I set it to 57,600bps or 115,200bps. It actually took me a couple of weeks to work through this problem, not least because I at one point accidentally reversed the Vcc and ground wires; half a second and a puff of smoke later, I was out one BlueSMiRF chip and $40. Lesson learned: never do that.
At some point in all this foolishness I tried using an Arduino library called NewSoftSerial which basically emulates a second serial port on any two arbitrary pins. This was extremely useful for writing a pass-through debugger to chat with the Bluetooth module: with the BlueSMiRF sitting on NewSoftSerial ports, the hardware serial ports were free to chat with the serial console in the Arduino environment. With this I could drop into command mode when the module first powered on, send commands and queries through the Arduino to the Bluetooth module, and get status responses relayed back. Handy!
What wasn't so handy, however, was interfacing with the BlueSMiRF. Even though NewSoftSerial did what I wanted it to, I was still having difficulty getting into command mode. It turns out I was having line termination problems, although not the sort that one normally thinks of when the issue of line termination comes up. In this case, I had been appending a carriage return (\r) to everything that got sent from the serial console in the Arduino IDE, since it strips out line endings on sending data. While the line terminator is necessary for commands, it turns that to get into command mode in the first place you need to send a series of three dollar signs (by default) without a line terminator--everything else requires a line terminator, however. If you do add a line terminator to the end, the BlueSMiRF basically throws up its metaphorical hands and yells "I don't understand your crazy moon language!" The solution: add a flag to the program and make terminal mode a dedicated state in the program.
Roundabout midnight on a Saturday I finally uncovered one particular post on the Sparkfun forums which set me straight. At this point the NeuroSky documentation had me (rather cleverly) connecting Vcc on the BlueSMiRF to a digital pin on the Arduino so that it could be powered on and off programatically (Note: this is also what the Make magazine article in volume 26 has you doing). However, that didn't seem to be making any contact with the MindSet--the status LED on the Bluetooth module just kept blinking as if it was scanning and not finding anything. The I/O pins run at 5V, though, so I connected Vcc to the 3.3V source on the Arduino and voilà! It connected almost instantly. Lesson learned: read the specs.
However, I now had a new problem facing me. Everything was coming out gibberish! Almost everything, anyway. I was getting checksum errors and over-long packets right and left, and almost nothing was registering. I could get some small values for meditation, but almost nothing for attention. It turns out that I still had the BlueSMiRF on a NewSoftSerial port which, while great for debugging, still runs a little bit slowly for the extreme amount of data that the MindSet is sending. I plugged it into the hardware serial port and started getting clean values almost immediately. Lesson learned: your brain puts out a serious amount of data!
Hopefully this will aid some of you in avoiding similar issues. The last piece of advice that I can give is that NeuroSky support is quite nice and they're very good about supporting developers and hackers--it's the main reason I chose their product over any of the other three available at the time.
Now go shoot some mind bullets!