Data Logger Shield Gone Haywire (Arduino Brain Library)

(also posted to LadyAda forums)

So I've got the awesome Data Logger Shield. When I upload the demo sketch (lighttemplogger - GitHub - adafruit/Light-and-Temp-logger: Example code for Adafruit data logging shield http://github.com/kitschpatrol/Arduino-Brain-Library/zipball/master ), it works great! Time to make it do something useful. I want to log data from an EEG toy using the Arduino Brain Library (How to Hack Toy EEGs | Frontier Nerds).Turns out the Brain library already outputs data in CSV, sweet.

I don't know much about coding, so I just took the relevant bits of the Brain library's BrainSerialOut program:

#include <Brain.h>

// Set up the brain parser, pass it the hardware serial object you want to listen on.
Brain brain(Serial);

void loop() {
if (brain.update()) {
  		Serial.println(brain.readErrors());
		Serial.println(brain.readCSV());
	}
}

and grafted them onto lighttemplogger:

#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <Brain.h>

[...]

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

// the logging file
File logfile;

Brain brain(Serial);

[...]

void loop(void)
{
  DateTime now;
	// 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"	
	if (brain.update()) {
  		Serial.println(brain.readErrors());
		Serial.println(brain.readCSV());
	}

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

[...]

Right, then.

When I run lighttemplogger, I get the following happy output over serial monitor:

Initializing SD card...card initialized.
Logging to: LOGGER47.CSV
millis,stamp,datetime,light,temp,vcc
999, 1337967332, "2012/5/25 17:35:32", 1023, 535.42, 1.10
1999, 1337967333, "2012/5/25 17:35:33", 1023, 535.42, 1.10
2999, 1337967334, "2012/5/25 17:35:34", 1023, 535.42, 1.10

When I run my version of the code (with the Brain library Frankensteined in), I get something a little more monstrous:

Initializin
ÿÿÿÿÿ¸ÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	À¸ÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	¸ÕßÜÿ	À¸ÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	?¸ÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	ðÄã¸ÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	?¸ÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	ñ¸ÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	ŸÕßÜÿ	¸ÕßÜÿ	ŸÕßÜÿ	à¸ÕßÜÿ	?¸ÕßÜÿ

Any ideas what could be going wrong here?

And yes, just so we're clear, I'm aware that my modifications to the lighttemplogger sketch wouldn't actually log brain data to the card. I'm just trying to take it slow and get the Brain library to work with a logging sketch before I actually get logging.

Any ideas what could be going wrong here?

Yes, and you're hiding the most interesting part of your code: Where are you writing to the SD card ?
There's your error ... :wink:

BTW1: I'm too lazy to search for that Brain library: What's the data type returned by Brain::readCSV() ?
BTW2: It's a bit unclear to me why there is no output visible from

   Serial.println(brain.readErrors());

I had expected at least an empty line in the Serial Monitor...