Is there interference between microsd card (SD library) and the brain library?

I have a very similar problem as this guy Data Logger Shield Gone Haywire (Arduino Brain Library) - Programming Questions - Arduino Forum ; however, it does not look like that was solved.
Project: I want to use an arduino to connect a toy eeg and log all of the data to a micro sd card. I am using the eeg from this tutorial How to Hack Toy EEGs | Frontier Nerds . It works perfectly on it's own. I am using a microSD card with a simple breakout from sparkfun SparkFun microSD Transflash Breakout - BOB-00544 - SparkFun Electronics. It also works perfectly on it's own

Problem: When I try to merge both of them together, it does not work. I have narrowed the problem down to one thing. On the eeg module, when the code calls brain.update() it returns false and not true.
BTW eeg returns a char, in my code a casted it to a string
So I think there is something in these libraries that are conflicting with each other, I am just not sure what.

Code for just the eeg (example from the library)

// Arduino Brain Library - Brain Serial Test

// Description: Grabs brain data from the serial RX pin and sends CSV out over the TX pin (Half duplex.)
// More info: https://github.com/kitschpatrol/Arduino-Brain-Library
// Author: Eric Mika, 2010 revised in 2014

#include <Brain.h>

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

void setup() {
    // Start the hardware serial.
    Serial.begin(9600);
}

void loop() {
    // 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());
    }
}

Code with just the SD card also just example

/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
 	
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  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)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "This is a test. Jacob stein ";


  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}

Combined code, modified for debugging. It returns "the head does not work" but it successfully writes to the micro sd card.

#include <SD.h>
#include <Brain.h>
Brain brain(Serial);
const int chipSelect = 10;
String dataToWrite;


void setup(){
  Serial.begin(9600);
  Serial.println("Initializing SD...");
  pinMode(chipSelect,OUTPUT);
  if (!SD.begin(chipSelect)){
    Serial.println("Card failed!!!");
    return;
  }
  Serial.println("Card Initialized.");
}

void loop(){
  if (brain.update()) {
        Serial.println(brain.readErrors());
        Serial.println(brain.readCSV());
    }
    else
    
    {
      
     Serial.println("The Head does not work"); 
    }
  File dataFile = SD.open("testtest.txt",FILE_WRITE);
  if(dataFile ){
    
    dataFile.println("hello");
    dataFile.close();
    Serial.print("Wrote to the SD: ");
    Serial.println("hello");
    }
  else{
    Serial.println("Couldn't make file!!!");
  }
delay(2000);
}

Earlier I had one if statement check if( brain.update() && dataFile) and then add the brain.readcsv() to the file. It wouldnt go into the body of the if statement though.

Three suggestions.

  • each time through loop() you open the sd card, but you don't close it.
  • opening the sd card each time through the loop is also going to slow down the rate of calls to brain.update().
  • You have added a delay(2000) at the end of loop which wasn't in the original brain code and which will also reduce the rate of calls to brain.update().

Pete