65.9K
CodeProject 正在变化。 阅读更多。
Home

ThinkUino 项目

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.83/5 (9投票s)

2013年3月26日

CPOL

3分钟阅读

viewsIcon

45555

downloadIcon

1082

用我们的大脑与 Arduino 板进行交互。

引言

ThinkUino (Thinking Arduino) 是一个开源项目,可以将 Arduino 开发板与 Mindwave 头戴式耳机连接起来。 通过读取脑电波,认知应用为控制电子电路开辟了新的领域。 本文我将介绍如何制作一个认知应用程序来控制一个 LED 灯。

背景

Mindwave... 它是什么?

Mindwave 是 Neurosky 生产的一款创新型头戴式耳机。 Mindwave 将脑电波转换为数字电子信号。 该设备有两个版本

Mindwave... 两个版本,但我应该选择哪个?

对于这个项目,Mindwave Mobile 是必不可少的。 该设备支持蓝牙连接,通过蓝牙连接以 RAW 模式将数据发送到连接的设备。 通过检索到的数据,我们可以解析耳机读取的各种频率。

我需要什么来实现这个项目?

创建该项目的基本要素是

  1. Mindwave Mobile 头戴式耳机
  2. Arduino 2009 或更高版本
  3. LED

使用代码

按照以下步骤创建此项目的架构。

配置 Arduino 开发板

将 LED 灯连接到电路板上。 将阳极插入第十三针(称为 Led 针),阴极插入 GND(接地)针。 如果您有疑问,请参见下图

之后,将 Arduino 连接到 USB 端口并上传此代码

int val;  
void setup() 
{ 
  Serial.begin(9600);
  pinMode(13,OUTPUT);
} 
 
void loop() 
{ 
  if(Serial.available()>0){
    val = Serial.read();
    if(val==1){
     digitalWrite(13,HIGH);
    }else{
      digitalWrite(13,LOW);
    }  
}

如您所见,代码非常简单。 板读取一个字节来设置 LED,然后

  • 字节设置为 0:将引脚 led 设置为 LOW
  • 字节设置为 1:将引脚 led 设置为 HIGH

配置 Mindwave 头戴式耳机

首先,我们必须将蓝牙耳机与计算机配对。 按照此步骤配对设备

  • 将耳机设置为可发现模式。 按住按钮三秒钟。 参见下图

  • 打开控制面板上的蓝牙选项,搜索所有可发现的设备,如果耳机(名为 MindWave Mobile)在列表中,请添加它。

现在...让我们分析一下代码!

您可以从此文章下载源代码,请参见附件...

该项目使用 ThinkGear.net 库。 该库从耳机检索 RAW 数据并进行解析,然后我们可以轻松访问所有频率和状态。 该耳机使用 eSense 协议。 eSense 精心设计所有频率并返回两种类型的状态,注意力和冥想。 我们使用 Attention 变量来改变 LED 的状态。

请参阅代码中的注释以理解它

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThinkGearNET;
using System.Threading;
using System.IO;
 
namespace ThinkUino
{
    public partial class Form1 : Form
    {
        
        private ThinkGearWrapper thinkGearWrapper = new ThinkGearWrapper(); //declare the thinkgear object
        private bool comando = false;
 
        private const string ARDUPORT = "COM5"; //declare the arduino serial port
        private const string MINDPORT = "COM7"; //declare the Mindwave serial port

        public Form1()
        {
           InitializeComponent();
        }
 
        private void btnHeadsetConnect_Click(object sender, EventArgs e)
        {
            if (thinkGearWrapper.Connect(MINDPORT, 57600, true)) //check if the Mindwave is connected
            {
                btnHeadsetConnect.Enabled = false; //disable connect button
                btnHeadsetDisconnect.Enabled = true; //enable disconnect button
                txtHeadsetDebug.AppendText("Headset connected." + Environment.NewLine);
                thinkGearWrapper.EnableBlinkDetection(true); //enable the eye blink on the eSense protocol
                thinkGearWrapper.ThinkGearChanged += new EventHandler<ThinkGearChangedEventArgs>(
                  thinkGearWrapper_ThinkGearChanged); //capture the event when a new data is incoming
            }
            else
                txtHeadsetDebug.AppendText("Could not connect to headset" + Environment.NewLine);
        }
 
        void thinkGearWrapper_ThinkGearChanged(object sender, ThinkGearChangedEventArgs e)
        {
            
            BeginInvoke(new MethodInvoker(delegate //use the AsyncTask to update the UI
                {
                    if ((thinkGearWrapper.ThinkGearState.BlinkStrength > 60)  ) // check if the eye blink strength is above 60
                    {
                        comando = !comando; //switch the led's state
                        txtHeadsetDebug.AppendText("Command: " + Convert.ToByte(comando) + Environment.NewLine); 
                        thinkGearWrapper.ThinkGearState.BlinkStrength = 0;//set the eye blink strength to zero
                    }
                    if (thinkGearWrapper.ThinkGearState.Attention > 60)//check if the Attention state is above 60
                    {
                        serialArduino.Write(new byte[] { Convert.ToByte(comando) }, 0, 1); //send the commando to arduino serial port
                        txtHeadsetDebug.AppendText("Command sendend: " + Convert.ToByte(comando) + Environment.NewLine);
                        txtArduinoDebug.AppendText("Command received: " +Convert.ToByte(comando));
                        thinkGearWrapper.ThinkGearState.Attention = 0; //set the attention state to zero
                    }
                    lblAttention.Text = "Attention: " + thinkGearWrapper.ThinkGearState.Attention;
                    lblMeditation.Text = "Meditation: " + thinkGearWrapper.ThinkGearState.Meditation;
                }));
            Thread.Sleep(10); //wait ten milliseconds
        }
 
        private void btnHeadsetDisconnect_Click(object sender, EventArgs e)
        {
            
            thinkGearWrapper.Disconnect(); //close the headset connection
            txtHeadsetDebug.AppendText("Disconnected." + Environment.NewLine);
            btnHeadsetDisconnect.Enabled = false; //disable the headset disconnect button
            btnHeadsetConnect.Enabled = true; //enable the headset connect button
        }
 
        private void bntArduinoConnect_Click(object sender, EventArgs e)
        {
            try
            {
                serialArduino.PortName = ARDUPORT; //set the arduino serial port name
                serialArduino.Open(); //open the communication
                txtArduinoDebug.AppendText("Arduino connected" + Environment.NewLine);
                btnArduinoDisconnect.Enabled = true; //enable the arduino disconnect button
                btnArduinoConnect.Enabled = false; //disable the arduino connect button
            }
            catch (IOException)
            {
                txtArduinoDebug.AppendText("Could not connect to Arduino" + Environment.NewLine);
            }
               
        }
 
        private void btnArduinoDisconnect_Click(object sender, EventArgs e)
        {
            if (serialArduino.IsOpen) //check if the communication is open
            {
                serialArduino.Close(); //close the arduino communication
                txtArduinoDebug.AppendText("Disconnected." + Environment.NewLine);
                btnArduinoConnect.Enabled = true; //enable the arduino connect button
                btnArduinoDisconnect.Enabled = false; //disable the arduino disconnect button
            }
        }
 
    }
} 

该应用程序只有一个窗口

为了正确使用该应用程序,首先连接设备,Mindwave 和 Arduino,然后单击窗口上的两个按钮。 眨眨你的眼睛来改变 LED 的状态,并从你的脑海中抹去所有的想法,除了 LED!! Smile | <img src=

再见!

© . All rights reserved.