MrMajd
December 10, 2018, 3:16pm
1
Hello everyone,
I'm working on a small project and i need to get some data from a txt file which is saved on SD card and process these data and then print the output on another file on the SD card.
I'm using Mega 2560 with an ITEAD GPS shield that comes with an SD card slot on it.
the 'Import' txt file with the saved values is structured like this:
1 2 3 4
5 6 7 8
and the results are printed to the txt file 'pvalues'
Here's my code (which is not working ):
#include <SPI.h>
#include <SD.h>
File dataFile;
const int chipSelect = 53;
int A[1];
int B[1];
int C[1];
int D[1];
bool imported = false;
int Ai=0;
void setup()
{
pinMode(chipSelect, OUTPUT);
if (!SD.begin(chipSelect)){
while(1);
}
}
void import()
{
dataFile = SD.open("Import.txt", FILE_READ);
if (dataFile)
{
while(dataFile.available())
{
for(Ai=0;Ai<2;Ai++)
{
A[Ai] = dataFile.parseInt();
B[Ai] = dataFile.parseInt();
C[Ai] = dataFile.parseInt();
D[Ai] = dataFile.parseInt();
}
dataFile.close();
}
}
}
void printValues()
{
dataFile = SD.open("pvalues.txt", FILE_WRITE);
if(dataFile)
{
while(dataFile.available())
{
for(Ai=0;Ai<2;Ai++)
{
dataFile.print(A[Ai]);
dataFile.print(' ');
dataFile.print(B[Ai]);
dataFile.print(' ');
dataFile.print(C[Ai]);
dataFile.print(' ');
dataFile.println(D[Ai]);
}
dataFile.close();
}
}
}
void loop()
{
if (imported == false)
{
import();
printValues();
imported = true;
}
}
could anyone help me to find out why isn't it working.
thanks
system
December 10, 2018, 3:26pm
2
could anyone help me to find out why isn't it working.
It does something that you failed to explain.
What it does is clearly different from what you want it to do, but it isn't clear what you want it to do.
You can NOT write 2 values in a one element array.
MrMajd
December 10, 2018, 3:33pm
3
if one is declearing an array like this: 'A[1]' doesn't that mean that it's a 2 element array 'A[0] and A[1]' ?
system
December 10, 2018, 3:37pm
4
MrMajd:
if one is declearing an array like this: 'A[1]' doesn't that mean that it's a 2 element array 'A[0] and A[1]' ?
No. The number in the brackets is the number of elements in the array, not the upper index.
MrMajd
December 10, 2018, 3:41pm
5
Thanks, I'm new to arduino and to programming in general.
could you please help me how to define the index of the array and then to assign a value to it
system
December 10, 2018, 4:02pm
6
could you please help me how to define the index of the array and then to assign a value to it
You need to declare your array properly, first.
int A[2];
Then, the code you posted should do what you want. If it does, you need to explain exactly what it does, and how that differs from what you want.
You do NOT need a global variable for the loop index. Use a local variable, of a more appropriate type:
for(byte i=0; i<2; i++)
MrMajd
December 10, 2018, 4:11pm
7
PaulS, I tried what you suggested
and this is my code now:
#include <SPI.h>
#include <SD.h>
File dataFile;
const int chipSelect = 53;
int A[2];
int B[2];
int C[2];
int D[2];
bool imported = false;
void setup()
{
pinMode(chipSelect, OUTPUT);
if (!SD.begin(chipSelect)){
while(1);
}
}
void import()
{
dataFile = SD.open("Import.txt", FILE_READ);
if (dataFile)
{
while(dataFile.available())
{
for(byte i=0;i<2;i++)
{
A[i] = dataFile.parseInt();
B[i] = dataFile.parseInt();
C[i] = dataFile.parseInt();
D[i] = dataFile.parseInt();
}
dataFile.close();
}
}
}
void printValues()
{
dataFile = SD.open("pvalues.txt", FILE_WRITE);
if(dataFile)
{
while(dataFile.available())
{
for(byte p=0;p<2;p++)
{
dataFile.print(A[p]);
dataFile.print(' ');
dataFile.print(B[p]);
dataFile.print(' ');
dataFile.print(C[p]);
dataFile.print(' ');
dataFile.println(D[p]);
}
dataFile.close();
}
}
}
void loop()
{
if (imported == false)
{
import();
printValues();
imported = true;
}
}
but i still get an empty txt file when i check the SD card
system
December 10, 2018, 4:17pm
8
but i still get an empty txt file when i check the SD card
So, where is the problem occurring? Add Serial.print() statements to find out.
while(dataFile.available())
{
for(byte i=0;i<2;i++)
{
A[i] = dataFile.parseInt();
B[i] = dataFile.parseInt();
C[i] = dataFile.parseInt();
D[i] = dataFile.parseInt();
}
dataFile.close();
}
As long as there is ANY data left to read in the file, read 8 ints and close the file. I really don't think the while statement is appropriate.
void printValues()
{
This function doesn't print anything, in the sense of being able to see the output somewhere. A more appropriate name is on order.
MrMajd
December 10, 2018, 4:36pm
9
PaulS:
void printValues()
{
This function doesn't print anything, in the sense of being able to see the output somewhere. A more appropriate name is on order.
this suppose to print the same values on the another txt file, so i could verify if the reading was done from the first txt file. what do you mean with ''A more appropriate name is on order'' does the name of the function really matter ?
system
December 10, 2018, 4:41pm
10
does the name of the function really matter ?
No, certainly not. Feel free to call it F14516136().
MrMajd
December 10, 2018, 4:46pm
11
so it finally worked, i removed the while loop as you suggested
thanks a lot.
I'll try to integreate it with the full code and ask you for help again if get any problems