I believe the issue is simply a slow reading of the analog values at a higher frequency as my serial monitor outputs are approximately the same as rates when the data is logged to an SD card. The SD card logging does not seem to be the bottleneck at the moment, but that is not to say it won't be later. I'm using an RTC for stamping the data with the date, but I omit that from the loop for the sake of streamlining the code. As for the sensor, it is a simple opamp for a strain gauge. Not much info on it. The sampling rate is the same regardless of whether I'm using the analog input from this strain gauge or my airspeed sensor, for example.
Here is the code that I am using:
//SD Card Setup
#include <SD.h>
const int CS_PIN =10;
const int POW_PIN =8;
//Strain Gauge Setup
int analogPin = 3; // connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
//RTC setup
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.println("Initializing Card");
pinMode(CS_PIN, OUTPUT);
pinMode(POW_PIN, OUTPUT);
digitalWrite(POW_PIN, HIGH);
if (!SD.begin(CS_PIN))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(230400);
delay(3000);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(DATE), F(TIME)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
//RTC sample bits
DateTime now = rtc.now();
File dataFile = SD.open("Strain1.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(now.year(), DEC);
dataFile.print(", ");
dataFile.print(now.month(), DEC);
dataFile.print(", ");
dataFile.print(now.day(), DEC);
dataFile.print(", ");
dataFile.print(now.hour(), DEC);
dataFile.print(", ");
dataFile.print(now.minute(), DEC);
dataFile.print(", ");
dataFile.println(now.second(), DEC);
dataFile.close();
}
}
void loop () {
File dataFile = SD.open("Strain1.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(analogRead(analogPin));
dataFile.print(", ");
dataFile.println(millis());
dataFile.close();
}
}
I've tried using arrays as well to write the data to the SD card in larger packets, but I did not have success with that. That is the most accessible way that I can think of to overcome this issue, however, at this stage, my ability to write code is entirely based off stitching together from other bits of code available and deleting sections here and there. It has served me well for the time being, but it really limits my creativity and resources to tackle this alone. Thank you for your help, and let me know if there is any more specifics that I can provide you with to help me with this matter. Thanks, again.