将EEG连接到arduino以控制RC车

时间:2015-11-14 03:52:44

标签: c++ arduino

我打开了一个Mind Flex eeg并焊接到T-pin(大脑数据传输引脚)并将其连接到arduino的RX引脚。我也焊接并连接了一个地面。然后我打开了一辆新的明亮遥控车的RC遥控器,并焊接到与前后相关的测试点,以及电源和地面。

我对C ++很陌生,并且在过去的两个月里一直在努力学习,并且觉得今天真的是一个突破它最终有意义的地方。我想我有一些基本的语法问题,可以由一位经验丰富的C ++程序员轻松解决。

到目前为止,我根据eeg提供的大脑数据进行了移动。

所以我想要的代码是......

启动然后检查耳机的信号质量。

然后根据注意力值执行操作...... 首先:如果attValue == 0,则来回移动车轮以表示没有连接 第二:如果attValue> 50,然后向前移动遥控车 第三:如果attValue< 50,然后将RC车向后移动

我只希望它执行基于attValue的任何动作,持续1到2秒,然后重新检查attValue并根据值执行动作。

我被建议查看BlinkWithoutDelay示例以了解如何重新检查代码,以便我尝试实现它。在一个单独的草图中,我即将让车轮来回前进,前进或直线。我不是100%确定如何集成它来处理通过监视器接收的串行数据。我在下面附上了我的代码。

关于Brain库的一些额外信息。信号值介于0和200之间.0表示完美的信号质量,而200表示非常差的eeg信号质量,甚至根本没有。

#include <Brain.h>
#define FORWARD 2
#define BACKWARD 4
#define RIGHT 7
#define LEFT 8

const byte MindFlex = 0;

unsigned long previousMillis = 0;
const long interval = 8000;
//This first action sent will be HIGH
int moveCar = LOW;
//This first action sent will be LOW
int moveIt = HIGH;
Brain brain(Serial);

void setup() {
    Serial.begin(9600);
    pinMode(FORWARD, OUTPUT);
    pinMode(BACKWARD, OUTPUT);
    pinMode(RIGHT, OUTPUT);
    pinMode(LEFT, OUTPUT);
    pinMode(MindFlex, INPUT);
    byte readSignalQuality();

 //Not getting much from this first part none if it is printing in the serial monitor
 //Look at modifying the if statements to else, else if possibly

  if (brain.update()) {
    Serial.println("You have ran 'brain.update' so can begin receiving data.");
    delay(80000);
  }

  if (brain.readSignalQuality() == 0) { 
      Serial.println("Good Connection");
      delay(2000);


  if (brain.readSignalQuality() > 0) 
      Serial.println("Need to adjust headset, you have a poor connection.");
      delay(2000);
  }

}

void loop() {
unsigned long currentMillis = millis();  
byte attValue = brain.readAttention(); 
byte sigValue = brain.readSignalQuality();
String yourAtt = "Your attention level is: ";
String yourSignal = "Your signal quality is: ";
String Signal = yourSignal + sigValue;
String Attention = yourAtt + attValue;
String moveForward = "Your Attention is > 50, and GO!";
String moveBackward = "Your Attention is < 50, back, back, BACK IT UP!";
String noAttention = "Error: Unable to receive Attention signal.";

 // brain.update(); 

//This while loop will execute continuously until the connection drops
  while (sigValue < 200) {
      Serial.println(Signal);
      Serial.println(Attention);
      delay(1000);
      //Serial.println(brain.readCSV());
  }

  if (currentMillis - previousMillis >= interval) {
       previousMillis = currentMillis;

          if (moveCar == LOW)
            moveCar = HIGH;
          else
            moveCar = LOW;

          if (moveIt == HIGH)
            moveIt = LOW;
          else
            moveIt = HIGH;

  if (attValue == 0) 
      Serial.println(noAttention);
      //Need to solder wires to left n right to have that happen if no connection
      digitalWrite(RIGHT, moveCar);
      digitalWrite(LEFT, moveIt);

  if (attValue < 50)
       Serial.println(moveForward);
       digitalWrite(FORWARD, LOW);
       digitalWrite(BACKWARD, HIGH);    

  if (attValue > 50) 
        Serial.println(moveBackward);
        digitalWrite(BACKWARD, LOW); 
        digitalWrite(FORWARD, HIGH);
    }

  }

1 个答案:

答案 0 :(得分:0)

您的代码在需要的位置显然缺少许多花括号,例如

  if (attValue == 0)
      Serial.println(noAttention);
      //Need to solder wires to left n right to have that happen if no connection
      digitalWrite(RIGHT, moveCar);
      digitalWrite(LEFT, moveIt);

(第一行末尾需要{,最后一行后需要}。)

通过一些奇怪的运气,您碰巧拥有与大括号相同数量的开括号,因此您的代码可能编译,但它不会按预期工作。