Go Down

Topic: reading variable date from sdcard (Read 8849 times) previous topic - next topic

redtank

Hello,
4 months ago I finished a project that uses a Gator+ board that controls a unique piece of manufacturing equipment that I designed and built for my employer. Basically, the equipment feeds flat wire into a cut off station and cuts the wire to 6 different lengths. I'm using a stepper motor to pull the wire into the machine and a solenoid to power the cut off blade. Every thing works well, but there needs to be some tweaking to fine tune the project. Up to this time, I've been altering the program: modify some of the variables, value wise to get the desired results. As an example, the solenoid that pulls the cut off blade is controlled by a time variable for how long it is energized. If I want to change that time amount, I go into my program and either decrease or increase the value, but this requires me to bring my laptop to work and go through the uploading process. I believe there is an easier way that will not involve having to reprogram.   What I would like to do is store all my variable data on an sdcard in a txt file and when the program is run, open the file on card and us the provided values for each variable in the program. If this is possible, then I can remove the sdcard and modify the txt file off line and never have to reprogram the Gator+. I was looking the a small sdcard reader designed for Arduinos, but plugs into a breadboard and you can then wire it to Arduino pins. Looks simple, costs $15 and could save me a lot of headaches....

Is it possible to read data from a sd card and get the desired results I'm looking for, and if yes, please point me towards an example program and I'll move forward with this design change.

Thank you in advance!

Cris

PaulS

Quote
Is it possible to read data from a sd card and get the desired results I'm looking for, and if yes, please point me towards an example program and I'll move forward with this design change.

Sure it is. Look at the SD library. Examples included.
The art of getting good answers lies in asking good questions.

redtank

Okay, I received my sd card reader and am able to write/read to the card. I've got a file called "B.txt" with the integer "5" in it and nothing else. What I'm trying to do is open the sd card, read the file, assign the "5" in the file to a variable "x". Then use that variable to flash a led. I simplfied the attached code by removing all error checking to make it easier to read, either way, the end result is the same. I've tried using different values in the file, if it is a single digit 0 thru 9, the result is -1 so the led does not flash. If the value is 10 thru 99 I get a 48, so the led flashes 48 times. Larger values return unpredictable values, the largest I've encountered is 111. I've tried to find some correlation between the value in the file and the end result, but there doesn't seem to be one. Any suggestions?

Thanks,
Cris


Code: [Select]
#include <SD.h>
File myFile;
int x;

void setup() {               
  Serial.begin(9600);
  pinMode(10, OUTPUT);
  pinMode(7, OUTPUT);
  SD.begin(10);
  myFile = SD.open("B.txt");
  Serial.write(myFile.read());
  x = (myFile.read());
  Serial.println( );
  Serial.println(x);
  myFile.close();
  }

void loop() {
  while (x > 0) {
  digitalWrite(7, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(7, LOW);    // set the LED off
  delay(1000);              // wait for a second
  x--;
  }
}


 

PaulS

Quote
I've got a file called "B.txt" with the integer "5" in it and nothing else.

Then, why are you reading two of the one characters?

Code: [Select]
Serial.write(myFile.read());
  x = (myFile.read());

Why are there parentheses around the second call to read()?

Quote
If the value is 10 thru 99 I get a 48

The value in the file is not 5. It is '5'. The value is not 10, it is "10". When you print the first character, the second character is stored in x (not the second number). The second character, when the value is "10" is '0'. The character '0' has the ASCII value 48.

Quote
Any suggestions?

Sure. Learn how to read data from the file correctly. Look at the examples again.
The art of getting good answers lies in asking good questions.

darkriftx

Redtank,
It seems all that PaulS wants people to do is examine poorly coded examples ( no offense PaulS ).  If you know a particular piece of a file is going to be a number, then you need someway to turn the ASCII value into a number.  Here is a bit of c pseudocode that should tell you the corresponding integer value of a character based number:

int x;
int y = -1;

x =  myfile.read();
if ( x >= 48 && x <= 57 ) y = x - 48;



Now y contains the number that you were reading in.  Please note that if your line of text in the file has multiple numbers and you need the composite numeric value, read in and convert the numbers until the read() returns a zero, which is the null terminator / end of the line. 

I hope this helps.

-- X

redtank

PaulS,
What was the point in your last reply? What did you accomplish by telling me to go back and re-evaluate the piss poor examples listed on the net? Did your posting help me solve my problem, or was it meant to show your superiority to the hobby and what a special God given person you are to mankind? You've obviously spent a great deal of time and energy posting to this forum, lets see, you are up to 16,890 posts? or did I miss a few more? How many people have you pissed off with your "cut above the rest" mentality? Here is my deal: I'm tied into using this technology for better or worse, my job depends on it, and as everyone can plainly see I'm not a coder. I'm good with the hardware end of things, but coding is my weak spot and I don't have much time to reinvent the wheel, rather look for some help after spending a lot of time trying to figure things out for myself. The absolute last thing I wanted to do is post a question to this forum because of people like yourself who lurk in these places to spread your poison. If I were you, I'd find another hobby where you can't mess with others. I'd rather sell all my Arduino equipment and find another SBC to work with so I don't have to come back to this forum ever again.

Thanks for nothing!
Cris

PaulS

Quote
What did you accomplish by telling me to go back and re-evaluate the piss poor examples listed on the net?

I don't recall telling you to look at piss-poor examples.

Reading from a file is a basic C exercise. Look in your C book to see how to read data from a file. Don't have one? GET ONE!

Quote
How many people have you pissed off with your "cut above the rest" mentality?

Not all that many, though I keep trying.

I think that it is pretty obvious that reading and printing a character from a file is a useless thing to do if that character is supposed to then be used to do something. If you don't see that as obvious, I can't help you. I'll add you to my list of people to ignore.

Quote
I'm tied into using this technology for better or worse, my job depends on it, and as everyone can plainly see I'm not a coder.

Then, you need to learn. I can't imagine why you accepted a job that is beyond your capabilities. Was your employer aware that you couldn't write code when you were hired?

Quote
I'd rather sell all my Arduino equipment and find another SBC to work with so I don't have to come back to this forum ever again.

If this is going to be your attitude, please do.
The art of getting good answers lies in asking good questions.

Go Up