Reading from sd cards restarts after certain position in text file

I am making a 3d printer software as a school project using arduino. I have an arduino mega with an ethernet shield. The gcode which contains all the information needed to print will be stored on the sd card. Therefore I need to read from this file through out the print. I managed to read line by line and save values in float variables. However after a certain point of reading, the arduino starts reading from the beginning of the text file even though it hasn't reached the end of the text file.

Why do you think this is happening?

I'd venture a guess that there's a bug in your code. Since you didn't post it, I can't speculate as to what specifically it might be.

Click for Code:

Your request has been blocked by Xxxxxx Information Security

Now, attach your code HERE!

Attached is the code

code arduino forum.txt (10.4 KB)

You use the atof() function in several places in the program. This function takes a zero terminated array of chars as its argument and you are passing it the c_word array. However, I cannot see where you add the zero termination to the array.

On another note, you would be wise to eliminate those gotos from the program. At the very least they make following the program flow difficult and using them is generally regarded as bad practice.

You need to read an entire record, as a string (a NULL terminated array of chars). Only when you encounter the end of record marker (usually a carriage return or line feed) should you attempt to parse the data in the string.

And, you should do THAT in a separate function.

And there should be NO goto crap in the code.

You need to read a record, parse the record, move the equipment. Repeat.

Reading the record and repeating should happen in loop(). Parsing the record and moving the equipment should be in separate functions.

You REALLY need to look at strcmp(), strtok(), etc. There is no reason to reinvent the wheel.