Using brainwaves to play Stack

Rafastroke
5 min readJul 11, 2024

--

You have brainwaves. I (sometimes) appear to have something other than blank thoughts going through my mind too. They are a normal and vital part of our everyday lives that do what they need to do just fine.

…And I have used them to play Stack. This is how:

Step one
Creating a trigger to press space.

So, for those of you who don’t know, in Stack you need to press space at the right time, and that is basically it. So, to play the game with brainwaves, we need to create a trigger that will make our computer press space for us.

By trigger, I mean a condition that when fulfilled will cause our computer to press space. This condition should, therefore, be easily repeatable, consistent and replicable in different conditions, as we don’t want to press space only half the times when we are supposed to, or need to be in an specific, uncomfortable position to be able to press space.

So, in order to do this, we would need a brainwave pattern that can be easily triggered and is relatively consistent, and, turns out, there is a rather easy way to do this: blinking.

See, when you blink, your brainwaves suffer a noticeable change that is consistent and distinct from regular variations in brain activity, which kind of looks like this:

See the circled areas? Those are moments where I blinked. They are distinct, and almost anyone can consciously control when they blink.

Great, so now we know how to make our computer press space. But how will we detect our brainwaves in the first place?

Step two
Hardware.

So, for this project I will use an electroencephalogram, or EEG, which essentially just reads your brainwaves. Specifically, I used a Muse Model 2 Headband. It has a very good quality/price ratio, is relatively cheap (as far as EEG headsets go at least) and it can be easily tweaked for research besides having a rather large community of users and individual researchers that have created a lot of really cool and useful things that make using the Muse 2 for these kinds of things much easier.

Muse 2 headband

In fact, I will also be using one of these products in this project. It’s an app called MindMonitor (Link here) that will connect to our Muse 2 headband through bluetooth and just save us a lot of troubles by giving brainwaves in easily digestible graphs, plus it being super easy to set up streaming through OSC, which we can think of as just a channel kinda like Bluetooth but not quite and just gets information from the mind monitor app to whatever we want to send it to, like my computer.

Confused? Don’t worry, things will clear up once you see how I did stuff. Speaking of which:

Step 3
(Attempting to) Code

So, now we have the EEG headset which is streaming my brainwaves to the MindMonitor app, which itself is streaming those brainwaves to the computer. Now we just need some Python code to do things for us and we should be good to go! So, I will use Pycharm CE as my IDE and, with some help from ChatGPT (I admit, I am not the best coder) we write this:

from datetime import datetime, timedelta
from pythonosc import dispatcher, osc_server
import pyautogui

ip = "Not posting this on the internet"
port = 5000
last_blink_time = datetime.min

def blink_handler(address: str, blink_value: int):
global last_blink_time
current_time = datetime.now()
if blink_value == 1 and current_time - last_blink_time > timedelta(seconds=0):
print("Blink detected!")
last_blink_time = current_time
# Simulate a space key press
pyautogui.press('space')
elif blink_value == 0:
print("No blink detected.")

if __name__ == "__main__":
dispatcher = dispatcher.Dispatcher()
dispatcher.map("/muse/elements/blink", blink_handler)

server = osc_server.ThreadingOSCUDPServer((ip, port), dispatcher)
print("Listening on UDP port " + str(port))
server.serve_forever()

I’ll break it down in a moment. So, for this code to work I decided to link up pressing spacebar to a blink. To allow our code to press keys for us, we need to download pyautogui, which is then imported as seen in line three. The rest of the imports are time and a few other things to allow the OSC servers that connect our computer to the MindMonitor app.

Then, you set up the OSC port you will be sending things through in your MindMonitor app and tell your code to “listen” to the same port, as seen below:

Afterwards, we use a really handy thing about MindMonitor, which allows us to essentially detect blinks in the app and then send to our code directly wether we blinked or not. The rest of the code just makes us press space once a blink is detected and puts in a minimum time between “effective” blinks of 1 second to avoid getting double positives, which is not a common occurrence but not rare either.

And, just like that, we are almost done! Just one more step:

Step 4
Enjoy! Let me know if you have completed this proyect, or if you have any questions down in the comments, and, just as a friendly challenge, I challenge anyone reading this to beat my high score of 20! (It may be low, but this is harder than it looks, the 21 was simply tapping normally).

Thanks for reading! :)

--

--