Ok so I have a specific question. I am making measuring system that is based on Uno microcontroller. I have code splitted in to parts in separate .cpp files so is more readable and easier to organize. The thing that is bugging me at the momment is how to read data from SD card where I have stored some parameters that are needed to bring the sistem to life in a case of power shortage so in other words I have parameters if arduino restarts.
I am flexible in the way how the data is storred in the LogFile on SD card as it will be generated on the first start of the system and later on will be used just as a backup. I am aware of the EEPROM option but I would rather have the backup on the SD as it is planned to be used later on for other things.
Currently I have data storred on the SD card in a form like:
1, 10
so it is like CSV. The other wat that I tought of saving parameter values was like:
x = 1
y = 10
I do not know which way is better but probably the first one. I will post the current code bellow.
This is how my .cpp looks like
// libraries
#include <Arduino.h>
#include "FileManagement.h"
#include <SPI.h>
#include <SD.h>
FileManagement::FileManagement(String file_name){
_file_name = file_name;
}
int *FileManagement::read(){
File myFile;
while (!Serial) {
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("Initialization failed!");
while (1);
}
Serial.println("Initialization done.");
myFile = SD.open(_file_name);
if (myFile)
{
Serial.println("Reading from ");
Serial.println(_file_name);
while (myFile.available())
{
char inChar = myFile.read(); //get a character
if (inChar == '\n') //if it is a newline
{
parseRecord(recordNum);
recordNum++;
charNum = 0; //start again at the beginning of the array record
}
else
{
aRecord[charNum] = inChar; //add character to record
charNum++; //increment character index
aRecord[charNum] = '\0'; //terminate the record
}
}
myFile.close();
Serial.println("Done.");
}
else
{
Serial.println("Error opening!");
Serial.print(_file_name);
}
int cycles = String(parameterArray[0]).toInt();
int baseline = String(parameterArray[1]).toInt();
static int return_data[2];
return_data[0] = cycles;
return_data[1] = baseline;
return return_data;
}
void FileManagement::parseRecord(byte index)
{
char * ptr;
ptr = strtok(aRecord, " = "); //find the " = "
ptr = strtok(NULL, ""); //get remainder of text
strcpy(parameterArray[index], ptr + 2); //skip 2 characters and copy to array
}
And this is what I have in the setup()
//LogFile
SD.begin();
if (SD.exists(logfile_path))
{
int* logdata = LogFile.read();
total_cycles = String(logdata[0]).toInt();
force_baseline_value = String(logdata[1]).toInt();
}else
{
//First time creating the file
LogFile.write(0, 0);
}
The problem is probably in pointers as I can not change the value of the variable in the main after assigning the value to the variable from the file. Can someone please help me how to create separate file with class methods to perform reading file data.
Thanks in advance.