Skip to content

Instantly share code, notes, and snippets.

@kumekay
Created June 27, 2015 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kumekay/ac095145d69e9b9ab331 to your computer and use it in GitHub Desktop.
Save kumekay/ac095145d69e9b9ab331 to your computer and use it in GitHub Desktop.
MindWave
////////////////////////////////////////////////////////////////////////
// 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
#define ATT 5
#define LEFT 1.7
#define RIGHT 3.3
#define powercontrol 10
#include "ADXL335.h"
ADXL335 accelerometer;
float ax,ay,az;
// 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 fw[] = {8,7,6,5,4,3,2};
int left = 10;
int right = 11;
int spd = 0;
// system variables
long lastReceivedPacket = 0;
boolean bigPacket = false;
//////////////////////////
// Microprocessor Setup //
//////////////////////////
void setup() {
accelerometer.begin();
pinMode(LED, OUTPUT);
Serial.begin(BAUDRATE); // USB
pinMode(left, OUTPUT);
digitalWrite(left, HIGH);
pinMode(right, OUTPUT);
digitalWrite(right, HIGH);
MotorOff();
}
void MotorOff() {
for (int i=0; i <= 6; i++){
pinMode(fw[i], OUTPUT);
digitalWrite(fw[i], HIGH);
}
digitalWrite(left, HIGH);
digitalWrite(right, HIGH);
}
////////////////////////////////
// Read data from Serial UART //
////////////////////////////////
byte ReadOneByte() {
int ByteRead;
while(!Serial.available());
ByteRead = Serial.read();
#if DEBUGOUTPUT
Serial.print((char)ByteRead); // echo the same byte out the USB serial (for debug purposes)
#endif
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;
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(" Time since last packet: ");
Serial.print(millis() - lastReceivedPacket, DEC);
lastReceivedPacket = millis();
spd = map(attention, ATT, 100, 0, 6);
MotorOff();
digitalWrite(fw[spd], LOW);
Serial.print(" Speed: ");
Serial.print(spd);
accelerometer.getAcceleration(&ax,&ay,&az);
Serial.print(" Acc: ");
Serial.print(ax);
if (ax < LEFT ) {
digitalWrite(left, LOW);
Serial.print(" LEFT ");
}
if (ax > RIGHT ) {
digitalWrite(right, LOW);
Serial.print(" RIGHT ");
}
Serial.print("\n");
}
#endif
bigPacket = false;
}
else {
// Checksum Error
} // end if else for checksum
} // end if read 0xAA byte
} // end if read 0xAA byte
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment