Arduino Forum

Using Arduino => Programming Questions => Topic started by: Asa Herring on Mar 29, 2011, 04:46 am

Title: Read set up text file at compile
Post by: Asa Herring on Mar 29, 2011, 04:46 am
How do I read a file into my program at compellation not and SD or some serial connection? I want to read an external text file into the program, either into several variables or preferably an array of them. That way I can change the results that get uploaded without messing around with my main program.

For example, I want to set the motor steps to the next station, time to run food motor and time to run water motor for 6 stations. To learn, I wrote this file (settings.txt) with unique numbers that would let me know what is going where.

11,12,13
21,22,23
31,32,33
41,42,43
51,52,53
61,62,63

I would like to read this file and load the numbers in …say   int d1, f1, w1, carriage return or blank d2, f2…etc.

It would be nice to read to a file (results.txt) to use as a debug tool. How would I do this?
Title: Re: Read set up text file at compile
Post by: liuzengqiang on Mar 29, 2011, 04:59 am
Sounds like the job of an array instead of individual element. You can use #include to include a file into your code. In the included file, define your variables like d[]={12,23,34,} etc.
Title: Re: Read set up text file at compile
Post by: Asa Herring on Mar 29, 2011, 05:09 am
What code would I use?


#include c:/arduino/setting.txt            // It finds the file

???fopen(settings.txt)???

Is there a place to look for an example?
Title: Re: Read set up text file at compile
Post by: liuzengqiang on Mar 29, 2011, 05:58 am
It would be wise to use arrays:

Code: [Select]
myVals.h
#define d_val 11, 21, 31, 41, 51, 61
#define f_val 12, 22, 32, 42, 52, 62
#define w_val 13, 23, 33, 43, 53, 63

Main:
#include "myVals.h"

int d[6]={d_val};
int f[6]={f_val};
int w[6]={w_val};

Title: Re: Read set up text file at compile
Post by: Asa Herring on Mar 29, 2011, 06:12 am
But how do I open the file? is the myVal.h the test file?
Title: Re: Read set up text file at compile
Post by: Asa Herring on Mar 29, 2011, 06:38 pm
I have spent many many hours on the net and haven't seen any hard code that will read a text file to populate variables in my arduino program. I was encouraged by the help directed toward #include and the array so I searched even more but found no real code to put it into action. (Many thanks for the array which I will edit and use.)  I have googled it in many different ways but when you put in "arduino read text file", all I get is how to "file write" or SD serial stuff.

In C++ and almost all other languages, there is a file read of some sort. It seems that with an arduino, you can read from an SD card after uploading but not a file before. Perhaps I am mistaken. This is a micro board, not a mini-computer. I guess that since the arduino isn't built for stand alone I/O there isn't any code for files like that ...or maybe no one cares about doing what I am trying to do and hasn't written any code to do it yet. I'm baffled.

If there isn't a tangable path to do what I am trying to do, I hope that someone will put me out of my misery.

In any case, thanks for the help.
Title: Re: Read set up text file at compile
Post by: jraskell on Mar 29, 2011, 07:09 pm
Arduino does provide an SD library to access files on an SD card.  This includes reading from, writing to, creating, deleting, etc.  It can be found here:

http://arduino.cc/en/Reference/SD

You would use this class to read the file data in during setup() and use the data read in as you wish.  This is not a compilation activity, but a setup/boot activity.  The Arduino itself does not compile anything.  Compliation is performed by the IDE, and it turns your C/C++ into a machine readable binary that gets downloaded and executed by the microcontroller.  It's your program that needs to be written in a way that reads in data from the SD card and uses it in some manner.
Title: Re: Read set up text file at compile
Post by: Asa Herring on Mar 29, 2011, 07:33 pm
That wouldn't be so bad but you are saying that I have to buy an SD card? You can't read from the computer disk where I am write and compiling the code?

The reason that I would want to do all of this is so that I could use VB or C# to make an interface app that would allow me to inter data in a safe and more user friendly manner. It would simply write the text file, then I would recompile that arduino program and upload it. 
Title: Re: Read set up text file at compile
Post by: Senso on Mar 29, 2011, 08:00 pm
When you include a file using #include, the compiler will "merge" that file with your source code, but remember that .h file must only contain C code, so just put your array as liudr said, compile it and test it!
Title: Re: Read set up text file at compile
Post by: Asa Herring on Mar 29, 2011, 08:12 pm
I'm an absolute dummy about this stuff.
So you're saying that I should drop the "read text file" for now (can't get there from here) and just manually edit the array?
Title: Re: Read set up text file at compile
Post by: liuzengqiang on Mar 29, 2011, 08:16 pm
I was away a bit so now I'm back.

In one file myStuff.h you write all your numbers. It's a text file. Use any editor that saves text files. It won't be purely numbers but has to have the #define d_val and such but this should be easy to adapt. Then the #include command in your arduino file will suck the myStuff.h into itself so at compile time all your variables are defined per your separate myStuff.h file. You can change its content outside arduino IDE. I've tested it to work just fine. Any more questions?
Title: Re: Read set up text file at compile
Post by: Asa Herring on Mar 29, 2011, 08:24 pm
In theory, this is a great path to get were I want to go. This will take me a couple of days to get it integrated into my program. I'm a dummy so I can't say that I will not have any more question but I am greatly appreciative for spoon feeding this newbie.
Title: Re: Read set up text file at compile
Post by: Senso on Mar 29, 2011, 08:44 pm
Days?
Remenber that Arduino only has 2048 bytes of ram, without including all the needed space for the stack and heap, thats an array of 2048 chars or 1024 ints, or 256 floats.
Title: Re: Read set up text file at compile
Post by: Asa Herring on Mar 29, 2011, 08:57 pm
Yes Sad to say that I am about to leave town to go to a viewing. as excited as I am, I won't be able to get to this until late tonight. Then I will have work out the syntax for my application with more research and brute force.
Title: Re: Read set up text file at compile
Post by: AWOL on Mar 29, 2011, 08:58 pm
Quote
or 256 floats.

sp. "512"
Title: Re: Read set up text file at compile
Post by: maniacbug on Mar 29, 2011, 09:16 pm
Also, you can use xxd for this if you have binary data you want to bring in.

Code: [Select]

$ echo Hello > hello.txt
$ xxd -i hello.txt > hello.h
$ cat hello.h
unsigned char hello_txt[] = {
  0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x0a
};
unsigned int hello_txt_len = 6;
Title: Re: Read set up text file at compile
Post by: Daneel on Mar 30, 2011, 09:38 am
I've got a workaround which I use which I could happily modify for you if you like:

I've made a batch file which copies the sketch and then searches through the sketch for placeholder values then replaces them with the values I want from a second file.  It'd be easy to modify it to work for what you'd like to do.

All you need to do is replace the values in your code with a placeholder, so for instance, if you had 11,12,13 before, now you put REPLACEME1,REPLACEME2,REPLACEME3

Then in the script you just make it search for those placeholders and replace them with 11,12,13 which would be stored in your 'config' file.  That way you end up with a new sketch file (or folder if you'd like that - I can make it do that) which had the test values in it.  Then you can keep it for the future too if you want to reuse those values.

If that sounds like what you're after then I'd be happy to rewrite it for you :)  let me know :)


As an aside - this sort of question is asked regularly..  I might try and generalise the rewrite so almost anyone can use it.
Title: Re: Read set up text file at compile
Post by: Asa Herring on Mar 30, 2011, 10:18 am
I have thought about doing that using VB because I use to play with text like that a lot. Thanks for the offer. It is cool that I can get help in North Carolina from some one half way around the world.

If I am away from the arduino environment, I can hold my own as a hobbyist but I am dyslexic and didn't have enough back ground reading in the learning area to catch what was being said.  Luckily, I just got the code from liudr to work. I couldn't understand what he was going for at first but as I read more and he answered my newbie questions a light went off in my head and I started banging away at the key board.

It does exactly what I want it to do now. I can edit that text file and not take a chance of messing up good working code. There will be times when I will have to change the locations or the stations and tune the amounts of liquid. During these times, I will be constantly updating the file, either manually or with another quickie application. I sure don't want to dive down in the code six months from now and have to re-teach myself what I did before making a change. Of course the arrays do simplify the code quite a bit. I just discovered the serial window and it is great to see it jump through that stations, updating the distances and times as it goes. Automation is a wonderful thing.

This phase is fully complete. Thanks for everyone's help!
Title: Re: Read set up text file at compile
Post by: liuzengqiang on Mar 30, 2011, 04:25 pm
Very good! I myself learned something while helping you: I didn't know it would work?!

I then thought about it and tested it out on my arduino and it did! Good news for both of us. I'll be blogging this trick sometime today!

That's what a teacher does, learn himself and teach it like he knows it for decades  XD

Asa, you must be a good learner. Typical learners get easily discouraged and start blaming professors for giving them hard problems to solve. You went out yourself and look for solutions.

Any chance you attend the university named after your location?
Title: Re: Read set up text file at compile
Post by: Asa Herring on Mar 30, 2011, 06:18 pm
I went to Arizona State in the 70's…in sociology, which prepares you for everything and nothing. Now I and a commercial aerospace design engineer in Winston-Salem. Go figure. My daughter attends there studying Psychology but I heard that they have a good computer program.

You came up with a great answer very quickly. Honestly, I didn't do the requisite reading to implement your solution at first. I'm still not going to read all that I should because of the dyslexia so I will continue to be a pain when I get to other heralds in my project.

I have a few patents so I always come up with easy ideas that aren't that easy to do. One part of making them come to fruition is, not letting my ignorance get in my way.

After I didn't get it, I started googling this and found that know one was doing set up files, I really thought that I had bitten off more that I could chew.

Thanks for pulling me out of the fire.
Title: Re: Read set up text file at compile
Post by: liuzengqiang on Mar 30, 2011, 06:41 pm
I see. I remember there was a teaching position opening some years back at that university which I could have applied for but I can't say for sure :)

Since the text-based website has no information about your age, I assumed you were a college student. Well, you were :) My sociology colleagues are pretty good at pulling apart fabrics of lies woven around us :) I enjoy talking to them over lunch.

The code I gave you simply serves as replacement inside the compiler.

If you do:
#define Fubar 9

Then:
Every time the compiler sees Fubar, it replaces it entirely with 9, so if I have this in the latter part of the program:

if(damage==fubar) reparable=FALSE;

Then the compiler sees

if (damage==9) reparable=FALSE;

This way if later you decide damage level 7 is fubar, you don't have to change all the fubar in your program from 9 to 7, but just change the #define. It's the same principle of algebra, using symbols instead of numerical literals in various situations.

The #define is a pre-processor directive.



Title: Re: Read set up text file at compile
Post by: Asa Herring on Mar 30, 2011, 08:36 pm
Cool,

I guess that I could use this even if I didn't plan for it in advance. So I guess that I had better change my one digit character variables to two, now that I have them working. That way I can find them later.

int d[6]={d_val};                    int distance[6]={d_val};
int f[6]={f_val};         To         int ftime[6]={f_val};
int w[6]={w_val};                   int wtime[6]={w_val};

I'm sure that I will be asking more weird questions in different threads because now I'm a super programmer, on a roll! ...at lease in my own mind. :D

There are several things that I will need to do to actually get this feeder running well. I just need to understand the difference between necessary and cool and keep my eye on the ball.

Thanks again,
Asa (?sâ;) Herring