Rat maze - data logging to SD

I've built an elliptical track for rats with sliding doors operated by servo motors and infrared sensors. I was wondering if you could please help me with an issue I'm having with data logging to SD card.

I need to log data from 5 sensors, and so I've included blocks of data logging code for each condition that meets the requirement of a "response" that needs to be recorded. I'm having the Arduino write a line of text to the CSV file each time a response happens. I'm experiencing a problem where if I have too many blocks of code for data logging in the program (greater than 4) the Arduino does not log anything. No error is given, it simply does not log, but the maze is still completely functional (motors moving when they're supposed to, etc).

If there are 4 or less blocks of data logging code, everything works, including data logging.

I've attached my code, please help. Thank you so much,

K

rat_maze.ino (20.1 KB)

Your problem starts on line 8. It is compounded on line 126.

Strings eat up, unnecessarily, huge amounts of memory. The stuff on line 126 doesn't even need to be wrapped in a String. Just put that text in the call to println(), and use the F() macro to keep it out of SRAM.

http://playground.arduino.cc/Code/AvailableMemory

File data_file = SD.open(file_name, FILE_WRITE);
String log_this_data = String(data_entry) + "," + String(event_time) + "," + String(single_correct_middle) + "," + String(single_incorrect_middle) + "," + String(single_correct_left) + "," + String(single_incorrect_left) + "," + String(single_correct_right) + "," + String(single_incorrect_right) + "," + String(single_lick_left) + "," + String(single_lick_right);
data_file.println(log_this_data);
data_file.close();

I would not use the above mess that many times in your code (13x) as it may happen you are going out of the memory.

Define 2 functions like:

void log_data(){
	file = SD.open(file_name, FILE_WRITE);   // maybe not necessary to do each time
	
	file.print(data_entry);
	file.print(",");
	file.print(event_time);
	file.print(",");
	...
	file.print(single_lick_right);
	file.print("\n");

	file.close();    // maybe not necessary to do each time 
}

void print_log_data(){
	Serial.print(data_entry);
	Serial.print(",");
	Serial.print(event_time);
	Serial.print(",");
	...
	Serial.print(single_lick_right);
	Serial.print("\n");
}

and do call them when necessary. Do not use the String mess..

/////////////////////////// RIGHT LICKOMETER ///////////////////////////
if(lick_value_right == HIGH && lick_lock_right == 0 && bottle_lock_right == 0)
{
	event_time = millis();
	lick_right += 1;
	single_lick_right = 1;
	data_entry += 1;

        log_data();                  // log the data to the SDcard
	print_log_data();             // print out the data

	single_lick_right = 0;
	motor_right.write(0);
	door_lock_middle = 0;
	sensor_lock_left = 1;
	door_lock_left = 1;
	door_lock_left2 = 1;
	sensor_lock_right = 1;
	door_lock_right = 1;
	door_lock_right2 = 1;
	bottle_lock_right = 1;
	activation_time = millis();
}

PaulS:
Strings eat up, unnecessarily, huge amounts of memory. The stuff on line 126 doesn't even need to be wrapped in a String. Just put that text in the call to println(), and use the F() macro to keep it out of SRAM.

What's magic " F() macro"? How it use? Is any description about this macros?

Just read file "revisions.txt"
for "arduino-1.0.5-r2"

@* Support has been added for printing strings stored in flash (program
memory) rather than RAM. Wrap double-quoted strings in F() to indicate
that they should be stored in flash, e.g. Serial.print(F("hello world")).@
Bingo-bongo. )

Thank you all so very much! I will get rid of the string and see what happens!

Kevin