Go Down

Topic: Read set up text file at compile (Read 7570 times) previous topic - next topic

Asa Herring

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?
I have an Arduino Mega 2560. I also have a (pH) BNC Sensor Shield,an I2C/SPI/Onewire Shield(I beleive that it an on board clock.)and a Breakout Board Shield, all three are from Andrew Oke. Additionally, I have a Stepping Motor but could certainly buy a Servo Motor instead if it is any better for what I want to do, short term/long term. Trying to read the lable, I guess that it is: Type 57BYG. 12 V/Phase, 20ohm, 0-6A/Phase 1.8 Deg/Step, No. 04052 with a K179 Stepper Driver board attached. I can't confirm that the board is fully funtional

liuzengqiang

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.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

Asa Herring

#2
Mar 29, 2011, 05:09 am Last Edit: Mar 29, 2011, 06:59 pm by Asa Herring Reason: 1
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?
I have an Arduino Mega 2560. I also have a (pH) BNC Sensor Shield,an I2C/SPI/Onewire Shield(I beleive that it an on board clock.)and a Breakout Board Shield, all three are from Andrew Oke. Additionally, I have a Stepping Motor but could certainly buy a Servo Motor instead if it is any better for what I want to do, short term/long term. Trying to read the lable, I guess that it is: Type 57BYG. 12 V/Phase, 20ohm, 0-6A/Phase 1.8 Deg/Step, No. 04052 with a K179 Stepper Driver board attached. I can't confirm that the board is fully funtional

liuzengqiang

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};

Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

Asa Herring

But how do I open the file? is the myVal.h the test file?
I have an Arduino Mega 2560. I also have a (pH) BNC Sensor Shield,an I2C/SPI/Onewire Shield(I beleive that it an on board clock.)and a Breakout Board Shield, all three are from Andrew Oke. Additionally, I have a Stepping Motor but could certainly buy a Servo Motor instead if it is any better for what I want to do, short term/long term. Trying to read the lable, I guess that it is: Type 57BYG. 12 V/Phase, 20ohm, 0-6A/Phase 1.8 Deg/Step, No. 04052 with a K179 Stepper Driver board attached. I can't confirm that the board is fully funtional

Asa Herring

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.
I have an Arduino Mega 2560. I also have a (pH) BNC Sensor Shield,an I2C/SPI/Onewire Shield(I beleive that it an on board clock.)and a Breakout Board Shield, all three are from Andrew Oke. Additionally, I have a Stepping Motor but could certainly buy a Servo Motor instead if it is any better for what I want to do, short term/long term. Trying to read the lable, I guess that it is: Type 57BYG. 12 V/Phase, 20ohm, 0-6A/Phase 1.8 Deg/Step, No. 04052 with a K179 Stepper Driver board attached. I can't confirm that the board is fully funtional

jraskell

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.

Asa Herring

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. 
I have an Arduino Mega 2560. I also have a (pH) BNC Sensor Shield,an I2C/SPI/Onewire Shield(I beleive that it an on board clock.)and a Breakout Board Shield, all three are from Andrew Oke. Additionally, I have a Stepping Motor but could certainly buy a Servo Motor instead if it is any better for what I want to do, short term/long term. Trying to read the lable, I guess that it is: Type 57BYG. 12 V/Phase, 20ohm, 0-6A/Phase 1.8 Deg/Step, No. 04052 with a K179 Stepper Driver board attached. I can't confirm that the board is fully funtional

Senso

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!

Asa Herring

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?
I have an Arduino Mega 2560. I also have a (pH) BNC Sensor Shield,an I2C/SPI/Onewire Shield(I beleive that it an on board clock.)and a Breakout Board Shield, all three are from Andrew Oke. Additionally, I have a Stepping Motor but could certainly buy a Servo Motor instead if it is any better for what I want to do, short term/long term. Trying to read the lable, I guess that it is: Type 57BYG. 12 V/Phase, 20ohm, 0-6A/Phase 1.8 Deg/Step, No. 04052 with a K179 Stepper Driver board attached. I can't confirm that the board is fully funtional

liuzengqiang

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?
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

Asa Herring

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.
I have an Arduino Mega 2560. I also have a (pH) BNC Sensor Shield,an I2C/SPI/Onewire Shield(I beleive that it an on board clock.)and a Breakout Board Shield, all three are from Andrew Oke. Additionally, I have a Stepping Motor but could certainly buy a Servo Motor instead if it is any better for what I want to do, short term/long term. Trying to read the lable, I guess that it is: Type 57BYG. 12 V/Phase, 20ohm, 0-6A/Phase 1.8 Deg/Step, No. 04052 with a K179 Stepper Driver board attached. I can't confirm that the board is fully funtional

Senso

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.

Asa Herring

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.
I have an Arduino Mega 2560. I also have a (pH) BNC Sensor Shield,an I2C/SPI/Onewire Shield(I beleive that it an on board clock.)and a Breakout Board Shield, all three are from Andrew Oke. Additionally, I have a Stepping Motor but could certainly buy a Servo Motor instead if it is any better for what I want to do, short term/long term. Trying to read the lable, I guess that it is: Type 57BYG. 12 V/Phase, 20ohm, 0-6A/Phase 1.8 Deg/Step, No. 04052 with a K179 Stepper Driver board attached. I can't confirm that the board is fully funtional

AWOL

"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up