Data Logger Shield Gone Haywire (Arduino Brain Library)

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Re: Data Logger Shield Gone Haywire (Arduino Brain Library)

Postby edan » Fri May 25, 2012 3:38 pm

[deleted]
Last edited by edan on Fri May 25, 2012 3:51 pm, edited 1 time in total.
edan
 
Posts: 26
Joined: Tue Apr 24, 2012 1:10 pm

Re: Data Logger Shield Gone Haywire (Arduino Brain Library)

Postby edan » Fri May 25, 2012 3:41 pm

[deleted]
Last edited by edan on Fri May 25, 2012 3:51 pm, edited 1 time in total.
edan
 
Posts: 26
Joined: Tue Apr 24, 2012 1:10 pm

Re: Data Logger Shield Gone Haywire (Arduino Brain Library)

Postby edan » Fri May 25, 2012 3:42 pm

[deleted]
Last edited by edan on Fri May 25, 2012 3:51 pm, edited 1 time in total.
edan
 
Posts: 26
Joined: Tue Apr 24, 2012 1:10 pm

Re: Data Logger Shield Gone Haywire (Arduino Brain Library)

Postby edan » Fri May 25, 2012 3:47 pm

...and, fail! Serial monitor looks like:

Code: Select all

Initializing SD card...LOGG!


Ok, kill all the RTC functions.
Code: Select all
// #include <RTClib.h>

Code: Select all
// RTC_DS1307 RTC; // define the Real Time Clock object

Code: Select all
/*
// connect to RTC
  Wire.begin(); 
  if (!RTC.begin()) {
    logfile.println("RTC failed");
#if ECHO_TO_SERIAL
    Serial.println("RTC failed");
#endif  //ECHO_TO_SERIAL
  }
*/


Code: Select all
//  DateTime now;

Code: Select all
  // fetch the time
//  now = RTC.now();
edan
 
Posts: 26
Joined: Tue Apr 24, 2012 1:10 pm

Re: Data Logger Shield Gone Haywire (Arduino Brain Library)

Postby edan » Fri May 25, 2012 3:48 pm

...and, forgot to disconnect the headset and got an stk500 error. Oops, fixed.

Ok, now the output is "interesting":
[code]
Initializing SD card...LOGGER62.CSV

millis,stamp,datetime,light,temp,vcc

999, , 1023
edan
 
Posts: 26
Joined: Tue Apr 24, 2012 1:10 pm

Re: Data Logger Shield Gone Haywire (Arduino Brain Library)

Postby edan » Fri May 25, 2012 3:48 pm

(It's safe to ignore the "millis,stamp,datetime,light,temp,vcc" header. The more accurate version would be something like 'millis, disabled date stuff, light reading from non-connected light sensor'...)

No idea why the Brain library isn't producing any output though...

Normally if I upload the default BrainSerialOut sketch, it sends a two-line output to the Serial Monitor:
Code: Select all
Serial.println(brain.readErrors());
      Serial.println(brain.readCSV());

That looks like:
Code: Select all
ERROR: Checksum
200,0,0,2076078,112614,60749,194459,149189,52289,43309,33957


Hmm.
edan
 
Posts: 26
Joined: Tue Apr 24, 2012 1:10 pm

Re: Data Logger Shield Gone Haywire (Arduino Brain Library)

Postby edan » Fri May 25, 2012 3:49 pm

Let's try commenting out wire.h in the original Frankenstein-lighttemplogger, just in case:
Code: Select all
// #include <Wire.h>


Well, it changed the serial output anyway:
[code]

Initializing SD card...LOGGER67.CSV
millis,stamp,datetime,light,temp,vcc
1000, , 1023
E
edan
 
Posts: 26
Joined: Tue Apr 24, 2012 1:10 pm

Re: Data Logger Shield Gone Haywire (Arduino Brain Library)

Postby edan » Fri May 25, 2012 3:50 pm

Any thoughts?
edan
 
Posts: 26
Joined: Tue Apr 24, 2012 1:10 pm

Re: Data Logger Shield Gone Haywire (Arduino Brain Library)

Postby edan » Sat May 26, 2012 1:13 pm

Right, so it's working now.

A couple of things:
I compiled in the MemoryFree library to see if RAM was indeed the problem.
After getting the program working and paring down the libraries to minimum, freememory() returns 412; with Wire.h and RTC.h it returns 218 and goes into a loop after printing 'Initializing SD card...' to serial.

To get the Brain library working properly I removed the delay() function entirely from lighttemplogger. On the advice of the Brain library's author I implemented an if (brain.update()){ } function, but instead of putting just the Serial.print(brain.readCSV) et al inside, I put just about the whole bleeding contents of void loop() inside. (I left the 'flush data to disk' section, for no reason in particular. Maybe this doesn't matter, whatever.)

The reason for using if (brain.update()) as the timer instead of delay() is simple: it seems the Brain library requires the sketch to be constantly calling brain.update() in order to see if there's new data coming in over serial. Calling brain.update() only once a second means it missed about 19 of every 20 packets from the EEG. Since those packets come once a second, only getting one in 20 is not acceptable -- but using the packets as a timer gets just about the same result as the deadly-in-this-case delay function.
edan
 
Posts: 26
Joined: Tue Apr 24, 2012 1:10 pm

Re: Data Logger Shield Gone Haywire (Arduino Brain Library)

Postby edan » Sat May 26, 2012 1:16 pm

Also, here's the (ugly as heck) code as it stands... note this is the last version that triggers the loop due to potentially low memory, you have to comment out the bits related to Wire.h and RTClib.h to get it working...

Code: Select all
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>
#define RTC_INCLUDE 1
#include <MemoryFree.h>

#include <Brain.h>
// #include <SoftwareSerial.h>

// A simple data logger for the Arduino analog pins

// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)

// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()

#define ECHO_TO_SERIAL   1 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()

// the digital pins that connect to the LEDs
#define redLEDpin 2
#define greenLEDpin 3

// The analog pins that connect to the sensors
#define photocellPin 0           // analog 0
#define tempPin 1                // analog 1
#define BANDGAPREF 14            // special indicator that we want to measure the bandgap

#define aref_voltage 3.3         // we tie 3.3V to ARef and measure it with a multimeter!
#define bandgap_voltage 1.1      // this is not super guaranteed but its not -too- off

#if RTC_INCLUDE
RTC_DS1307 RTC; // define the Real Time Clock object
#endif


// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;

// the logging file
File logfile;

// Create a SoftwareSerial connection, TX on pin 4, and RX on pin 3
// SoftwareSerial softBrain(4, 3);

Brain brain(Serial);


void error(char *str)
{
//  Serial.print("error: ");
  Serial.println(str);
 
  // red LED indicates error
  digitalWrite(redLEDpin, HIGH);

  while(1);
}

void setup(void)
{
  Serial.begin(9600);
  Serial.println();
 
Serial.print("freeMemory()=");
    Serial.println(freeMemory());

  // initialize the SD card
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present");
  }
// Serial.println("card initialized.");
 
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break;  // leave the loop!
    }
  }
 
  if (! logfile) {
    error("couldnt create file");
  }
 
  // Serial.print("Logging to: ");
  Serial.println(filename);



// connect to RTC
  Wire.begin(); 
  if (!RTC.begin()) {
    logfile.println("RTC failed");
#if ECHO_TO_SERIAL
    Serial.println("RTC failed");
#endif  //ECHO_TO_SERIAL
  }





  logfile.println("millis,stamp,datetime,light,temp,vcc");   
#if ECHO_TO_SERIAL
  Serial.println("millis,stamp,datetime,light,temp,vcc");
#endif //ECHO_TO_SERIAL

  // If you want to set the aref to something other than 5v
  analogReference(EXTERNAL);
}

void loop(void)
{
   


   // Expect packets about once per second.
   // The .readCSV() function returns a string (well, char*) listing the most recent brain data, in the following format:
   // "signal strength, attention, meditation, delta, theta, low alpha, high alpha, low beta, high beta, low gamma, high gamma"   
   

   

  // delay for the amount of time we want between readings
//  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
 

if (brain.update()){
 

// log milliseconds since starting
  uint32_t m = millis();
  logfile.print(m);           // milliseconds since start
  logfile.print(", ");   
#if ECHO_TO_SERIAL
  Serial.print(m);         // milliseconds since start
  Serial.print(", "); 
#endif


#if RTC_INCLUDE
   DateTime now;
// fetch the time
  now = RTC.now();
  // log time
// logfile.print(now.unixtime()); // seconds since 1/1/1970
logfile.print(", ");
  logfile.print('"');
  logfile.print(now.year(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print(" ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);
  logfile.print('"');
#if ECHO_TO_SERIAL
// Serial.print(now.unixtime()); // seconds since 1/1/1970
  Serial.print(", ");
  Serial.print('"');
  Serial.print(now.year(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
  Serial.print('"');
#endif //ECHO_TO_SERIAL
#endif


/*  analogRead(photocellPin);
  delay(10); */
  int photocellReading = 1023; 
 
/* analogRead(tempPin);
  delay(10);
  int tempReading = analogRead(tempPin);   
 
  // converting that reading to voltage, for 3.3v arduino use 3.3, for 5.0, use 5.0
  float voltage = tempReading * aref_voltage / 1024; 
  float temperatureC = (voltage - 0.5) * 100 ;
  float temperatureF = (temperatureC * 9 / 5) + 32;
  */
 
  logfile.print(", ");   
  logfile.print(photocellReading);
//  logfile.print(", ");   
//  logfile.print(temperatureF);
#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(photocellReading);
//  Serial.print(", ");   
//  Serial.print(temperatureF);






    char* brainerrors = brain.readErrors();
    char* brainCSV= brain.readCSV();
   Serial.println(brainerrors);
Serial.println(brainCSV);

logfile.println(brainerrors);
logfile.println(brainCSV);

   





#endif //ECHO_TO_SERIAL

  // Log the estimated 'VCC' voltage by measuring the internal 1.1v ref
/* analogRead(BANDGAPREF);
  delay(10);
  int refReading = analogRead(BANDGAPREF);
  float supplyvoltage = (bandgap_voltage * 1024) / refReading;
 
  logfile.print(", ");
  logfile.print(supplyvoltage);
#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(supplyvoltage);
#endif // ECHO_TO_SERIAL
*/
  logfile.println();
#if ECHO_TO_SERIAL
  Serial.println();
#endif // ECHO_TO_SERIAL
}

  digitalWrite(greenLEDpin, LOW);

  // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  // which uses a bunch of power and takes time
  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  syncTime = millis();
 
  // blink LED to show we are syncing data to the card & updating FAT!
  digitalWrite(redLEDpin, HIGH);
  logfile.flush();
  digitalWrite(redLEDpin, LOW);
 
}
edan
 
Posts: 26
Joined: Tue Apr 24, 2012 1:10 pm

Re: Data Logger Shield Gone Haywire (Arduino Brain Library)

Postby edan » Sat May 26, 2012 2:19 pm

Here's a final version of the code, which doesn't use the RTC, but works well.
The serial port output (with the EEG not attached to anything) looks like:
Code: Select all
freeMemory()=308
Initializing SD card...LOGGER12.CSV
millis,datetime,eegSignalQuality,eegAttention,eegMeditation,eegDelta,eegTheta,eegLowAlpha,eegHighAlpha,eegLowBeta,eegHighBeta,eegLowGamma,eegMidGamma,eegErrors,arduinoVcc
33204,,200,0,0,961704,948936,321243,799999,353993,680961,159887,680403,,1.10
34200,,200,0,0,1178073,1126832,19134,293223,318321,313967,117719,944390,,1.10
35197,,200,0,0,915391,606309,227217,520855,255481,220556,167184,1536422,,1.10


The file written to the SD card looks like:
Code: Select all
$ cat /Volumes/UNTITLED/LOGGER12.CSV
millis,datetime,eegSignalQuality,eegAttention,eegMeditation,eegDelta,eegTheta,eegLowAlpha,eegHighAlpha,eegLowBeta,eegHighBeta,eegLowGamma,eegMidGamma,eegErrors,arduinoVcc
33204,,200,0,0,961704,948936,321243,799999,353993,680961,159887,680403
,
,1.10
34200,,200,0,0,1178073,1126832,19134,293223,318321,313967,117719,944390
,
,1.10
35197,,200,0,0,915391,606309,227217,520855,255481,220556,167184,1536422
,
,1.10
36194,,200,0,0,546763,793062,122126,196001,74772,523084,220101,898616
,
,1.10
37191,,200,0,0,512321,926258,50426,411561,193521,307559,140287,846748
,
,1.10
38188,,200,0,0,741632,685952,111663,314725,144079,442628,177630,644404
,
,1.10
39184,,200,0,0,928219,1100698,104559,166973,106187,226251,103925,1673322
,
,1.10
40181,,200,0,0,1152892,704336,131955,484996,201688,557181,92337,1089919
,
,1.10


Code: Select all
// Logs data from a connected EEG toy using the Brain library to an SD card plugged into an Adafruit Data Logger Shield


#include <SD.h>
// #include <Wire.h>
// #include <RTClib.h>
#include <MemoryFree.h>
#include <Brain.h>


// how many milliseconds between grabbing data and logging it. 1000 ms is once a second -- deprecated
// #define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)



// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()

#define ECHO_TO_SERIAL   1 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()

// the digital pins that connect to the LEDs
#define redLEDpin 2
#define greenLEDpin 3

#define BANDGAPREF 14            // special indicator that we want to measure the bandgap

#define aref_voltage 3.3         // we tie 3.3V to ARef and measure it with a multimeter!
#define bandgap_voltage 1.1      // this is not super guaranteed but its not -too- off

// RTC_DS1307 RTC; // define the Real Time Clock object


// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;

// the logging file
File logfile;

// Create a new Brain object on hardware serial
// (listening on the RX pin)
Brain brain(Serial);


void error(char *str)
{
//  Serial.print("error: ");
  Serial.println(str);
 
  // red LED indicates error
  digitalWrite(redLEDpin, HIGH);

  while(1);
}

void setup(void)
{   
   Serial.begin(9600);
   Serial.println();

   // for troubleshooting, tell us how much RAM is left
   Serial.print("freeMemory()=");
   Serial.println(freeMemory());

  // initialize the SD card
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present");
  }
// Serial.println("card initialized.");
 
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break;  // leave the loop!
    }
  }
 
  if (! logfile) {
    error("couldnt create file");
  }
 
  // Serial.print("Logging to: ");
  Serial.println(filename);



/* comment this block out if removing Wire.h/RTC.h from includes

// connect to RTC
//  Wire.begin();
  if (!RTC.begin()) {
    logfile.println("RTC failed");
#if ECHO_TO_SERIAL
    Serial.println("RTC failed");
#endif  //ECHO_TO_SERIAL
  }
 
  */




  logfile.println("millis,datetime,eegSignalQuality,eegAttention,eegMeditation,eegDelta,eegTheta,eegLowAlpha,eegHighAlpha,eegLowBeta,eegHighBeta,eegLowGamma,eegMidGamma,eegErrors,arduinoVcc");   
#if ECHO_TO_SERIAL
  Serial.println("millis,datetime,eegSignalQuality,eegAttention,eegMeditation,eegDelta,eegTheta,eegLowAlpha,eegHighAlpha,eegLowBeta,eegHighBeta,eegLowGamma,eegMidGamma,eegErrors,arduinoVcc");
#endif //ECHO_TO_SERIAL

  // If you want to set the aref to something other than 5v
  analogReference(EXTERNAL);
}

void loop(void)
{
   


   // Expect packets about once per second.
   // The .readCSV() function returns a string (well, char*) listing the most recent brain data, in the following format:
   // "signal strength, attention, meditation, delta, theta, low alpha, high alpha, low beta, high beta, low gamma, high gamma"   
   

   

  // delay for the amount of time we want between readings
//  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
 

if (brain.update()){
 

// log milliseconds since starting
  uint32_t m = millis();
  logfile.print(m);           // milliseconds since start
  logfile.print(",");   
#if ECHO_TO_SERIAL
  Serial.print(m);         // milliseconds since start
  Serial.print(","); 
#endif


/* comment all this out if removing Wire.h/RTC.h
   DateTime now;
// fetch the time
  now = RTC.now();
  // log time
// logfile.print(now.unixtime()); // seconds since 1/1/1970
  logfile.print('"');
  logfile.print(now.year(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print(" ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);
  logfile.print('"');
#if ECHO_TO_SERIAL
// Serial.print(now.unixtime()); // seconds since 1/1/1970
  Serial.print('"');
  Serial.print(now.year(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
  Serial.print('"');
#endif //ECHO_TO_SERIAL

*/


   // on to the good stuff, print/log the EEG data
   
    char* brainerrors = brain.readErrors();
    char* brainCSV= brain.readCSV();
#if ECHO_TO_SERIAL
   Serial.print(",");
   Serial.print(brainCSV);
    Serial.print(",");
   Serial.print(brainerrors);
#endif //ECHO_TO_SERIAL

   logfile.print(",");
   logfile.println(brainCSV);
   logfile.print(",");
   logfile.println(brainerrors);



  // Log the estimated 'VCC' voltage by measuring the internal 1.1v ref
  analogRead(BANDGAPREF);
  delay(10);
  int refReading = analogRead(BANDGAPREF);
  float supplyvoltage = (bandgap_voltage * 1024) / refReading;
 
  logfile.print(",");
  logfile.print(supplyvoltage);
#if ECHO_TO_SERIAL
  Serial.print(",");   
  Serial.print(supplyvoltage);
#endif // ECHO_TO_SERIAL
  logfile.println();
#if ECHO_TO_SERIAL
  Serial.println();
#endif // ECHO_TO_SERIAL
}

  digitalWrite(greenLEDpin, LOW);

  // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  // which uses a bunch of power and takes time
  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  syncTime = millis();
 
  // blink LED to show we are syncing data to the card & updating FAT!
  digitalWrite(redLEDpin, HIGH);
  logfile.flush();
  digitalWrite(redLEDpin, LOW);
 
}
edan
 
Posts: 26
Joined: Tue Apr 24, 2012 1:10 pm

Re: Data Logger Shield Gone Haywire (Arduino Brain Library)

Postby edan » Sat May 26, 2012 2:26 pm

Err, that logfile doesn't look right. Should have been using logfile.print instead of logfile.println to print the EEG stuff. Here's the fixed version.

Serial monitor output:
Code: Select all

freeMemory()=308
Initializing SD card...LOGGER13.CSV
millis,datetime,eegSignalQuality,eegAttention,eegMeditation,eegDelta,eegTheta,eegLowAlpha,eegHighAlpha,eegLowBeta,eegHighBeta,eegLowGamma,eegMidGamma,eegErrors,arduinoVcc
19715,,200,0,0,278012,668842,142325,320505,490834,118212,85457,572401,ERROR: Checksum,1.10
20711,,200,0,0,287682,1894180,66595,371821,45959,382731,145089,1453104,ERROR: Checksum,1.10
21708,,200,0,0,322068,213904,37648,73432,12146,73968,29015,348077,ERROR: Checksum,1.10
22705,,200,0,0,631892,1031669,32419,164952,64483,458654,112229,675881,ERROR: Checksum,1.10
23702,,200,0,0,619217,1467119,129039,405954,75416,666706,114956,810149,ERROR: Checksum,1.10
28389,,80,0,0,473687,860884,116241,352291,290308,851508,158445,662009,ERROR: Checksum,1.10
29385,,80,0,0,575739,882394,99855,833027,244817,448659,192398,686223,ERROR: Checksum,1.10
30383,,80,0,0,1083259,886526,31010,527803,184641,267780,154211,1294269,ERROR: Checksum,1.10
37548,,80,0,0,426591,1293735,228691,259559,143018,banned,77287,1061194,ERROR: Checksum,1.10
38544,,80,0,0,696424,795286,119708,749761,171988,512470,181959,754365,ERROR: Checksum,1.10


The file written to disk:
Code: Select all


$ cat /Volumes/UNTITLED/LOGGER13.CSV
millis,datetime,eegSignalQuality,eegAttention,eegMeditation,eegDelta,eegTheta,eegLowAlpha,eegHighAlpha,eegLowBeta,eegHighBeta,eegLowGamma,eegMidGamma,eegErrors,arduinoVcc
19715,,200,0,0,278012,668842,142325,320505,490834,118212,85457,572401,ERROR: Checksum,1.10
20711,,200,0,0,287682,1894180,66595,371821,45959,382731,145089,1453104,ERROR: Checksum,1.10
21708,,200,0,0,322068,213904,37648,73432,12146,73968,29015,348077,ERROR: Checksum,1.10
22705,,200,0,0,631892,1031669,32419,164952,64483,458654,112229,675881,ERROR: Checksum,1.10
23702,,200,0,0,619217,1467119,129039,405954,75416,666706,114956,810149,ERROR: Checksum,1.10
28389,,80,0,0,473687,860884,116241,352291,290308,851508,158445,662009,ERROR: Checksum,1.10
29385,,80,0,0,575739,882394,99855,833027,244817,448659,192398,686223,ERROR: Checksum,1.10
30383,,80,0,0,1083259,886526,31010,527803,184641,267780,154211,1294269,ERROR: Checksum,1.10
37548,,80,0,0,426591,1293735,228691,259559,143018,banned,77287,1061194,ERROR: Checksum,1.10
38544,,80,0,0,696424,795286,119708,749761,171988,512470,181959,754365,ERROR: Checksum,1.10


And the full program code:
Code: Select all

// Logs data from a connected EEG toy using the Brain library to an SD card plugged into an Adafruit Data Logger Shield


#include <SD.h>
// #include <Wire.h>
// #include <RTClib.h>
#include <MemoryFree.h>
#include <Brain.h>


// how many milliseconds between grabbing data and logging it. 1000 ms is once a second -- deprecated
// #define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)



// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()

#define ECHO_TO_SERIAL   1 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()

// the digital pins that connect to the LEDs
#define redLEDpin 2
#define greenLEDpin 3

#define BANDGAPREF 14            // special indicator that we want to measure the bandgap

#define aref_voltage 3.3         // we tie 3.3V to ARef and measure it with a multimeter!
#define bandgap_voltage 1.1      // this is not super guaranteed but its not -too- off

// RTC_DS1307 RTC; // define the Real Time Clock object


// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;

// the logging file
File logfile;

// Create a new Brain object on hardware serial
// (listening on the RX pin)
Brain brain(Serial);


void error(char *str)
{
//  Serial.print("error: ");
  Serial.println(str);
 
  // red LED indicates error
  digitalWrite(redLEDpin, HIGH);

  while(1);
}

void setup(void)
{   
   Serial.begin(9600);
   Serial.println();

   // for troubleshooting, tell us how much RAM is left
   Serial.print("freeMemory()=");
   Serial.println(freeMemory());

  // initialize the SD card
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present");
  }
// Serial.println("card initialized.");
 
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break;  // leave the loop!
    }
  }
 
  if (! logfile) {
    error("couldnt create file");
  }
 
  // Serial.print("Logging to: ");
  Serial.println(filename);



/* comment this block out if removing Wire.h/RTC.h from includes

// connect to RTC
//  Wire.begin();
  if (!RTC.begin()) {
    logfile.println("RTC failed");
#if ECHO_TO_SERIAL
    Serial.println("RTC failed");
#endif  //ECHO_TO_SERIAL
  }
 
  */




  logfile.println("millis,datetime,eegSignalQuality,eegAttention,eegMeditation,eegDelta,eegTheta,eegLowAlpha,eegHighAlpha,eegLowBeta,eegHighBeta,eegLowGamma,eegMidGamma,eegErrors,arduinoVcc");   
#if ECHO_TO_SERIAL
  Serial.println("millis,datetime,eegSignalQuality,eegAttention,eegMeditation,eegDelta,eegTheta,eegLowAlpha,eegHighAlpha,eegLowBeta,eegHighBeta,eegLowGamma,eegMidGamma,eegErrors,arduinoVcc");
#endif //ECHO_TO_SERIAL

  // If you want to set the aref to something other than 5v
  analogReference(EXTERNAL);
}

void loop(void)
{
   


   // Expect packets about once per second.
   // The .readCSV() function returns a string (well, char*) listing the most recent brain data, in the following format:
   // "signal strength, attention, meditation, delta, theta, low alpha, high alpha, low beta, high beta, low gamma, high gamma"   
   

   

  // delay for the amount of time we want between readings
//  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
 

if (brain.update()){
 

// log milliseconds since starting
  uint32_t m = millis();
  logfile.print(m);           // milliseconds since start
  logfile.print(",");   
#if ECHO_TO_SERIAL
  Serial.print(m);         // milliseconds since start
  Serial.print(","); 
#endif


/* comment all this out if removing Wire.h/RTC.h
   DateTime now;
// fetch the time
  now = RTC.now();
  // log time
// logfile.print(now.unixtime()); // seconds since 1/1/1970
  logfile.print('"');
  logfile.print(now.year(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print(" ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);
  logfile.print('"');
#if ECHO_TO_SERIAL
// Serial.print(now.unixtime()); // seconds since 1/1/1970
  Serial.print('"');
  Serial.print(now.year(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
  Serial.print('"');
#endif //ECHO_TO_SERIAL

*/


   // on to the good stuff, print/log the EEG data
   
    char* brainerrors = brain.readErrors();
    char* brainCSV= brain.readCSV();
#if ECHO_TO_SERIAL
   Serial.print(",");
   Serial.print(brainCSV);
    Serial.print(",");
   Serial.print(brainerrors);
#endif //ECHO_TO_SERIAL

   logfile.print(",");
   logfile.print(brainCSV);
   logfile.print(",");
   logfile.print(brainerrors);



  // Log the estimated 'VCC' voltage by measuring the internal 1.1v ref
  analogRead(BANDGAPREF);
  delay(10);
  int refReading = analogRead(BANDGAPREF);
  float supplyvoltage = (bandgap_voltage * 1024) / refReading;
 
  logfile.print(",");
  logfile.print(supplyvoltage);
#if ECHO_TO_SERIAL
  Serial.print(",");   
  Serial.print(supplyvoltage);
#endif // ECHO_TO_SERIAL
  logfile.println();
#if ECHO_TO_SERIAL
  Serial.println();
#endif // ECHO_TO_SERIAL
}

  digitalWrite(greenLEDpin, LOW);

  // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  // which uses a bunch of power and takes time
  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  syncTime = millis();
 
  // blink LED to show we are syncing data to the card & updating FAT!
  digitalWrite(redLEDpin, HIGH);
  logfile.flush();
  digitalWrite(redLEDpin, LOW);
 
}
edan
 
Posts: 26
Joined: Tue Apr 24, 2012 1:10 pm

Re: Data Logger Shield Gone Haywire (Arduino Brain Library)

Postby edan » Sun May 27, 2012 2:10 pm

It works!

I used the Arduino/MindFlex as a sleep monitor last night, producing a 3.4MB CSV data file spanning ~9 hours of sleep. (This is the relevant Arduino sketch: http://pastebin.com/Ye43V8ca

To generate a graph with gnuplot, I created a command file (eeg.gpl> containing the following instructions: (remove the # to graph attention/meditation values, I found them superfluous)
Code: Select all
set datafile separator ","
set key autotitle columnhead
set autoscale x
set yrange [0:1500000]
set y2range [0:100]
set parametric

plot for [i=6:13] file using 1:i with lines smooth bezier, file using 1:3 with lines axis x1y2 smooth csplines #, for [i=4:5] file using 1:i with lines lw 2 axis x1y2 smooth bezier


I then called gnuplot from the command line with the following command:
Code: Select all
gnuplot -e "file='/Path/to/LOGGER00.CSV'" /Path/to/eeg.gpl



The resulting graph (generated through AquaTerm, since I'm on a Mac, looks like this:
Image


In the middle of the graph you can see when the MindFlex headset fell off (!) during the night, and when I put it back on.
edan
 
Posts: 26
Joined: Tue Apr 24, 2012 1:10 pm

Previous

Return to Arduino Shields from Adafruit

Who is online

Users browsing this forum: No registered users and 4 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [108]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[31]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[34]
LCDs & Displays[48]
Components & Parts[70]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[111]
 
Wireless[14]
Cables[62]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]