SPI insanity with SD and BMP280

Hi all!

I've been working on a custom altimeter for model rocketry using a BMP280 barometer and SD card (both SPI), among other things. When I started logging data to the SD card, I got a whole host of issues:

  • (SdFat) File.flush() seems to break the SD writer.
  • I have to re-begin() SdFat every time I write, or writing to the SD doesn't work.
  • I have to re-begin() SdFat anyway every few barometer reads, or that doesn't work. It seems to be based on operations rather than time (32 bytes at most), but I'm not sure.

My code is also formatted as a library for ease of customization/modularity later, so I'll attach that. Any ideas are appreciated! The (main) function to look at is ArdIU::store()- it's a template, so it's in the .h file.

ArdIU.cpp (5.17 KB)

ArdIU.h (3.05 KB)

Ghotilabs:
My code is also formatted as a library

It might be better to get the code to work before you do that.

The library itself works, other than this weird anomaly. In fact, it has to be a library, because template functions aren't recognized in the Arduino IDE and are messed up by its optimization algorithms.

I have code that works. It's just VERY finicky and dependent on tiny changes in the code. I just want to know why it's such a pain.

Might not be related, but in begin(), just after

pinMode(csSD, OUTPUT);
digitalWrite(csSD, HIGH);

add

pinMode(csBaro, OUTPUT);
digitalWrite(csBaro, HIGH);
SPI.begin();

I tried setting the pins high but that didn't seem to help anything. What is the SPI.begin() for? I would have guessed that that was all done by the various libraries.