Ok, so I've gone to this website and learned to avoid using txt/CSV files for data logging because it is significantly faster. But now I have a new issue. Essentially, what I am trying to do is put two variables in an SD card, but these variable are changing over time so in the "array terminology" I will have 1000 rows(this is enough sampling) and 2 columns(the two variables).
My Idea
I think I want to have an array of structs and be able to read each of them from the sd card. But why would I need to create 2D arrays if all the data is stored in the SD card anyway, right? When I'm done, I will take this data and make the Arduino print them out on an LCD display**
Both pieces of my code below are what I have so far. Please check both sketches.
This code counts up by 1 every second, starting at 1. This code works, but it doesn't log the data into the sd card. It should send the following to the serial monitor: "1 2 3 4 5..."
#include <SD.h>
#include <SPI.h>
const int chipSelect = 10;
File dataFile;
struct datastore {
uint8_t num;
};
void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
if (SD.exists("1.dat"))
SD.remove("1.dat");
dataFile = SD.open("1.dat", FILE_WRITE);
}
int i = 0;
void loop() {
struct datastore myData;
myData.num = 1 + i;
Serial.println(myData.num);
i++;
dataFile.write((const uint8_t *)&myData, sizeof(myData));
delay(1000);
}
This code prints out each collected number. Obviously, this code is a fail, but its like pseudo
#include <SD.h>
#include <SPI.h>
const int chipSelect = 10;
File dataFile;
struct datastore {
uint8_t num;
};
void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
dataFile = SD.open("1.dat", FILE_READ);
}
void loop() {
if (dataFile) {
struct datastore myData;
dataFile.read((uint8_t*)&myData, sizeof(myData));
Serial.println((int)myData.num);
delay(50);
}
else{
Serial.println("can't open");
}
}
The goal is to make the second code print out the numbers in the serial monitor: "1 2 3 4 5..."