Avr hangs

Hi Guys,
Just made a lap timer on my mega1284. And it works pretty well - I have it printing the values to my terminal however I am trying to write the data to sd card. If I replace the code that reads button presses and stores the “time gap” between presses in an array with arbitrary values in the array positions, it has no problem writing to the sd card - so the code that writes to the sd card is fine:

printf("7 clicks in total\n");
res = f_open(&fl1, "0:rotor.txt", FA_CREATE_ALWAYS | FA_WRITE); //open file to write to
   if (res != FR_OK) { 
      printf("file open failed, reason: %d\n", res); 
   } 
// *****initiating timings******************************** /
 while (PINB&(1<<PB4));  // loop here until first button press

    timer_tick=0;// here is where we start timing
    _delay_ms(10);  // delay for 10 ms to debounce button press
    while (!(PINB&(1<<PB4)));  // loop here until button is released
    _delay_ms(10);  // delay for 10 ms to debounce button release

   //cumulative timings. j is 0 - 5 because first press is above
   for (j=0;j<6;j++){

    while (PINB&(1<<PB4));  // loop here until second button press
    rotor[j] = (double)timer_tick/1000;      //get the value of the millisecond counter
    _delay_ms(10);  // delay for 10 ms to debounce button press
	printf("[%d] time: %f\n",j+1, rotor[j]);
 while (!(PINB&(1<<PB4)));  // loop here until button is released
    }
printf("storing data\n");
printf("%.3f, %.3f, %.3f, %.3f, %.3f, %.3f\n\n", rotor[0], rotor[1], rotor[2], rotor[3], rotor[4], rotor[5]);

sprintf(stringwrite, "%.3f, %.3f, %.3f, %.3f, %.3f, %.3f\n", rotor[0], rotor[1], rotor[2], rotor[3], rotor[4], rotor[5]);
int stringlength = strlen(stringwrite);
printf("length = %d\n",stringlength);
		res = f_write(&fl1, stringwrite, strlen(stringwrite), &s2); //write to file
		if (res != FR_OK)
		{ 
			printf("write failed: %d\n", res);
		}
printf("finished writing\n");
f_close(&fl1);

So the user presses the button 7 times to store 6 times. They appear nicely on the terminal. It then prints a string to the terminal of those times. All good. It then creates a string with sprintf, prints the length of the string and tries to save that to the sd card. It hangs though at

printf("length = %d\n",stringlength);

As I said, if I replace all the code that actually collects the timings with just arbitrary values stored in the array rotor[] it has no issues writing to the sd card.
Any ideas?
Cheers guys

Hello,

I have not looked over your code in detail, but it looks like a good candidate for the strategy of debugging that I generally recommend: simplify your code as much as possible until you find the minimal program that demonstrates the problem. For example, if the SD card code works fine, take it out! You should be able to get down to something about three lines long that has the problem. Then, if you still have a question, you can post your entire code here - along with a slightly different version that does work.

Without simplification, it is going to be very hard for anyone else to guess what problem you are having, since the problem could be in the code that you are not showing, or it could depend on something complicated about the code you are showing. And with simplification, you will very likely see the problem yourself.

-Paul

Thanks for your response Paul. It turns out that I learnt something quite massive about SPI.
I moved CS off the SS line to free it up for the button, however the button requires its pin to be an input, but if the SS pin is an input the IC goes into slave mode. What I didnt realise that moving CS off SS does not also move this feature. SS is always the line that decides whether its slave or master.
By adding my own button rather than the one on the dev board attached to SS I have overcome the problem.