Arduino连接TGAM(蓝牙脑波模块)之代码篇

Arduino连接TGAM(蓝牙脑波模块)

因为从淘宝上购买以后,很多网店都没有与Arduino连接的代码,经历千辛万苦终于找到了官网的一段与Arduino通讯的代码。下面把资料以及连接附在这里,供大家学习。
这里附上官网链接:http://developer.neurosky.com/docs/doku.php?id=mindwave_mobile_and_arduino

百度网盘链接:https://pan.baidu.com/s/18fkeSE6d1n24q9dkD6HQAQ
提取码:ijdo
Arduino连接TGAM(蓝牙脑波模块)之代码篇_第1张图片

Arduino代码说明

TGAM套件自带一个蓝牙,我们需要在Arduino板上再接一个蓝牙。两个蓝牙之间相互通讯。即设置两个蓝牙,一个为主机,一个为从机。

这里需要注意:主机和从机之间可以互发数据。最主要的区别在于,蓝牙主机是主动搜索,而蓝牙从机只能被动连接。

Arduino连接TGAM(蓝牙脑波模块)之代码篇_第2张图片

连接图

Arduino连接TGAM(蓝牙脑波模块)之代码篇_第3张图片
Arduino连接TGAM(蓝牙脑波模块)之代码篇_第4张图片
Arudino代码

////////////////////////////////////////////////////////////////////////
// Arduino Bluetooth Interface with Mindwave
// 
// This is example code provided by NeuroSky, Inc. and is provided
// license free.
////////////////////////////////////////////////////////////////////////

#define LED 13
#define BAUDRATE 57600
#define DEBUGOUTPUT 0
// checksum variables
byte generatedChecksum = 0;
byte checksum = 0; 
int payloadLength = 0;
byte payloadData[64] = {0};
byte poorQuality = 0;
byte attention = 0;
byte meditation = 0;
int raw=0;
// system variables
long lastReceivedPacket = 0;
boolean bigPacket = false;

//////////////////////////
// Microprocessor Setup //
//////////////////////////
void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(BAUDRATE);           // USB

}

////////////////////////////////
// Read data from Serial UART //
////////////////////////////////
byte ReadOneByte() {
  int ByteRead;
  while(!Serial.available());
    digitalWrite(13,HIGH);
  ByteRead = Serial.read();
  return ByteRead;
}

/////////////
//MAIN LOOP//
/////////////
void loop() {


  // Look for sync bytes
  if(ReadOneByte() == 170) {
    if(ReadOneByte() == 170) {

      payloadLength = ReadOneByte();
      if(payloadLength > 169)                      //Payload length can not be greater than 169
          return;

      generatedChecksum = 0;        
      for(int i = 0; i < payloadLength; i++) {  
        payloadData[i] = ReadOneByte();            //Read payload into memory
        generatedChecksum += payloadData[i];
      }   

      checksum = ReadOneByte();                      //Read checksum byte from stream      
      generatedChecksum = 255 - generatedChecksum;   //Take one's compliment of generated checksum

        if(checksum == generatedChecksum) {    

        poorQuality = 200;
        attention = 0;
        meditation = 0;

        for(int i = 0; i < payloadLength; i++) {    // Parse the payload
          switch (payloadData[i]) {
          case 2:
            i++;            
            poorQuality = payloadData[i];
            bigPacket = true;            
            break;
          case 4:
            i++;
            attention = payloadData[i];                        
            break;
          case 5:
            i++;
            meditation = payloadData[i];
            break;
          case 0x80:
            i = i + 3;
           raw=payloadData[i-1]*256+payloadData[i];
           if(raw>=32768)
           raw=raw-65536;
            break;
          case 0x83:
            i = i + 25;      
            break;
          default:
            break;
          } // switch
        } // for loop

#if !DEBUGOUTPUT

        // *** Add your code here ***

        if(bigPacket) {
          if(poorQuality == 0)
            digitalWrite(LED, HIGH);
          else
            digitalWrite(LED, LOW);
          Serial.print("PoorQuality: ");
          Serial.print(poorQuality, DEC);
          Serial.print(" Attention: ");
          Serial.print(attention, DEC);
          Serial.print(" raw ");
          Serial.print(raw);
          Serial.print(" Time since last packet: ");
          Serial.print(millis() - lastReceivedPacket, DEC);
          lastReceivedPacket = millis();
          Serial.print(" Att: ");
          int att=Serial.print(attention,DEC);
          Serial.print("\n");

          switch(attention / 10) {
          case 0:
      digitalWrite(LED, HIGH);        
            break;
          case 1:
        digitalWrite(LED, HIGH);
            break;
          case 2:
           digitalWrite(LED, HIGH);
            break;
          case 3:              
            digitalWrite(LED, HIGH);       
            break;
          case 4:
         digitalWrite(LED, HIGH);           
            break;
          case 5:
     digitalWrite(LED, HIGH);           
            break;
          case 6:              
        digitalWrite(LED, HIGH);       
            break;
          case 7:
        digitalWrite(LED, HIGH);        
            break;    
          case 8:
     digitalWrite(LED, HIGH);
            break;
          case 9:
            digitalWrite(LED, HIGH);
            break;
          case 10:
            
            digitalWrite(LED, HIGH);
            break;           
          }  
          Serial.flush();                   
        }
#endif        
        bigPacket = false;        
      }
      else {
        
           }  
           Serial.flush();
    } 
  } 
}


这里从Arduino串口读取到专注度。从而可以还控制LED灯的开关,还有小车的运动。

关于蓝牙主从机的连接我会稍后发。

你可能感兴趣的:(科技)