I must admit to being quite new to Arduinos (been playing for about 6 weeks). One problem I have not been able to solve on my own or with previous forum posts:
I can't seem to write to the SD card within a Timer1 ISR(). Is this not possible? Or am I missing something? I am able to write to the card when inside the setup, so I don't believe it's my card or shield.
I'm using the Mega 2560 with the Adafruit Data Logger Shield. Here is the code that is refusing to work for me
/*
Timer interrupt. Using 16bit Timer/Counter 1 for mega 2560
*/
const int chipSelect = 10;
#include <avr/interrupt.h>
#include <avr/io.h>
#include <SdFat.h>
unsigned int count = 0;
SdFat sd;
SdFile aFile;
//*****************************************************************
//Â setup()
//*****************************************************************
void setup()Â {
Â
 Serial.begin(9600);  //testing
 pinMode(10, OUTPUT); //SD card pin
 if (!sd.init(SPI_FULL_SPEED, chipSelect)) {
  Serial.println("init error");
  sd.initErrorHalt();
 }
 //***************setting up Timer1******************
 DDRB = 0x01;  //PB0 aka pin 53
 PORTB = 0x00; //start with all pins LOWÂ
 //setup Timer1 to fire every 250ms
//Â TCCR1A = 0x00;Â Â //disable Timer1 while we set it up
 TCCR1B = 0x00;  //disable Timer1 while we set it up
 //reset timer/counter1
 TCNT1 = 0x0000;    //set counter to 0, to count up to Output Compare Register Â
 TCCR1A = 0x00; //Timer1 Control Reg A: Wave Gen Mode normal
 //TCCR1B bits 2:0 clock select bits, 011=clk(io)/64(from prescalar)
 TCCR1B = 0x03; //Timer1 Control Reg B: prescalar set to 64
 OCR1A = 0xF487;  //set Output Compare Reg to 62499 Â
 //turn on timer1Â
 TIMSK1 = 0x02;    //enable output compare interrupt for OCR1AÂ
 sei();  //enables interrupts
}Â Â Â Â Â Â //************** end setup()
Â
//***************************************
//Â Â main loop()
//***************************************
void loop()Â
{
}Â Â Â Â Â Â //*********************end main loop()
//*******************************************
//Â Â Â Â logSD()
//*******************************************
void logSD()Â
{
   if (!aFile.open("log.csv", O_RDWR | O_CREAT | O_APPEND))Â
   {
    sd.errorHalt("opening log.csv for write failed in logSD()");
   }
   aFile.println("in loop");
   aFile.close();
}Â Â Â Â Â Â //*************end logSD()
//********************************************************
//Â Â Â Â Â ISR for Timer1
//**********************************************************
ISR(TIMER1_COMPA_vect)Â {
  count++; //testing
Â
  PORTB= 0x01; //switch pin 53 to HIGH, PB0
  logSD();
  PORTB = 0x00;  //switch to lowÂ
 Â
  Serial.println(count, DEC); //testing Â
  OCR1A += 0xF487;   //set output compare to next value
}Â Â Â Â Â Â //*******************end ISR for Timer1
I have tried simply writing to SD card inside the ISR without the subroutine logSD(), but the result is the same either way for me. Thanks in advance for any helpful suggestions or pointers to places I may have missed with my googling skills.