SonicDH

Portable EEG w/ SD Card Logging

May 22nd, 2014
308
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Arduino Brain Library - Brain Serial Test
  2.  
  3. // Description: Grabs brain data from the serial RX pin and sends CSV out over the TX pin (Half duplex.)
  4. // More info: https://github.com/kitschpatrol/Arduino-Brain-Library
  5. // Author: Eric Mika, 2010 revised in 2014, Adapted for local SD Card Logging by Victor Frost
  6. const int chipSelect = 10;
  7. #include <Brain.h>
  8. #include <SdFat.h>
  9. SdFat sd;
  10. SdFile myFile;
  11. // Set up the brain parser, pass it the hardware serial object you want to listen on.
  12. Brain brain(Serial);
  13. void logEvent(const char *msg) {
  14.   // create or open a file for append
  15.   ofstream sdlog("LOGFILE.CSV", ios::out | ios::app);
  16.  
  17.   // append a line to the file
  18.   sdlog << msg << endl;
  19.  
  20.   // check for errors
  21.   if (!sdlog) sd.errorHalt("append failed");
  22.  
  23.   sdlog.close();
  24. }
  25. void setup() {
  26.   pinMode(A0, OUTPUT);  
  27.   digitalWrite(A0, HIGH);
  28.     // Start the hardware serial and get the SD Card ready.
  29.     Serial.begin(9600);
  30.       while (!Serial) {}  // wait for Leonardo
  31.     Serial.println("Starting Up");
  32.      if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
  33.      Serial.print("Logging Brain Activity");    
  34. }
  35.  
  36. void loop() {
  37.     // Expect packets about once per second.
  38.     // The .readCSV() function returns a string (well, char*) listing the most recent brain data, in the following format:
  39.     // "signal strength, attention, meditation, delta, theta, low alpha, high alpha, low beta, high beta, low gamma, high gamma"    
  40.     // Also, log the data to the SDcard, since it's there.
  41.     if (brain.update()) {
  42.         Serial.println(brain.readErrors());
  43.         Serial.println(brain.readCSV());
  44.         logEvent(brain.readCSV());
  45.     }
  46. }
RAW Paste Data