Go Down

Topic: Reading a specific line of text from a txt file (Read 256 times) previous topic - next topic

DanSPE

Hello, I am new to Arduino with little programming experience (MATLAB and excel proficient with little Arduino). I am working on a project that has stored lines of text that are ordered in a text file that I would like to look up based on a lookup value.

I've searched vigorously but can't seem to find what I'm looking for/ don't really understand what I'm reading(as far as the codes are concerned). It doesn't seem as straightforward as one may think(or maybe I'm over complicating it).

My goal:
Read the date from an RTC and that date will be the lookup value. I have for example: September 12 as a value of 0912 and so on for each month and day. I would like to use this value to search in the text file. The text file would be formatted in rows as follows:
0912Hello World
0913It is tomorrow

From here, I want to display the line on an LCD screen(I understand this part).

So my question is, 1. Can someone explain how the values of an RTC can be obtained and used as a lookup value? And 2, how do you search a text file based on a specific lookup value and read that line (minus the lookup value).

If anyone could elaborate on this or know of better references, it would be greatly appreciated.

Thanks!

ieee488


Read a line at time from the text file into a char array #1.

The lookup value is a char array #2 with 5 elements including the NULL at the end.


Use strstr() to see if char array #2 occurs in char array #1.
http://www.cplusplus.com/reference/cstring/strstr/

Keep reading from the text file until match is found or end of file is reached.



DanSPE

It would be worth mentioning (should have in the original post) that this device will be triggered by a push button and will be battery powered. I have not tried the above method but I'm beginning to think about how much power this process could consume. The dates will all be in consecutive order like the following:

0912Hello world
0913It is tomorrow
0914Another phrase
.
.
.

The intent is to have 365 things (1 for each day) and possibly more (cycle larger than 1 year). My concern is, if it has to search all the way down to the 365th lookup value, will this be power/time consuming?

Perhaps it could be done with 2 seperate files? One, say file A that has the same lines as mentioned above but another say file B that stores what has already been used. Essentially it would see if the lookup value matches that of file A (it would always be the first or second line) and if it is, display it. If it's not, would be one day off, it will take the line before and move it to file B. This way it doesn't have to search all the way down hundreds of lines.

Does that method make sense?

Lucario448

More files implies more overhead. What I would do is to store the file cursor index (aka the value returned by position()) every time I advance one line.
This way, when you have to reopen, you don't have to start again from the beginning; but from the position previously saved.


About energy saving... it would be better if you power the Arduino directly with 3.3v. The lower amount of voltage regulators, the better.

If you get rid of all regulators and built-in LEDs, the Arduino in the deepest sleep mode available, will actually consume currents in the microamps scale :o

The SD card... basically the same thing: one second after the last interaction and it goes to sleep as well (also it's the only moment you can remove it or "hot-unplug" without causing any damage).

How much it takes when reading/writing? The Arduino microcontroller alone with the oscillator spooled up, I think like 12 mA; same with the SD card is active.
When both are asleep, maybe like a 1/8 of a mA.

I might be wrong with those values, but otherwise they are pretty close.

Go Up