Read only a .csv at specific lines (sunrise / sunset tide display)

Hello everyone,

I am making a tide & sunrise/sunset display screen and I am having trouble working out how to read from a .csv file / specific line.

I have the following example data (which is in an excel sheet and can be manipulated to facilitate this code):

day, highwater, lowater, sunrise, sunset,

1, 0750, 1130, 0600, 1700, 2, 0830, 1210, 0610, 1710, 3, 0910, 1245, 0612, 1715, 4, 0630, 1920, 0530, 1940,

I am struggling to work out how to include this in my Arduino MEGA 2560 program. I have a clock that states the days and I would like the arduino when, for example, it is day 3 to look up the third row and take the comma separated values split them into 4 values which can then be referred to in the main code to display them to the LCD. I should be ok phrasing the array once the correct line has been selected but unsure how to just go to a specific line..if this is even possible.

Any help or thoughts will be much appreciated. Thanks!

I have the following example data (which is in an excel sheet and can be manipulated to facilitate this code):

A csv file and an excel sheet are two completely different things. You haven't a hope in hell of reading an Excel sheet on the Arduino. You COULD read the csv file.

I am struggling to work out how to include this in my Arduino MEGA 2560 program.

What is "this"? Including data doesn't make sense. Including code to read from a file is a different story.

The basic steps are to initialize the SD card reader, open a file, read some data, storing it in an array, re-initializing the index when appropriate (the end of the line is encountered and it isn't the line of interest), stopping when the end of the file is encountered, and closing the file.

Which step(s) are you having problems with? Have you looked at any of the examples in the SD library?

It's a .csv file (I am aware you can't read an excel file on arduino, but was referencing the fact that I have it in excel so I can manipulate it should it need to be a different format or separated by a different means).

What I would like to know, and apologies if i was not clear in asking, is: How do you 'goto' a specific line in a .csv file (saved onto an SD card) and output only that line of data to several integers?

As in my example: day, highwater, lowater, sunrise, sunset,

1, 0750, 1130, 0600, 1700, 2, 0830, 1210, 0610, 1710, 3, 0910, 1245, 0612, 1715, 4, 0630, 1920, 0530, 1940,

Outputting for example just line 3 with new assigned values as: value1= 0910 value2= 1245 etc.

Thanks again

pejay28: It's a .csv file (I am aware you can't read an excel file on arduino, but was referencing the fact that I have it in excel so I can manipulate it should it need to be a different format or separated by a different means).

What I would like to know, and apologies if i was not clear in asking, is: How do you 'goto' a specific line in a .csv file (saved onto an SD card) and output only that line of data to several integers?

As in my example: day, highwater, lowater, sunrise, sunset,

1, 0750, 1130, 0600, 1700, 2, 0830, 1210, 0610, 1710, 3, 0910, 1245, 0612, 1715, 4, 0630, 1920, 0530, 1940,

Outputting for example just line 3 with new assigned values as: value1= 0910 value2= 1245 etc.

Thanks again

You will have to write routines that:

  • Scan the data file looking for a specific day Read characters from the file, Parsing them into integers, Using the commas as separaters.
  • If the parsed 'day' is not the day you want, Then reading forward looking for the newline character. Then jump back into the 'day' scan routine.

Chuck

How do you 'goto' a specific line in a .csv file

You can only jump to the start of a specific record if all the records are the same length.

While that is true of the portion of the file you posted, what happens after day 9?

If the records are not all the same length, you have to read until you find the end of a record, and then increment the record count. If the record count matches the one you are looking for, then use the data collected from the file.

Otherwise, reset the array to have a NULL in position 0, and set the index back to 0 and keep reading.

Thanks guys for the replies - I will work on some code in relation to the feedback and report back here with a solution..hopefully! Cheers