Skip to content

Instantly share code, notes, and snippets.

@Vaughn4
Last active May 7, 2018 04:31
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 Vaughn4/d851b6ed2a663e3af4cb1acadfd3dece to your computer and use it in GitHub Desktop.
Save Vaughn4/d851b6ed2a663e3af4cb1acadfd3dece to your computer and use it in GitHub Desktop.
This is the 2 tab code required for the Sleep Tracking Brain Wave Headband created by Vaughn Chambers & Kaleb Burch
/*
Sleep Tracking Brain Wave Headband Code
This code collects EEG brain wave data using 3 electrodes placed on the your forehead. It takes a set number of
samples using a set sampling frequency and then does a Fourier transform of the data. Next, the most abundant frequency
(peak) of the data is found and used for an output. Data can be transmitted to a computer via USB cable or to your
phone via bluetooth using the BlueFruit Application. Also, depending on your last peak brain wave, a NeoPixel LED light
will change colors to replicate your current brain activity. Data can be logged over a long period of time, for example,
a night of sleep, and then analyzed using Excel.
The Circuit:
- Inputs
- Battery Power connected to Adafruit Flora
- EEG Signal using Bitalino EEG extension
- Outputs
- Brain Wave Peak Data
- Time Logger
- NeoPixel LED
Created Sunday May 6th, 2018
By: Vaughn Chambers and Kaleb Burch
*/
//FOURIER TRANSFORM
#include "arduinoFFT.h" //This is the Fast Fourier Transform that converts the data into the frequency domain
#define SAMPLES 128 //Number of sample collected for each peak calculation (must be a power of 2, mas is 128)
#define SAMPLING_FREQUENCY 300 //Hz, must be less than 10000 due to ADC, 300 seems to be optimal for EEG
arduinoFFT FFT = arduinoFFT();
double vReal[SAMPLES]; // used for cosine waves
double vImag[SAMPLES]; // used for sine waves
//BLUETOOTH
#include <avr/power.h>
#include <Arduino.h>
#include "Adafruit_BLE.h" //extension for BlueFruit Component
#include "Adafruit_BluefruitLE_UART.h" // for BlueFruit Communication
#include "BluefruitConfig.h" // See other tab
Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN); // See other tab
//NEOPIXEL
#include <Adafruit_NeoPixel.h> //extension for offboard Neopixel
#define PIN 9 //Pin that the Neopixel is attached to on the Flora
#define NUMPIXELS 1 //Number of Neopiexels used
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); //
//TIME
unsigned int sampling_period_us; //Defining time logging that will start when Flora is powered
unsigned long microseconds; //Defining time logging that will start when Flora is powered
void setup() {
//When using Computer to record data (uncomment if using computer)
//Serial.begin(115200);
//When using Bluetooth to record data (uncomment if using BlueFruit)
ble.begin(115200);
//Start up neopixel
pixels.begin();
sampling_period_us = round(1000000*(1.0/SAMPLING_FREQUENCY)); //How often Neopixel can change
}
void loop() {
/*SAMPLING*/
for(int i=0; i<SAMPLES; i++)
{
microseconds = micros(); //Defining time logging
vReal[i] = analogRead(A3); //Pin on Flora that EEG input is attached to
vImag[i] = 0; //0 indicates that it is not needed, vReal[i] can capture everything
while(micros() < (microseconds + sampling_period_us)){
}
}
/*FAST FOURIER TRANSFORM*/
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD); //Fast Fourier Transform taking in data
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD); //Fast Fourier Transform puting data into frequency domain
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES); //Fast Fourier Transform finding magnitudes in frequency domain
double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY); //Peak of last sample group is found
/*OUTLIER REMOVAL*/
if(peak>1 && peak<45){ //if peak of last data is less than 1 or above 45, skip this section and do not print
/*PRINT RESULTS TO COMPUTER*/
//Serial.print(microseconds/1000000); //Print time in seconds
//Serial.print(","); //Separate with a comma
//Serial.println(peak); //Print out what frequency is the most dominant
/*PRINT RESULTS TO BLUETOOTH*/
ble.print(microseconds/1000000); //Print time in seconds
ble.print(","); //Separate with a comma
ble.println(peak); //Print out what frequency is the most dominant
/*NEOPIXEL BRAIN WAVE INDICATOR*/
for(int i=0;i<NUMPIXELS;i++){
//for theta waves (1 Hz to 4 Hz), have NeoPixel be purple
if(peak>1 && peak<4){
pixels.setPixelColor(i, pixels.Color(19,0,39)); // Dim purple color.
pixels.show(); // This sends the updated pixel color to the hardware.
}
//for delta waves (4 Hz to 8 Hz), have NeoPixel be blue
if(peak>4 && peak<8){
pixels.setPixelColor(i, pixels.Color(0,0,39)); // Dim blue color.
pixels.show(); // This sends the updated pixel color to the hardware.
}
//for alpha waves (8 Hz to 14 Hz), have NeoPixel be green
if(peak>8 && peak<14){
pixels.setPixelColor(i, pixels.Color(0,39,0)); // Dim green color.
pixels.show(); // This sends the updated pixel color to the hardware.
}
//for beta and gamma waves (> 14 Hz), have NeoPixel be yellow
if(peak>14){
pixels.setPixelColor(i, pixels.Color(39,39,0)); // Dim yellow color.
pixels.show(); // This sends the updated pixel color to the hardware.
}
}
delay(250); //Repeat the process every quarter of a second
}
}
// COMMON SETTINGS
// ----------------------------------------------------------------------------------------------
// These settings are used in both SW UART, HW UART and SPI mode
// ----------------------------------------------------------------------------------------------
#define BUFSIZE 160 // Size of the read buffer for incoming data
#define VERBOSE_MODE true // If set to 'true' enables debug output
// SOFTWARE UART SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins that will be used for 'SW' serial.
// You should use this option if you are connecting the UART Friend to an UNO
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_SWUART_RXD_PIN 1 // Pin on Flora that attaches to RX of BlueFruit
#define BLUEFRUIT_SWUART_TXD_PIN 0 // Pin on Flora that attaches to TX of BlueFruit
#define BLUEFRUIT_UART_CTS_PIN -1 // Not used for Flora, set to -1 if unused
#define BLUEFRUIT_UART_RTS_PIN -1 // Not used for Flora, set to -1 if unused
// HARDWARE UART SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the HW serial port you are using. Uncomment
// this line if you are connecting the BLE to Leonardo/Micro or Flora
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_HWSERIAL_NAME Serial1
// SHARED UART SETTINGS
// ----------------------------------------------------------------------------------------------
// The following sets the optional Mode pin, its recommended but not required
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_UART_MODE_PIN -1 // Set to -1 if unused (not using the Mode pin)
// SHARED SPI SETTINGS (Not needed for Flora)
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins to use for HW and SW SPI communication.
// SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when
// using HW SPI. This should be used with nRF51822 based Bluefruit LE modules
// that use SPI (Bluefruit LE SPI Friend).
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_SPI_CS 8 //Does not matter for Flora
#define BLUEFRUIT_SPI_IRQ 7 //Does not matter for Flora
#define BLUEFRUIT_SPI_RST 4 //Does not matter for Flora
// SOFTWARE SPI SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins to use for SW SPI communication.
// This should be used with nRF51822 based Bluefruit LE modules that use SPI
// (Bluefruit LE SPI Friend).
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_SPI_SCK 13 //Does not matter for Flora
#define BLUEFRUIT_SPI_MISO 12 //Does not matter for Flora
#define BLUEFRUIT_SPI_MOSI 11 //Does not matter for Flora
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment