read sd card with indexing data.

hello,

I'm here to ask for help because now I'm desperate !

I made a project with arduino but I realized that the EEPROM memory is not sufficient to hold the data that I have read.
Up to this point I have arranged, the program is simple , I read one time from an RTC , comparing it with a data table and I turn on or off various loads .
Now I would like to use an SD card (which I have already connected and that works ) .
the card contains a. txt file with a table like this:

/ / Day , start, stop ( this line in the file does not exist)
1,11300,18023
2,12121,13131
3,14345,13417
4,14345,14456
5,14761,12984
6 ....

up to 365 ( days a year )

I would like to do one simple thing:

on arduino I have a float variable that contains the number of the day ( day) , reading from the card I would like to load in two other variables float (start and stop ) the values ??corresponding to the row where the first number indicates the current day .
eg . today "day" = 2 then "start" = 12121 and "stop" = 13131

Unfortunately I'm too ignorant about programming to understand how to use the library or SdFat Sd .

Some kind soul can help me? thanks

on arduino I have a float variable that contains the number of the day ( day)

So, you want to find the record number 134.7899? Why are you using a float to store an int?

reading from the card I would like to load in two other variables float (start and stop ) the values ??corresponding to the row where the first number indicates the current day .

The other two values are not floats, either.

Unfortunately I'm too ignorant about programming to understand how to use the library or SdFat Sd .

Get over it, or offer some money.

Hi

Using direct file IO with "records" of identical length you can read any part of a file. In the following procedure I am reading from a text file that contains 121 records, each 30 bytes long - the file is exactly 3,630 bytes. You will note from the first five record example that I have padded each record with asterisks - it happens that the longest procedure name in the file is 29 bytes so using asterisks the records are always delimited and I can use the p_checkram_file.readStringUntil('*') statement to read any record given a calculation of the correct starting (seek) position.

Below the procedure I have shown the code fragments needed to open and close the text file that contains my 121 fixed lentgth records.

By padding your records to the same length with a special character and then parsing the record to extract each field you should be able to solve your problem.

Catweazle NZ

String LoadProcedureName(File &p_checkram_file, int p_proc_num) {				
/* This is the first five record (0-4) of the file
SDDisplay*******************
SDFileDisplay2**************
SDFileDisplay3**************
SDFileDisplay4**************
FireProcess*****************
*/
  int l_current_SPI_device = CurrentSPIDevice();
  //Determine file length
  SPIDeviceSelect(DC_SDCardSSPin);
  unsigned long l_filesize = p_checkram_file.size();
  unsigned long l_posn = (p_proc_num - 1) * 30; //30 is the record length
  String l_result = "";
  if (l_posn >= l_filesize) {
     l_result = String(p_proc_num + ' ' + EPSR(E_File_Record_Length_Error_2769));
  }
  else {
    p_checkram_file.seek(l_posn);
    l_result = p_checkram_file.readStringUntil('*');		
  }
  SPIDeviceSelect(l_current_SPI_device);
  return l_result;
}


//OTHER CODE FRAGMENTS
//Here is the opening of the direct access file
  File l_check_ram_file;
  boolean l_checkram_file_OK = true;
  if (!SD.exists(&l_filename[0]))
    l_checkram_file_OK = false;
  else {
    l_check_ram_file = SD.open(&l_filename[0],FILE_READ);			
    if (!l_check_ram_file) 
      l_checkram_file_OK = false;
    //
  }


//Do not forget to close the text file
  if (l_checkram_file_OK == true) {
    SPIDeviceSelect(DC_SDCardSSPin);
    l_check_ram_file.close();

thanks, I solved starting from the examples of the library, now I just have to able to put a variable in place of "tab1.csv," how can I do? I've tried using a string variable but it does not work :slight_smile:

void Read_SD_card1()
{
char filename[]= "tab1.csv";
delay(200);
char c1, c2, c3, c4, c5;

// open input file
ifstream sdin(filename);

// check for open error
if (!sdin.is_open()) error("open");

// read until DayGiulianRead < DayGiulian
while ((sdin >> DayGiulianRead >> c1 >> AlbaOggiOre >> c2 >> AlbaoggiMinuti >> c3 >> TramontoOggiOre >> c4 >> TramontoOggiMinuti >> c5 >> DurataFadeMin ) && (DayGiulianRead < DayGiulian))
{
if (c1 != ';' || c2 != ';' || c3 != ';'|| c4 != ';'|| c5 != ';') error("comma"); // error in line if not commas
}
delay(10);
}

I've tried using a string variable but it does not work

You have some code that implements this that you didn't show. The code does something that you didn't describe. You want it to do something, presumably not the same as what it actually does. And, we're supposed to guess what the problem is?

ok , the code reads from a flie . csv whose structure is as follows:
1 ;7, 44;11 ;30 ; 10
2; 7;45 ; 15;31; 10
3 ; 7; 46 ;16 ; 33; 11
4 ; 7, 45 ;17 ; 34; 10
5 ;7;44 ; 18;36;10

......
up to 266 ( days in a leap year ), the first field is the day of the year which is also in variable ' DayGiulian ' , the file is read until the day of the table ( ' DayGiulianRead ' first field ) is less than the current day , at this point the reading is halted and in the variables :
' AlbaOggiOreRead_SD ' ,
' AlbaoggiMinutiRead_SD '
TramontoOggiOreRead_SD '
' TramontoOggiMinutiRead_SD '
' DurataFadeMinRead_SD '

I get the correct values.

eg . if the day of the year is = 3

I have the following situation:

' AlbaOggiOreRead_SD ' = 7
' AlbaoggiMinutiRead_SD ' = 46
TramontoOggiOreRead_SD ' = 16
' TramontoOggiMinutiRead_SD ' = 33
' DurataFadeMinRead_SD ' = 11

and since here everything is ok , the code does what I need.

The problem that I have yet to solve is the name of the file to read , I tried to replace :

char filename [ ] = " tab3.csv " ;

with :

char realfilename = " tab3.csv "

then :

char filename [ ] = realfilename ;

but it does not work . How can I make allows choosing the file name through a variable?

ps. Sorry for my english, I'm italian :wink:

up to 266 ( days in a leap year ),

I wonder why my years feel so much longer. 8)

char realfilename = " tab3.csv "

You can't store a string in a char variable. A char variable holds one character.

How can I make allows choosing the file name through a variable?

char filename [ ] = " tab3.csv " ;

filename is a variable (of type array of char).

Beyond that, I don't understand your question/problem.

I would like to use a variable instead of a fixed filename, so I'll can choose different files.

solved using: string.toCharArray()