SD card reading

Hi,
I wanted to read from one file in a SD card, change the read character and write it to an other file.
But only the first character is read each time and even that read character is not written to an other file. My code is

#include <SD.h>
char Instruction;
const int CS_PIN = 53;
File IN_Track;
File Return_Track;
File Resolute_Track;
int i = 0;
void setup() //all robot if else
{
Serial.begin(9600);
pinMode(CS_PIN, OUTPUT);
SD.begin();
Serial.println(“SD card detected”);
}

void loop()
{
SD_Return();
}

//file.seek(file.size());

void SD_Return()
{
IN_Track = SD.open(“Track.txt”);
int pos;
if(IN_Track)
{
//IN_Track.seek(’\0’);
pos = (IN_Track.position());
//pos --;

IN_Track.seek(pos + i);
char Go = IN_Track.read();
if(Go == ‘S’)
{
(Go = ‘S’);
Serial.println(Go);
}
else if(Go == ‘L’)
{
(Go = ‘R’);
Serial.println(Go);
}
else if(Go == ‘R’)
{
(Go = ‘L’);
Serial.println(Go);
}
else if(Go == ‘E’ || Go == ‘T’)
{
(Go = ‘S’);
Serial.println(Go);
}
else if(Go == ‘f’)
(Go = ‘g’);
else if(Go == ‘g’)
(Go = ‘f’);

IN_Track.close();
SD2_Write(Go);
i +2;
}
else
IN_Track.close();
}

void SD2_Write(char R_Go)
{
Return_Track = SD.open(“Return.txt”);
Serial.println(“File created”);
if(Return_Track)
Return_Track.println(R_Go);
Return_Track.close();
}

output:
SD card detected
S
File created
S
File created
(Repeatedly printed in serial monitor)

Track file contains the following data:
S
L
R
E
T

pos is a local variable in function SD_Return(). It is reset to 0 each time SD_Return() is called from the loop() function. Is that what you want ?
If not, move the declaration of pos to the top of the script.

hi,
I moved the declaration to top. But I get the same output, and in is not written in return file as per the program

Yes. I see now what you are trying to do, and the correction is a little more extensive than I thought.
In principle you appear to want to do the following:

Open the input file Intrack
Get the first entry in the file
While there are still entries in the file, execute the following logic:
{
read print Write_to_Return_Track
S S S
L R R
R L L
E,T S S
f - g
g - f

}
close the input file

The way you have structured your code at the moment, it will continually process the first entry in the file.
The call to SD_Return() should be made at the end of setup() and be removed from loop().
Within SD_Return() you should have a while statement to step through the contents of the input file, implementing your logic to print and write to the output file.

Here is an example which more or less matches what you want to do: https://www.arduino.cc/en/Tutorial/ReadWrite

Hi,
Thanks for helping out. Is there any way if I could read the file from last to the beginning, like at first seeking the null position(which would be the end the file) ?

ramyar:
Hi,
Thanks for helping out. Is there any way if I could read the file from last to the beginning, like at first seeking the null position(which would be the end the file) ?

Try:

https://www.element14.com/community/thread/41030/l/how-can-i-read-from-a-sd-card-file-backwards?displayFullThread=true