Hi all,
I purchased an Arduino UIno board and sparkfun microsd Shield about 1-2 months ago. I'm trying to collect pressure data that records to the sd card. I got it all working and installed a heartbeat LED to indicate that data is being is logged. Everything works when I use the usb cable for power but I'm having issues when I use my 9v and 12v power packs. When I use a power pack connection, the heart beat led does not pulse and the logging is unreliable. Sometimes I'll have data, no date, or a corrupted file.
I'm not sure what is going on and could use some help. My thought is this must be a hardware issue since the board is getting powered but not functioning correctly. Attached is my code which I adapted from the SDFAT library example:
// A simple data logger for the Arduino analog pins with optional DS1307
// uses RTClib from GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC Arduino library
#include <SdFat.h>
#include <SdFatUtil.h> // define FreeRam()
#define SD_CHIP_SELECT SS // SD chip select pin
#define USE_DS1307 0 // set nonzero to use DS1307 RTC
#define LOG_INTERVAL 1000 // mills between entries
#define SENSOR_COUNT 3 // number of analog pins to log
#define ECHO_TO_SERIAL 0 // echo data to serial port if nonzero
#define WAIT_TO_START 1 // Wait for serial input in setup()
#define ADC_DELAY 0 // ADC delay for high impedence sensors
// file system object
SdFat sd;
// text file for logging
ofstream logfile;
// Serial print stream
ArduinoOutStream cout(Serial);
// buffer to format data - makes it eaiser to echo to Serial
char buf[80];
// constants won't change. Used here to
// set pin numbers:
const int ledPin = 7; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
//------------------------------------------------------------------------------
#if SENSOR_COUNT > 6
#error SENSOR_COUNT too large
#endif // SENSOR_COUNT
//------------------------------------------------------------------------------
// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
//------------------------------------------------------------------------------
void setup() {
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) sd.initErrorHalt();
// create a new file in root, the current working directory
char name[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
name[6] = i/10 + '0';
name[7] = i%10 + '0';
if (sd.exists(name)) continue;
logfile.open(name);
break;
}
if (!logfile.is_open()) error("file.open");
//format header in buffer
obufstream bout(buf, sizeof(buf));
bout << pstr("millis");
for (uint8_t i = 0; i < SENSOR_COUNT; i++) {
bout << pstr(",sens") << int(i);
}
logfile << buf << endl;
}
//------------------------------------------------------------------------------
void loop() {
uint32_t m;
// wait for time to be a multiple of interval
do {
m = millis();
} while (m % LOG_INTERVAL);
// use buffer stream to format line
obufstream bout(buf, sizeof(buf));
// start with time in millis
bout << m;
// read analog pins and format data
for (uint8_t ia = 0; ia < SENSOR_COUNT; ia++) {
#if ADC_DELAY
analogRead(ia);
delay(ADC_DELAY);
#endif // ADC_DELAY
bout << ',' << analogRead(ia);
}
bout << endl;
// log data and flush to SD
logfile << buf << flush;
// check for error
if (!logfile) error("write data failed");
// don't log two points in the same millis
if (m == millis()) delay(1);
// toggle LED State
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
Any help would be greatly appreciated!