SHARE
TWEET

arduino eeg logger

a guest May 27th, 2012 180 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Logs data from a connected EEG toy using the Brain library to an SD card plugged into an Adafruit Data Logger Shield
  3.  
  4.  
  5. #include <SD.h>
  6. // #include <Wire.h>
  7. // #include <RTClib.h>
  8. #include <MemoryFree.h>
  9. #include <Brain.h>
  10.  
  11.  
  12. // how many milliseconds between grabbing data and logging it. 1000 ms is once a second -- deprecated
  13. // #define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)
  14.  
  15.  
  16.  
  17. // how many milliseconds before writing the logged data permanently to disk
  18. // set it to the LOG_INTERVAL to write each time (safest)
  19. // set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
  20. // the last 10 reads if power is lost but it uses less power and is much faster!
  21. #define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
  22. uint32_t syncTime = 0; // time of last sync()
  23.  
  24. #define ECHO_TO_SERIAL   1 // echo data to serial port
  25. #define WAIT_TO_START    0 // Wait for serial input in setup()
  26.  
  27. // the digital pins that connect to the LEDs
  28. #define redLEDpin 2
  29. #define greenLEDpin 3
  30.  
  31. #define BANDGAPREF 14            // special indicator that we want to measure the bandgap
  32.  
  33. #define aref_voltage 3.3         // we tie 3.3V to ARef and measure it with a multimeter!
  34. #define bandgap_voltage 1.1      // this is not super guaranteed but its not -too- off
  35.  
  36. // RTC_DS1307 RTC; // define the Real Time Clock object
  37.  
  38.  
  39. // for the data logging shield, we use digital pin 10 for the SD cs line
  40. const int chipSelect = 10;
  41.  
  42. // the logging file
  43. File logfile;
  44.  
  45. // Create a new Brain object on hardware serial
  46. // (listening on the RX pin)
  47. Brain brain(Serial);
  48.  
  49.  
  50. void error(char *str)
  51. {
  52. //  Serial.print("error: ");
  53.   Serial.println(str);
  54.  
  55.   // red LED indicates error
  56.   digitalWrite(redLEDpin, HIGH);
  57.  
  58.   while(1);
  59. }
  60.  
  61. void setup(void)
  62. {  
  63.    Serial.begin(9600);
  64.    Serial.println();
  65.  
  66.    // for troubleshooting, tell us how much RAM is left
  67.    Serial.print("freeMemory()=");
  68.    Serial.println(freeMemory());
  69.  
  70.   // initialize the SD card
  71.   Serial.print("Initializing SD card...");
  72.   // make sure that the default chip select pin is set to
  73.   // output, even if you don't use it:
  74.   pinMode(10, OUTPUT);
  75.  
  76.   // see if the card is present and can be initialized:
  77.   if (!SD.begin(chipSelect)) {
  78.     error("Card failed, or not present");
  79.   }
  80. // Serial.println("card initialized.");
  81.  
  82.   // create a new file
  83.   char filename[] = "LOGGER00.CSV";
  84.   for (uint8_t i = 0; i < 100; i++) {
  85.     filename[6] = i/10 + '0';
  86.     filename[7] = i%10 + '0';
  87.     if (! SD.exists(filename)) {
  88.       // only open a new file if it doesn't exist
  89.       logfile = SD.open(filename, FILE_WRITE);
  90.       break;  // leave the loop!
  91.     }
  92.   }
  93.  
  94.   if (! logfile) {
  95.     error("couldnt create file");
  96.   }
  97.  
  98.   // Serial.print("Logging to: ");
  99.   Serial.println(filename);
  100.  
  101.  
  102.  
  103. /* comment this block out if removing Wire.h/RTC.h from includes
  104.  
  105. // connect to RTC
  106. //  Wire.begin();
  107.   if (!RTC.begin()) {
  108.     logfile.println("RTC failed");
  109. #if ECHO_TO_SERIAL
  110.     Serial.println("RTC failed");
  111. #endif  //ECHO_TO_SERIAL
  112.   }
  113.  
  114.   */
  115.  
  116.  
  117.  
  118.  
  119.   logfile.println("millis,datetime,eegSignalQuality,eegAttention,eegMeditation,eegDelta,eegTheta,eegLowAlpha,eegHighAlpha,eegLowBeta,eegHighBeta,eegLowGamma,eegMidGamma,eegErrors,arduinoVcc");    
  120. #if ECHO_TO_SERIAL
  121.   Serial.println("millis,datetime,eegSignalQuality,eegAttention,eegMeditation,eegDelta,eegTheta,eegLowAlpha,eegHighAlpha,eegLowBeta,eegHighBeta,eegLowGamma,eegMidGamma,eegErrors,arduinoVcc");
  122. #endif //ECHO_TO_SERIAL
  123.  
  124.   // If you want to set the aref to something other than 5v
  125.   analogReference(EXTERNAL);
  126. }
  127.  
  128. void loop(void)
  129. {
  130.    
  131.  
  132.  
  133.    // Expect packets about once per second.
  134.    // The .readCSV() function returns a string (well, char*) listing the most recent brain data, in the following format:
  135.    // "signal strength, attention, meditation, delta, theta, low alpha, high alpha, low beta, high beta, low gamma, high gamma"  
  136.    
  137.  
  138.    
  139.  
  140.   // delay for the amount of time we want between readings
  141. //  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
  142.  
  143.  
  144. if (brain.update()){
  145.  
  146.  
  147. // log milliseconds since starting
  148.   uint32_t m = millis();
  149.   logfile.print(m);           // milliseconds since start
  150.   logfile.print(",");    
  151. #if ECHO_TO_SERIAL
  152.   Serial.print(m);         // milliseconds since start
  153.   Serial.print(",");  
  154. #endif
  155.  
  156.  
  157. /* comment all this out if removing Wire.h/RTC.h
  158.    DateTime now;
  159. // fetch the time
  160.   now = RTC.now();
  161.   // log time
  162. // logfile.print(now.unixtime()); // seconds since 1/1/1970
  163.   logfile.print('"');
  164.   logfile.print(now.year(), DEC);
  165.   logfile.print("/");
  166.   logfile.print(now.month(), DEC);
  167.   logfile.print("/");
  168.   logfile.print(now.day(), DEC);
  169.   logfile.print(" ");
  170.   logfile.print(now.hour(), DEC);
  171.   logfile.print(":");
  172.   logfile.print(now.minute(), DEC);
  173.   logfile.print(":");
  174.   logfile.print(now.second(), DEC);
  175.   logfile.print('"');
  176. #if ECHO_TO_SERIAL
  177. // Serial.print(now.unixtime()); // seconds since 1/1/1970
  178.   Serial.print('"');
  179.   Serial.print(now.year(), DEC);
  180.   Serial.print("/");
  181.   Serial.print(now.month(), DEC);
  182.   Serial.print("/");
  183.   Serial.print(now.day(), DEC);
  184.   Serial.print(" ");
  185.   Serial.print(now.hour(), DEC);
  186.   Serial.print(":");
  187.   Serial.print(now.minute(), DEC);
  188.   Serial.print(":");
  189.   Serial.print(now.second(), DEC);
  190.   Serial.print('"');
  191. #endif //ECHO_TO_SERIAL
  192.  
  193. */
  194.  
  195.  
  196.    // on to the good stuff, print/log the EEG data
  197.    
  198.     char* brainerrors = brain.readErrors();
  199.     char* brainCSV= brain.readCSV();
  200. #if ECHO_TO_SERIAL
  201.    Serial.print(",");
  202.    Serial.print(brainCSV);
  203.     Serial.print(",");
  204.    Serial.print(brainerrors);
  205. #endif //ECHO_TO_SERIAL
  206.  
  207.    logfile.print(",");
  208.    logfile.print(brainCSV);
  209.    logfile.print(",");
  210.    logfile.print(brainerrors);
  211.  
  212.  
  213.  
  214.   // Log the estimated 'VCC' voltage by measuring the internal 1.1v ref
  215.   analogRead(BANDGAPREF);
  216.   delay(10);
  217.   int refReading = analogRead(BANDGAPREF);
  218.   float supplyvoltage = (bandgap_voltage * 1024) / refReading;
  219.  
  220.   logfile.print(",");
  221.   logfile.print(supplyvoltage);
  222. #if ECHO_TO_SERIAL
  223.   Serial.print(",");  
  224.   Serial.print(supplyvoltage);
  225. #endif // ECHO_TO_SERIAL
  226.   logfile.println();
  227. #if ECHO_TO_SERIAL
  228.   Serial.println();
  229. #endif // ECHO_TO_SERIAL
  230. }
  231.  
  232.   digitalWrite(greenLEDpin, LOW);
  233.  
  234.   // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  235.   // which uses a bunch of power and takes time
  236.   if ((millis() - syncTime) < SYNC_INTERVAL) return;
  237.   syncTime = millis();
  238.  
  239.   // blink LED to show we are syncing data to the card & updating FAT!
  240.   digitalWrite(redLEDpin, HIGH);
  241.   logfile.flush();
  242.   digitalWrite(redLEDpin, LOW);
  243.  
  244. }
RAW Paste Data
Top