So I've been working on a project with a few guys and I need to create a voltage data logger that will store 1 data point (minimum 8 bits) per millisecond (I know it's a bit fast). The system needs to create a new file and start logging every time i press a button and stop once pressed again. I have been messing with Arduino IDE and some circuits for a while now but I have little programming experience. I assume I'll need to buffer some data and send blocks of data to the SD card to maintain a minimum 1 kHz log rate. Will someone please help me with some code for this?
I am using the Feather M0 Adalogger, 16GB fat32 micro SDHC card, and a spring loaded button; default off position. (I will most likely use the INA219 Current Sensor Breakout but I need the code done by the time I get a sensor )
I am now using a temporary current sensor for the purposes of testing
After a couple days of research this is as far as I've got with my code.
#include <SPI.h>
#include <SD.h>
#define chipSelect 4
File logfile;
void setup() {
while (!Serial) {}
Serial.begin(9600);
Serial.println("Press button to start logging");
if (!SD.begin(chipSelect)) {
Serial.println("Card init. failed!");
}
}
uint8_t state = 0;
uint8_t i = 0;
uint8_t ButtonRead;
void loop() {
if (digitalRead(13) == HIGH) {
ButtonRead = HIGH;
}
else {
ButtonRead = LOW;
}
if (ButtonRead == HIGH) {
if (state == 0)
{
char DataFile[9];
strcpy(DataFile, "file00.TXT");
for (uint8_t i = 0; i < 100; i++) {
DataFile[4] = '0' + i / 10;
DataFile[5] = '0' + i % 10;
if (! SD.exists(DataFile)) {
break;
}
}
logfile = SD.open(DataFile, FILE_WRITE);
if (!logfile) {
Serial.println("Could not create file");
logfile.close();
ButtonRead = LOW;
delay(1500);
return;
}
else {
state = 255;
Serial.print("Created ");
Serial.println(DataFile);
}
}
else
{
state = 0;
logfile.close();
ButtonRead = LOW;
delay(1500);
return;
}
}
if (state == 255)
{
logfile.print(analogRead(A1));
logfile.print(" , ");
logfile.println(millis());
Serial.print(analogRead(A1));
Serial.print(" , ");
Serial.println(millis());
}
}
Thanks for the help.