Weird problems with a datalogging program

So, I've been working on a datalogging scripts, and so far have had some fun with coding on the arduino. Good, healthy fun, usually, but now I've run in to a few snags.

Snag no. 1:
The code seems to just... stop. Just after running for about 10 seconds. Oddly; the actual number seems consistent between resets/restarts, but whenever I change a bit of code later on (for example; change the contents of a string), it changes.
Snag no. 2:
When I try to store some values in a 2d array, nothing happens. Really, nothing; the added screen (which I use for debugging right now) blinks, but doesn't show anything after that. Which is odd, since the code adding something to the array is in loop(), and there's a bit of the initialization feedback in setup(), which doesn't get shown.
I've also got an oddity; whenever I change a counter ('datapoint') to int instead of byte, I get an error on the SD card. Which works fine otherwise.

My code:

#include <U8glib.h> // screen
#include <DS1302.h> // time-keeping thingy
#include <SPI.h> // spi
#include <SD.h> // sd

U8GLIB_PCD8544 u8g(4, 3, 1, 2, 0); //init screen
DS1302 rtc(7, 6, 5); // Init time thingy

const int sd_select = 10; // sd card slave pin
Sd2Card card;
SdVolume volume;
SdFile root;

File datalog;
const int datalimit = 60;
byte datapoint = 0; //if this is an int, logstart() shows !datalog to be true :S
const int samplefreq = 1000;
String fb[5];
unsigned long times[datalimit];
int data[3][datalimit];

void setup() {
  screeninitialize();
  sdinitialize();
  timeinitialize();
  draw();
  delay(2500);
  logstart();
}
void loop() {
	long unsigned t = millis()+samplefreq;
	fb[0] = rtc.getDateStr();
	fb[1] = rtc.getTimeStr();
	fb[2] = "MILLIS() = "+String(millis());
	fb[3] = "DP = "+String(datapoint);
	delay(10);
	draw();	// show data
	
	//data[1][datapoint] = datapoint; // if this is uncommented, nothing happens. Really, just a blank screen.
	
	datapoint++;
	if (datapoint >= datalimit){
		logwrite(); // if array is full, write data off
	}
	
	if (millis()<t | millis()<t-(samplefreq*2) | millis()>t+(samplefreq*2)){ // if we're still in our timeframe...
		delay(t-millis()); // wait until we're out (note the overflow protection)
	} // if we're already out our window, we quickly go to the next phase
}
void draw(){
  u8g.firstPage();
  do {
    for (int x = 0;x<6;x++){
      u8g.setPrintPos(0, ((x+1)*9));
      u8g.print(fb[x]);
    }
  }
  while( u8g.nextPage() );
  for (int x = 0;x<6;x++){fb[x] = "";};
}
void screeninitialize(){
  u8g.setRot180();
  u8g.setColorIndex(1);
  u8g.setFont(u8g_font_helvR08);
  fb[0] = "Screen located";
}
void sdinitialize(){
  pinMode(sd_select, OUTPUT);
  if (!SD.begin()) {
    fb[1] = "ERROR WITH SD CARD";
    draw(); //show error
    while (1){} //stay there
  }
  fb[1] = "SD found";
}
void timeinitialize(){
	fb[2] = rtc.getTimeStr();
	fb[3] = rtc.getDateStr();
}
void logstart(){
  datalog = SD.open("datalog.txt", FILE_WRITE);// create file, using start date and increment
  if (!datalog){fb[1] = "SD ERROR";fb[2] = "LOGSTART";draw();while (1){}} //give error, stay there
  String tempstr = "Start of measurement: ";
  tempstr += rtc.getDOWStr();
  tempstr += ", "; 
  tempstr += rtc.getDateStr();
  tempstr += ", "; 
  tempstr += rtc.getTimeStr();
  datalog.println(tempstr);
  datalog.close();// close file
}
void logwrite(){
  datalog = SD.open("datalog.txt", FILE_WRITE); // (re-)open file
  if (!datalog){fb[1] = "SD ERROR";fb[2] = "LOGWRITE";draw();while (1){}} //give error, stay there
  while (datapoint+1<0){
	datalog.print(String(times[datapoint]));
	datapoint--;
  }
  datalog.close();
}

Due to the weirdness of the errors, I assume they share some common origin, but as of yet have had no luck in finding it. Can anyone spot one (or more) glaring issues?

So, I've been working on a datalogging scripts

On which Arduino? I'd venture to guess that you are running out of memory. You have some large arrays and the dreaded String class (an array of them, nonetheless).

If you say the words "weird" and "array", you probably have ram overflow.
You could test for the amount of free ram, Arduino Playground - AvailableMemory

Some advice not to use the String class at all.

You have included libraries (with buffers) and the String class (eats ram) and have a few arrays (more ram usage).

You could upgrade to the Arduino Mega 2560.

Or remove the String class, and use the "F()" macro. That places a string in flash, without ram use. But I'm not sure that can be used as your code is now.
You can do this:

datalog.println( F("Hello log file"));      // string only in flash

(while I was writing this, Pauls wrote the same)

Adding this to the feedback on the screen

fb[4] = String(freeMemory());

shows a number starting from 440, going down with 4 or 5 every time, with the arduino (uno, sorry) hanging on 394.
Now this doesn't show the available memory in all cases, so I set the datalimit to 10, but to no avail.

Sidebar:
A matrix of 360 integer datapoints and the 'times', a set of five strings with around 8 chars on average, and some assorted variables, should total around:
3
602+460+5*8+5 = 360+240+40+5 = 645 bytes. Can I then assume that the other libraries use around 1kb of memory, given that i end up with something near 400 bytes left?

If I understand the arduino correctly, it should do something similar to pre-allocating memory, in that it tries to find out how much memory it needs beforehand. So, the number going down with every iteration seems weird. Combining weird with strings, I wound up in the draw() function, which originally relied on a smaller font which enabled 6 lines of feedback (fb). In other words; it was accessing and emptying fb[5], which doesn't exist. Correcting this to 5 (making it stop with fb[4]) gets the arduino past iteration 8 (snag 1) and for some weird reason doesn't make it hang whenever I try to store something in the array (snag 2). So, I found a (stupid) mistake, which was causing both snags. THANK YOU!

Off-note; storing the five strings using the PROGMEM method seems like a quite interesting thing to look at, and I'll certainly try to incorporate it! Thanks again!

I'm glad you found it.

It is not only the libraries, but the also String class requires ram.
With a number of libraries, the 1k could be normal.
Some libraries use buffers for incoming and buffers for outgoing data and also copies of buffers.

The PROGMEM is not the same as the "F()" macro. But I don't understand the difference, as you can read in my post, PROGMEM and F() macro as parameters for function - #3 by system - Programming Questions - Arduino Forum

The PROGMEM can be use for you data.

The "F()" macro can be used with libraries that use the stream class (with write, print and println). Like the Serial and the SD library.

I am also having problems storing data in an array using a while loop. I am using the Arduino Mega2560 board.

I am trying to log two data points (stepper motor position and counts per second) in a 2d array. The number of data points (data_i) are calculated using the start, end and step width. Below is the partial coder where I try to populate the array with the motor position "ai" and the accumulated counts "timerCounts".

The compiler produces the error message "array_data was not declared in this scope" for the code "array_data[0][data_index] = {ai};"

Is this a limitation in the usage of arrays or a coding error?

Thanks.

/*int array_data[2][data_i];*/               //array with two columns and "data_i" rows 

 while (data_collection == true) {
  
      Serial.print((float(ai)/1000),3);Serial.print(" ");
        
//-----------------------Call ISR to count pulses----------------------------------------------------------
// stop Timer 0 interrupts from throwing the count out
  byte oldTCCR0A = TCCR0A;
  byte oldTCCR0B = TCCR0B;
  TCCR0A = 0;    // stop timer 0
  TCCR0B = 0;

    
  
  startCounting (di);  // how many mS to count for, call function to count pulses from SCA

  while (!counterReady) 
     { }  // loop until count over

  // adjust counts by counting interval to give frequency in Hz
  float frq = (timerCounts *  1000.0) / timerPeriod;

  // restart timer 0
  TCCR0A = oldTCCR0A;
  TCCR0B = oldTCCR0B;

  /*Serial.print ("Frequency: ");
  Serial.println ((unsigned long) frq);*/
  Serial.println (timerCounts);
  
//----------------------------------------------------------
  array_data[0][data_index] = {ai};			        //current motor position
  array_data[1][data_index] = {timerCounts};		//accumulated counts
  data_index++;
//-----------------------------------------------------------
  
  // let serial stuff finish
  delay(200);

Is this a limitation in the usage of arrays or a coding error?

Looks like a coding error. In cases like this, "Below is the partial coder where I try" isn't a good idea. Try something more like "Below is the ALL the coder where I try".

"... was not declared in this scope"

As a beginner, I've seen this quite a few times. It's essentially saying the array isn't there, which is usually due to it being declared outside the scope of this specific piece of code. Your coding style seems to be quite different than my own, it seems to be only an exerpt, so actually pointing to a line and saying "That's your problem, right there" isn't possible.

You could try to mark which pieces of code work in separate scopes (of make a small flowchart. I like flowcharts), and then follow the variable throughout the code. To check if a variable is accessible in a specific part of the code (to help track it; to see where it's not declared), you could add small lines like "var = var", just so the debugger will mark it if it can't find that specific variable.