Hi Guys, I have two files in a SD card and since they have 2000 values and I can't index it in an array, I was trying to make the operations I need with this two files and get the result, which is just one file and save it in an array.
I'll post the code I wrote so far, I think I'm in the right direction, but I don't know if the operation is being done right and how to convert char to float (I think that's is necessary in this case).
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
File myFile;
File myFile2;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
// re-open the file for reading:
float sum = 0;
myFile = SD.open("data000.txt");
myFile2 = SD.open("data001.txt");
if (myFile) {
Serial.println("FUNCIONANDO 1");
if (myFile2) {
Serial.println("FUNCIONANDO 2");
// read from the file until there's nothing else in it:
while (myFile.available()) {
while (myFile2.available()) {
sum = sum + (myFile.read()-myFile2.read())*(myFile.read()-myFile2.read());
Serial.println(sum);
}
}
// close the file:
myFile.close();
myFile2.close();
}
}
else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
//Serial.println(sum);
}
void loop()
{
// nothing happens after setup
}
char to float = 1 byte to 4 bytes.
So 2000 bytes to 8000 bytes.
Are you running this on a '1284P so you have 16K bytes SRAM to work in?
I have a new card out, Bobuino2, with 1284P and SD card, could be what you need here.
Available with onboard or offboard USB/Serial adapter.
Pics/details here: http://www.crossroadsfencing.com/BobuinoRev17/
Hi Guys, I have two files in a SD card and since they have 2000 values and I can't index it in an array,
You cannot normally index a file with an array.
What are you actually trying to accomplish ? Read two files ? Copy one to the other ? Compare them ?
The SD.h implementation will only let you access one file on the sd card at a time. The more recent and sophisticated SDFAT implementation allows you to access more than one file, I think.
I'm sorry if I was not clear.
Basically, I have two files data000.txt and data001.txt. They have 2000 (strings) values and I can organize them the way I want. (each value at a different line, or separated by ",").
The thing is that I want to do operations with the first line of the first file with the first line of the second file, and so on.
Lets say that data000.txt = [1, 2, 3, 4, 5] and data001.txt = [6, 7, 8, 9, 10].
I want the operation to be sum((data000.txt-data001.txt)^2), which is ((1-6)ˆ2+(2-7)ˆ2+...).
I'm trying to get just one number out of these 4000 numbers.
I've been read about getting numbers from SD card, but I couldn't figure it out yet.
I've been read about getting numbers from SD card, but I couldn't figure it out yet.
Write a function that takes a file pointer, and returns a value. Get that function to work (using Serial.print() to debug it). Then call that function to get a value from the first file. Call it again to get a value from the second file. Do whatever voodoo you want with the numbers.
You know where a number starts (after the last delimiter (or the beginning of the file)). You know where a number ends - at the delimiter or the end of the file. You know that the number is stored as a series of characters, so read all the characters until you find a delimiter or end of file. Convert the NULL terminated array of chars to a value using atoi(), atof(), strtoul(), etc.
How accurate must the sum of these 4000 values be? Arduino float is good to 6 places.
How much does the data vary? Is it all the same number of decimal places? If all the numbers are multiplied by 1000 or 1000000 then will there be no decimals? If so then you can convert them all to 32 bit or 64 bit integers for fast math and then divide by the 1000 or 1000000 at the end, simply by placing a decimal point if the only place that matters is a number for human eyes.
You can do text math which is code it the same way you do math by hand without a calculator.
Text math can always be right.
Michinyon has the right of SDFat IIRC. Not needing to close and open files over and over does speed the file code.
As far as sorting without using loads of RAM, that is a matter of reading a key value and working through the entire file(s) looking for a more or a less, then over and over with the new values until you have a first or next sorted record number to add to an index list that also become part of the sort since you don't sort already sorted records. At no time do you ever hold more than two keys. It is very mundane, slow code that works every time.